UNPKG

@state-sync/redux-path-reducer

Version:
57 lines (56 loc) 1.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function isEmpty(obj) { for (var key in obj) { if (obj.hasOwnProperty(key)) return false; } return true; } var InvocationMap = /** @class */ (function () { function InvocationMap() { this.lastRequestId = 0; this.invocations = {}; this.timeout = 60000; } InvocationMap.prototype.isEmpty = function () { return isEmpty(this.invocations); }; InvocationMap.prototype.request = function (sender) { var _this = this; this.lastRequestId++; var id = this.lastRequestId; var promise = new Promise(function (resolve, reject) { sender(id); var inv = { id: id, resolve: resolve, reject: reject }; _this.invocations[id] = inv; setTimeout(function () { var p = _this.invocations[id]; if (p) { p.reject('timeout'); delete _this.invocations[id]; } }, _this.timeout); }); return promise; }; InvocationMap.prototype.response = function (forId) { console.info('response id:' + forId, this.invocations); var p = this.invocations[forId]; if (p) { p.resolve(forId); delete this.invocations[forId]; } return this.isEmpty(); }; InvocationMap.prototype.error = function (forId, error) { var p = this.invocations[forId]; if (p) { p.reject(error); delete this.invocations[forId]; } return this.isEmpty(); }; return InvocationMap; }()); exports.InvocationMap = InvocationMap;