English 中文(简体)
Managing a shared resource with functional programming style in JavaScript
原标题:

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.

最佳回答

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

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签