Skip to content

Commit

Permalink
[lib] add locks feature
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Sep 1, 2011
1 parent 9248cb1 commit 249a2a0
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion lib/Porter.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
},
delay: false
};
this._locks = {};

this.headers = this.options.headers;

Expand Down Expand Up @@ -89,6 +91,18 @@

ajax.run = function(callbacks, validators) {

// Setup locks
if (!conf.ignoreLocks && conf.type !== 'GET' && self._locks[conf.url]) {
self._locks[conf.url].push({
conf: conf,
callbacks: callbacks,
validators: validators
});
return;
} else {
self._locks[conf.url] = [];
}

// merge the global _listeners into this xhr.
for(l in self._listeners) {
if(self._listeners.hasOwnProperty(l)) {
Expand Down Expand Up @@ -160,6 +174,33 @@
break;
case 4:

// Release locks or run delayed requests
if (!conf.ignoreLocks && conf.type !== 'GET' &&
self.options.delay !== false) {

setTimeout(function() {
var data = self._locks[conf.url].shift();

// If we have any request queued
if (data) {
var _locks = self._locks;

// Temporally clear locks
self._locks = {};

// Do unlocked request
var req = new self.ajax(data.conf);
req.run(data.callbacks, data.validators);

// Restore locks
self._locks = _locks;
} else {
// Remove lock
self._locks[conf.url] = null;
}
}, self.options.delay);
}

var response = ajax.xhr.responseText.trim(),
status = ajax.xhr.status,
statusText = ajax.xhr.statusText,
Expand Down

0 comments on commit 249a2a0

Please sign in to comment.