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.