electron-event-flux
Version:
Redux store which synchronizes between instances in multiple process
57 lines (56 loc) • 2.15 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var StoreBase_1 = require("./StoreBase");
var SubIdGenerator = /** @class */ (function () {
function SubIdGenerator() {
this.count = 0;
this.prefix = Math.random().toString(36).slice(2, 5);
}
SubIdGenerator.prototype.genId = function () {
this.count += 1;
return this.prefix + this.count;
};
return SubIdGenerator;
}());
// The StoreBase that can subscribe and unsubscribe
var SubStoreBase = /** @class */ (function (_super) {
__extends(SubStoreBase, _super);
function SubStoreBase() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.subMap = {};
_this.idGenerator = new SubIdGenerator();
return _this;
}
SubStoreBase.prototype.unsubscribe = function (subId) {
var dispose = this.subMap[subId];
if (!dispose)
console.error("The subId " + subId + " isnot subscribed");
dispose && dispose.dispose();
delete this.subMap[subId];
};
SubStoreBase.prototype.genSubId = function (dispose) {
var id = this.idGenerator.genId();
this.subMap[id] = dispose;
return id;
};
SubStoreBase.prototype.dispose = function () {
_super.prototype.dispose.call(this);
var subMap = this.subMap;
Object.keys(subMap).forEach(function (key) { return subMap[key] && subMap[key].dispose(); });
};
return SubStoreBase;
}(StoreBase_1.default));
exports.default = SubStoreBase;
;