clean-architecture
Version:
Utilities for implementing clean architecture using Redux
34 lines • 1.33 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.createUsecaseContextApi = void 0;
var assert_1 = require("tsafe/assert");
function createUsecaseContextApi(getInitialContext) {
var weakMap = new WeakMap();
return {
"getContext": function (rootContext) {
var getMemoizedContext = weakMap.get(rootContext);
if (getMemoizedContext !== undefined) {
return getMemoizedContext();
}
(0, assert_1.assert)(getInitialContext !== undefined, "Slice context not initialized");
getMemoizedContext = memoize(getInitialContext);
weakMap.set(rootContext, getMemoizedContext);
return getMemoizedContext();
},
"setContext": function (rootContext, context) {
return weakMap.set(rootContext, memoize(typeof context === "function" ? context : function () { return context; }));
},
"getIsContextSet": function (rootContext) { return weakMap.has(rootContext); }
};
}
exports.createUsecaseContextApi = createUsecaseContextApi;
function memoize(fn) {
var cache = undefined;
return function () {
if (cache !== undefined) {
return cache;
}
return (cache = fn());
};
}
//# sourceMappingURL=usecaseContext.js.map
;