overmind
Version:
Frictionless state management
147 lines • 5.92 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.createActionsProxy = exports.getActionPaths = exports.getActionsByName = exports.getChangeMutations = exports.getFunctionName = exports.processState = exports.isPromise = exports.json = exports.MockedEventEmitter = exports.MODE_SSR = exports.MODE_TEST = exports.MODE_DEFAULT = exports.EXECUTION = exports.ORIGINAL_ACTIONS = exports.IS_OPERATOR = exports.IS_TEST = exports.ENVIRONMENT = exports.createOnInitialize = exports.deepCopy = void 0;
const tslib_1 = require("tslib");
const is_plain_obj_1 = tslib_1.__importDefault(require("is-plain-obj"));
const proxy_state_tree_1 = require("proxy-state-tree");
const statemachine_1 = require("./statemachine");
// Due to avoid circular dependency warnings we export this utility from here
var statemachine_2 = require("./statemachine");
Object.defineProperty(exports, "deepCopy", { enumerable: true, get: function () { return statemachine_2.deepCopy; } });
const createOnInitialize = () => {
return ({ actions }, instance) => {
const initializers = getActionsByName('onInitializeOvermind', actions);
return Promise.all(initializers.map((initialize) => initialize(instance)));
};
};
exports.createOnInitialize = createOnInitialize;
exports.ENVIRONMENT = (() => {
let env;
try {
env = process.env.NODE_ENV;
}
catch (_a) {
console.warn('Overmind was unable to determine the NODE_ENV, which means it will run in DEVELOPMENT mode. If this is a production app, please configure your build tool to define NODE_ENV');
env = 'development';
}
return env;
})();
exports.IS_TEST = exports.ENVIRONMENT === 'test';
exports.IS_OPERATOR = Symbol('operator');
exports.ORIGINAL_ACTIONS = Symbol('origina_actions');
exports.EXECUTION = Symbol('execution');
exports.MODE_DEFAULT = Symbol('MODE_DEFAULT');
exports.MODE_TEST = Symbol('MODE_TEST');
exports.MODE_SSR = Symbol('MODE_SSR');
class MockedEventEmitter {
emit() { }
emitAsync() { }
on() { }
once() { }
addListener() { }
}
exports.MockedEventEmitter = MockedEventEmitter;
const json = (obj) => {
return (0, statemachine_1.deepCopy)(obj && obj[proxy_state_tree_1.IS_PROXY] ? obj[proxy_state_tree_1.VALUE] : obj);
};
exports.json = json;
function isPromise(maybePromise) {
return (maybePromise instanceof Promise ||
(maybePromise &&
typeof maybePromise.then === 'function' &&
typeof maybePromise.catch === 'function'));
}
exports.isPromise = isPromise;
function processState(state) {
return Object.keys(state).reduce((aggr, key) => {
if (key === '__esModule') {
return aggr;
}
const originalDescriptor = Object.getOwnPropertyDescriptor(state, key);
if (originalDescriptor && 'get' in originalDescriptor) {
Object.defineProperty(aggr, key, originalDescriptor);
return aggr;
}
const value = state[key];
if ((0, is_plain_obj_1.default)(value)) {
aggr[key] = processState(value);
}
else {
Object.defineProperty(aggr, key, originalDescriptor);
}
return aggr;
}, (0, is_plain_obj_1.default)(state) ? {} : state);
}
exports.processState = processState;
function getFunctionName(func) {
return func.name || func.displayName || '';
}
exports.getFunctionName = getFunctionName;
const getChangeMutationsDelimiter = '.';
function getChangeMutations(stateA, stateB, path = [], mutations = []) {
const stateAKeys = Object.keys(stateA);
const stateBKeys = Object.keys(stateB);
stateAKeys.forEach((key) => {
if (!stateBKeys.includes(key)) {
mutations.push({
delimiter: getChangeMutationsDelimiter,
args: [],
path: path.concat(key).join('.'),
hasChangedValue: false,
method: 'unset',
});
}
});
stateBKeys.forEach((key) => {
if ((0, is_plain_obj_1.default)(stateA[key]) && (0, is_plain_obj_1.default)(stateB[key])) {
getChangeMutations(stateA[key], stateB[key], path.concat(key), mutations);
}
else if (stateA[key] !== stateB[key]) {
mutations.push({
delimiter: getChangeMutationsDelimiter,
args: [stateB[key]],
path: path.concat(key).join('.'),
hasChangedValue: false,
method: 'set',
});
}
});
return mutations;
}
exports.getChangeMutations = getChangeMutations;
function getActionsByName(name, actions = {}, currentPath = []) {
return Object.keys(actions).reduce((aggr, key) => {
if (typeof actions[key] === 'function' && key === name) {
return aggr.concat(actions[key]);
}
return aggr.concat(getActionsByName(name, actions[key], currentPath.concat(key)));
}, []);
}
exports.getActionsByName = getActionsByName;
function getActionPaths(actions = {}, currentPath = []) {
return Object.keys(actions).reduce((aggr, key) => {
if (typeof actions[key] === 'function') {
return aggr.concat(currentPath.concat(key).join('.'));
}
return aggr.concat(getActionPaths(actions[key], currentPath.concat(key)));
}, []);
}
exports.getActionPaths = getActionPaths;
function createActionsProxy(actions, cb) {
return new Proxy(actions, {
get(target, prop) {
if (prop === exports.ORIGINAL_ACTIONS) {
return actions;
}
if (typeof target[prop] === 'function') {
return cb(target[prop]);
}
if (!target[prop]) {
return undefined;
}
return createActionsProxy(target[prop], cb);
},
});
}
exports.createActionsProxy = createActionsProxy;
//# sourceMappingURL=utils.js.map
;