diffusion
Version:
Diffusion JavaScript client
54 lines (53 loc) • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WorkerServiceRequestRegistry = void 0;
var conversation_id_1 = require("./../conversation/conversation-id");
var Long = require("long");
var NEXT_RID = (function () {
var id = new Long(0);
return function () {
id = id.add(1);
return new conversation_id_1.ConversationId(id);
};
})();
/**
* A registry for active service requests
*/
var WorkerServiceRequestRegistry = /** @class */ (function () {
function WorkerServiceRequestRegistry() {
this.activeRequests = {};
}
/**
* Add a callback and register it under a new ID
*
* @return the ID with which the request can be retrieved
*/
WorkerServiceRequestRegistry.prototype.addRequest = function (callback) {
var requestId = NEXT_RID();
this.activeRequests[requestId.toString()] = callback;
return requestId;
};
/**
* Get the callback for an ID.
*
* Once the callback has been used to send a response or fail, it will be
* removed from the registry.
*/
WorkerServiceRequestRegistry.prototype.getCallback = function (rid) {
var _this = this;
var key = rid.toString();
var callback = this.activeRequests[key];
return {
respond: function (response) {
callback.respond(response);
delete _this.activeRequests[key];
},
fail: function (error, msg) {
callback.fail(error, msg);
delete _this.activeRequests[key];
}
};
};
return WorkerServiceRequestRegistry;
}());
exports.WorkerServiceRequestRegistry = WorkerServiceRequestRegistry;