2010-12-08 09:41:06 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a callback container
|
|
|
|
* @return {Object} that wraps callbacks and returns a one-time id.
|
|
|
|
*/
|
2010-10-25 05:16:40 +04:00
|
|
|
exports.create = function() {
|
|
|
|
var lastId = 1,
|
|
|
|
callbacks = {};
|
|
|
|
|
|
|
|
return Object.create({}, {
|
|
|
|
wrap: {
|
|
|
|
value: function(callback) {
|
|
|
|
var callbackId = lastId++;
|
|
|
|
callbacks[callbackId] = callback || function() {};
|
|
|
|
return callbackId;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
processResponse: {
|
|
|
|
value: function(callbackId, args) {
|
|
|
|
var callback = callbacks[callbackId];
|
2013-12-02 18:06:18 +04:00
|
|
|
|
|
|
|
if(callback) {
|
|
|
|
callback.apply(null, args);
|
|
|
|
}
|
|
|
|
|
2010-10-25 05:16:40 +04:00
|
|
|
delete callbacks[callbackId];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
removeResponseCallbackEntry: {
|
|
|
|
value: function(callbackId) {
|
|
|
|
delete callbacks[callbackId];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|