views:

59

answers:

1

How does the functional programming paradigm deal with shared resources that need to maintain state?

I have a JavaScipt program that allows access from multiple callers to a shared resource, a authenticated HTTP session in this case.

The resource has bunch of state information associated with it including things like credentials, cache time, polling intervals, already visited links, and other stuff needed to interact with a RESTful data service. The important thing is that the HTTP session needs to operate serially. Only one caller at a time should be allowed access to it, and the next caller may not be allowed to use it until the server responds to the first caller.

Curently I'm doing this by rolling my own buffer (a simple JavaScript array of requests lined up in a pool). However, the program is particularly prone to defects because of all the mutable data, so I'm trying to refactor using a more functional programming design.

From what I understand, the way to handle state in functional programming is to pass a copy of a new data structure to a caller rather than modifying state variables on an object. This is great for the most part, except if I recreate the shared resource (the HTTP session) for each call to the API, then the HTTP service (a RESTful client) becomes inconsistent, creating errors on the server.

+1  A: 

don't expose the http session, instead let the client code queue requests, each with a function to be called with the response.

just somebody
Yeah, I'm currently queuing requests in a buffer/pool, but I think I need to encapsulate the http session better so that it is not exposed to the api, and then I can do all my functional programming inside the capsule.
Kris Walker