core-native
Version:
A lightweight framework based on React Native + Redux + Redux Saga, in strict TypeScript.
99 lines • 3.56 kB
JavaScript
import { __generator } from "tslib";
import { app } from "../app";
import {} from "../Logger";
import { produce, enablePatches } from "immer";
import {} from "../module";
import { setStateAction } from "../reducer";
import {} from "../typed-saga";
if (process.env.NODE_ENV === "development")
enablePatches();
var Module = /** @class */ (function () {
function Module(name, initialState) {
this.name = name;
this.initialState = initialState;
}
Module.prototype.onEnter = function (routeParameters) {
return __generator(this, function (_a) {
return [2 /*return*/];
});
};
Module.prototype.onDestroy = function () {
return __generator(this, function (_a) {
return [2 /*return*/];
});
};
Module.prototype.onTick = function () {
return __generator(this, function (_a) {
return [2 /*return*/];
});
};
Module.prototype.onAppActive = function () {
return __generator(this, function (_a) {
return [2 /*return*/];
});
};
Module.prototype.onAppInactive = function () {
return __generator(this, function (_a) {
return [2 /*return*/];
});
};
Module.prototype.onFocus = function () {
return __generator(this, function (_a) {
return [2 /*return*/];
});
};
Module.prototype.onBlur = function () {
return __generator(this, function (_a) {
return [2 /*return*/];
});
};
Object.defineProperty(Module.prototype, "state", {
get: function () {
return this.rootState.app[this.name];
},
enumerable: false,
configurable: true
});
Object.defineProperty(Module.prototype, "rootState", {
get: function () {
return app.store.getState();
},
enumerable: false,
configurable: true
});
Object.defineProperty(Module.prototype, "logger", {
get: function () {
return app.logger;
},
enumerable: false,
configurable: true
});
Module.prototype.setState = function (stateOrUpdater) {
if (typeof stateOrUpdater === "function") {
var originalState = this.state;
var updater_1 = stateOrUpdater;
var patchDescriptions_1;
// TS cannot infer RootState["app"][ModuleName] as an object, so immer fails to unwrap the readonly type with Draft<T>
var newState = produce(originalState, function (draftState) {
// Wrap into a void function, in case updater() might return anything
updater_1(draftState);
}, process.env.NODE_ENV === "development"
? function (patches) {
// No need to read "op", in will only be "replace"
patchDescriptions_1 = patches.map(function (_) { return _.path.join("."); });
}
: undefined);
if (newState !== originalState) {
var description = "@@".concat(this.name, "/setState").concat(patchDescriptions_1 ? "[".concat(patchDescriptions_1.join("/"), "]") : "");
app.store.dispatch(setStateAction(this.name, newState, description));
}
}
else {
var partialState_1 = stateOrUpdater;
this.setState(function (state) { return Object.assign(state, partialState_1); });
}
};
return Module;
}());
export { Module };
//# sourceMappingURL=Module.js.map