typedux
Version:
Slightly adjusted Redux (awesome by default) for TS
240 lines • 9.32 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "@3fv/prelude-ts", "../util", "./Actions", "@3fv/logger-proxy", "shortid", "@3fv/guard", "immutable", "lodash", "../dev"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseActionFactory = void 0;
var prelude_ts_1 = require("@3fv/prelude-ts");
var util_1 = require("../util");
var Actions_1 = require("./Actions");
var logger_proxy_1 = require("@3fv/logger-proxy");
var ID = __importStar(require("shortid"));
var guard_1 = require("@3fv/guard");
var Immutable = __importStar(require("immutable"));
var lodash_1 = require("lodash");
var dev_1 = require("../dev");
var log = logger_proxy_1.getLogger(__filename);
/**
* Base class for action implementations for a given state
*
*/
var BaseActionFactory = /** @class */ (function () {
/**
* Create a new action factory that consumes and produces a specific
* state type
*
* @param stateType
* @param withStore
*/
function BaseActionFactory(stateType, withStore) {
if (withStore === void 0) { withStore = undefined; }
if (log.isDebugEnabled() && dev_1.isDev) {
log.debug("Created action factory with state type: " + stateType.name);
}
this.stateType = stateType;
this.store = withStore;
this.pushStoreActions();
}
BaseActionFactory.prototype.pushStoreActions = function () {
var _this = this;
var _a;
prelude_ts_1.Option.ofNullable((_a = this.store) === null || _a === void 0 ? void 0 : _a.actionContainer)
.ifSome(function (actions) {
_this.actions.forEach(actions.registerAction.bind(actions));
});
};
Object.defineProperty(BaseActionFactory.prototype, "actions", {
/**
* @inheritDoc
*/
get: function () {
return lodash_1.uniqBy(__spread(this.actionMap.values()), function (_a) {
var fullName = _a.fullName;
return fullName;
});
},
enumerable: false,
configurable: true
});
BaseActionFactory.prototype.getAction = function (leafOrFullName, type) {
var _this = this;
return [leafOrFullName, Actions_1.createLeafActionType(leafOrFullName, type)]
.map(function (key) { return _this.actionMap.get(key); })
.filter(guard_1.isDefined)[0];
};
/**
* @inheritDoc
*/
BaseActionFactory.prototype.registerAction = function (reg) {
var _a;
var map = this.actionMap = (_a = this.actionMap) !== null && _a !== void 0 ? _a : new Map();
Array(reg.fullName, reg.type)
.forEach(function (key) { return map.set(key, reg); });
this.pushStoreActions();
return reg;
};
BaseActionFactory.prototype.getStore = function () {
var _a, _b;
return (_b = (_a = this.store) !== null && _a !== void 0 ? _a : BaseActionFactory.clazzStore) !== null && _b !== void 0 ? _b : Actions_1.getGlobalStore();
};
Object.defineProperty(BaseActionFactory.prototype, "dispatcher", {
/**
* Get the current dispatcher
*
* Implemented for the purpose of thunks
* etc where the dispatcher can be augmented
*
* @returns {Function|(function(any): any)}
*/
get: function () {
var store = this.getStore();
if (!store || !guard_1.isFunction(store.dispatch)) {
throw new Error("Global dispatcher must be set before any actions occur");
}
return (function (action) {
return store.dispatch(action);
});
},
enumerable: false,
configurable: true
});
Object.defineProperty(BaseActionFactory.prototype, "state", {
/**
* Retrieve the current state using the global
* getState or the augmented one
*
* directly applicable to @see dispatcher
*
* @returns instance of the state supported by this factory
*/
get: function () {
var store = this.getStore();
//const getStoreState = getGlobalStateProvider()
var state = store === null || store === void 0 ? void 0 : store.getState();
if (!state)
return null;
var leaf = this.leaf();
return !leaf
? state
: Immutable.Map.isMap(state)
? state.get(leaf)
: state[leaf];
},
enumerable: false,
configurable: true
});
/**
* withDispatcher creates a new instance of
* this action implementation with a
* new dispatcher and optionally a new
* getState
*
* @returns {any}
* @param newStore
*/
BaseActionFactory.prototype.withStore = function (newStore) {
var instance = lodash_1.clone(this);
instance.setStore(newStore);
return instance;
};
/**
* Set the store for action factory
*
* @param newStore
* @return {this<S, M>}
*/
BaseActionFactory.prototype.setStore = function (newStore) {
this.store = newStore;
this.pushStoreActions();
return this;
};
/**
* Create a new action message object that
* fits the shape defined by the generic M
*
* @param id
* @param type
* @param reducers
* @param data
* @param args
* @param leaf
*/
BaseActionFactory.prototype.newMessage = function (id, leaf, type, reducers, args, data) {
if (reducers === void 0) { reducers = []; }
if (args === void 0) { args = []; }
if (data === void 0) { data = {}; }
return Object.assign({
id: prelude_ts_1.Option.ofNullable(id)
.filter(util_1.isNotEmpty)
.getOrCall(function () { return ID.generate(); }),
leaf: leaf,
type: Actions_1.createLeafActionType(this.leaf(), type),
reducers: reducers,
args: args,
stateType: this.stateType
}, data);
};
/**
* setError action applies to all states
*
* @param error
*/
//@ActionThunk()
BaseActionFactory.prototype.setError = function (error) { };
return BaseActionFactory;
}());
exports.BaseActionFactory = BaseActionFactory;
(function (BaseActionFactory) {
function setStore(newClazzStore) {
BaseActionFactory.clazzStore = newClazzStore;
}
BaseActionFactory.setStore = setStore;
})(BaseActionFactory || (BaseActionFactory = {}));
exports.BaseActionFactory = BaseActionFactory;
});
//# sourceMappingURL=BaseActionFactory.js.map