UNPKG

@tacky/store

Version:

State management framework based on react

103 lines (76 loc) 2.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mutation = mutation; var _store = require("../core/store"); var _symbol = require("../const/symbol"); var _common = require("../utils/common"); var _interfaces = require("../interfaces"); var _error = require("../utils/error"); var _decorator = require("../utils/decorator"); var _domain = require("../core/domain"); function createMutation(target, name, original, isAtom) { var stringMethodName = (0, _common.convert2UniqueString)(name); return function () { this[_symbol.CURRENT_MATERIAL_TYPE] = _interfaces.EMaterialType.MUTATION; _domain.materialCallStack.push(this[_symbol.CURRENT_MATERIAL_TYPE]); for (var _len = arguments.length, payload = new Array(_len), _key = 0; _key < _len; _key++) { payload[_key] = arguments[_key]; } _store.store.dispatch({ name: stringMethodName, payload: payload, type: _interfaces.EMaterialType.MUTATION, domain: this, original: (0, _common.bind)(original, this), isAtom: isAtom }); _domain.materialCallStack.pop(); var length = _domain.materialCallStack.length; this[_symbol.CURRENT_MATERIAL_TYPE] = _domain.materialCallStack[length - 1] || _interfaces.EMaterialType.DEFAULT; }; } /** * decorator @mutation, update state by mutation styling. */ function mutation() { var isAtom = false; var decorator = function decorator(target, name, descriptor) { // typescript only: @mutation method = () => {} if (descriptor === void 0) { var mutationFunc; Object.defineProperty(target, name, { enumerable: true, configurable: true, get: function get() { return mutationFunc; }, set: function set(original) { mutationFunc = createMutation(target, name, original, isAtom); } }); return; } // babel/typescript: @mutation method() {} if (descriptor.value !== void 0) { var original = descriptor.value; descriptor.value = createMutation(target, name, original, isAtom); return descriptor; } // babel only: @mutation method = () => {} var initializer = descriptor.initializer; descriptor.initializer = function () { (0, _error.invariant)(!!initializer, 'The initializer of the descriptor doesn\'t exist, please compile it by using babel and correspond decorator plugin.'); return createMutation(target, name, initializer && initializer.call(this), isAtom); }; return descriptor; }; for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } if ((0, _decorator.quacksLikeADecorator)(args)) { // @mutation return decorator.apply(null, args); } // @mutation(args) isAtom = args[0] !== void 0 ? args[0] : false; return decorator; }