electron-event-flux
Version:
Redux store which synchronizes between instances in multiple process
83 lines (82 loc) • 2.88 kB
JavaScript
"use strict";
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
var StoreBase_1 = require("./StoreBase");
var IS_APP_STORE = '@@__APP_STORE__@@';
var BatchUpdateHost = /** @class */ (function () {
function BatchUpdateHost(appStore) {
this.runState = 'idle';
this.appStore = appStore;
}
// The AppStore need to update the state
BatchUpdateHost.prototype.requestUpdate = function () {
var _this = this;
if (this.runState === 'idle') {
this.runState = 'prepare';
// Collect all of the update request and update AppStore After 20ms
setTimeout(function () { return _this.runUpdate(); }, 20);
}
};
BatchUpdateHost.prototype.runUpdate = function () {
this.runState = 'idle';
this.appStore._sendUpdate(); //enable appStore update
};
return BatchUpdateHost;
}());
exports.BatchUpdateHost = BatchUpdateHost;
var AppStore = /** @class */ (function () {
function AppStore() {
this._init = false;
this.didChangeCallbacks = [];
this.prevState = {};
this.state = {};
this.stores = {};
this[_a] = true;
this.batchUpdater = new BatchUpdateHost(this);
}
AppStore.isAppStore = function (maybeAppStore) {
return !!(maybeAppStore && maybeAppStore[IS_APP_STORE]);
};
;
AppStore.prototype.buildStore = function (storeClass, args, options) {
return StoreBase_1.buildStore(this, storeClass, args, options);
};
AppStore.prototype.setState = function (state) {
if (!this._init) { // 未初始化完成
Object.assign(this.state, state);
}
else {
this.state = Object.assign({}, this.state, state);
this.batchUpdater.requestUpdate();
}
};
AppStore.prototype._sendUpdate = function () {
var _this = this;
this.handleWillChange && this.handleWillChange(this.prevState, this.state);
this.didChangeCallbacks.forEach(function (callback) { return callback(_this.state); });
this.prevState = this.state;
};
AppStore.prototype.handleWillChange = function (prevState, state) {
};
AppStore.prototype.onDidChange = function (callback) {
this.didChangeCallbacks.push(callback);
};
AppStore.prototype.init = function () {
this._init = true;
this.prevState = this.state;
return this;
};
AppStore.prototype.dispose = function () {
this.didChangeCallbacks = [];
for (var key in this.stores) {
var store = this.stores[key];
if (store instanceof StoreBase_1.default) {
store.dispose();
}
}
this.prevState = this.state = this.stores = null;
};
return AppStore;
}());
_a = IS_APP_STORE;
exports.default = AppStore;