UNPKG

redux-typed-kit

Version:

Powerful extensitions for building class-based Redux architecture powered by TypeScript.

96 lines 4.36 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import 'reflect-metadata'; import { METADATA_KEY_ACTION, METADATA_KEY_MIDDLEWARE_TYPE } from './constants'; var Middleware = /** @class */ (function () { function Middleware() { this.preActionsMap = new Map(); this.postActionsMap = new Map(); } Middleware.prototype.executePre = function (store, action, actionType) { var preFuncs = this.preActionsMap.get(actionType || action.type); if (preFuncs != null) { for (var _i = 0, preFuncs_1 = preFuncs; _i < preFuncs_1.length; _i++) { var preFunc = preFuncs_1[_i]; preFunc.bind(this)(store, action); } } }; Middleware.prototype.executePost = function (store, action, prevState, actionType) { var postFuncs = this.postActionsMap.get(actionType || action.type); if (postFuncs != null) { for (var _i = 0, postFuncs_1 = postFuncs; _i < postFuncs_1.length; _i++) { var postFunc = postFuncs_1[_i]; postFunc.bind(this)(store, action, prevState); } } }; Middleware.prototype.call = function (store) { var _this = this; return (function (reduxStore) { return function (next) { return function (action) { _this.executePre(store, action); // for all listeners with 'any' type of action _this.executePre(store, action, Object.name); var prevState = store.state; next(action); _this.executePost(store, action, prevState); // for all listeners with 'any' type of action _this.executePost(store, action, prevState, Object.name); }; }; }); }; Middleware.prototype.create = function (store) { // idk why all methods are in prototype class var proto = Object.getPrototypeOf(this); while (proto.constructor.name != Middleware.name) { var methodNames = Object.getOwnPropertyNames(proto).filter(function (x) { return x != 'constructor' && !x.startsWith('_'); }); for (var _i = 0, methodNames_1 = methodNames; _i < methodNames_1.length; _i++) { var key = methodNames_1[_i]; var metaActionType = Reflect.getMetadata(METADATA_KEY_ACTION, this, key); if (metaActionType == null) continue; var handlerType = Reflect.getMetadata(METADATA_KEY_MIDDLEWARE_TYPE, this, key); var func = this[key]; if (handlerType === 'pre') { if (!this.preActionsMap.has(metaActionType)) this.preActionsMap.set(metaActionType, []); this.preActionsMap.get(metaActionType).push(func); } else { if (!this.postActionsMap.has(metaActionType)) this.postActionsMap.set(metaActionType, []); this.postActionsMap.get(metaActionType).push(func); } } proto = Object.getPrototypeOf(proto); } return this.call(store).bind(this); }; return Middleware; }()); export { Middleware }; var IsolatedMiddleware = /** @class */ (function (_super) { __extends(IsolatedMiddleware, _super); function IsolatedMiddleware(getState) { var _this = _super.call(this) || this; _this.getState = getState; return _this; } return IsolatedMiddleware; }(Middleware)); export { IsolatedMiddleware }; //# sourceMappingURL=middleware.js.map