reshow-flux
Version:
The smallest react flux and Fast hook alternative
163 lines (146 loc) • 4.23 kB
JavaScript
import _objectSpread from "reshow-runtime/es/helpers/objectSpread2";
import _createClass from "reshow-runtime/es/helpers/createClass";
import _classCallCheck from "reshow-runtime/es/helpers/classCallCheck";
import _defineProperty from "reshow-runtime/es/helpers/defineProperty";
// @ts-check
import { forEachMap } from "get-object-value";
import { Map, Set, fromJS, is as equal } from "immutable";
import { createReducer } from "reshow-flux-base";
import callfunc from "call-func";
import toJS from "./toJS.mjs";
import { STRING } from "reshow-constant";
/**
* @typedef {number|string} MapKeyType
*/
/**
* @template StateType
* @template ActionType
*
* @typedef {import("reshow-flux-base").StoreObject<StateType, ActionType>} StoreObject
*/
/**
* @typedef {import("reshow-flux-base").ActionObject} ActionObject
*/
/**
* Hack for defined this inside MaybeMapType will make null and undefined disapper
* @typedef {{[key: MapKeyType]:any}} MaybeMapObject
*/
/**
* @typedef {string|undefined|null|StateMap|MaybeMapObject} MaybeMapType
*/
/**
* @typedef {ActionObject|MaybeMapType} ImmutableAction
*/
/**
* @interface
*/
export var StateMap = /*#__PURE__*/_createClass(function StateMap() {
_classCallCheck(this, StateMap);
/** @type {function(MapKeyType[]):any}*/
_defineProperty(this, "getIn", void 0);
/** @type {function(MapKeyType):any}*/
_defineProperty(this, "get", void 0);
/** @type {function(MapKeyType, any):any}*/
_defineProperty(this, "set", void 0);
/** @type {function():any}*/
_defineProperty(this, "toJS", void 0);
/** @type {function():any}*/
_defineProperty(this, "clear", void 0);
});
/**
* @typedef {object} StoreObjectWithMap
* @property {function(MapKeyType):any} getMap
*/
/**
* @template [StateType=StateMap]
* @template [ActionType=MaybeMapType]
*
* @typedef {StoreObject<StateType, ActionType>&StoreObjectWithMap} ImmutableStoreObject
*/
/**
* @template [StateType=StateMap]
* @template [ActionType=MaybeMapType]
*
* @callback ReducerTypeWithMap
* @param {StateType} state
* @param {ActionType} action
* @returns {StateType}
*/
/**
* @callback forEachCb
* @param {any} Value
* @param {any} Key
* @returns {any}
*/
/**
* @param {any} state
* @param {MapKeyType} k
* @returns {object}
*/
var _getMap = function getMap(state, k) {
if (state && state.get) {
return toJS(state.get(k));
} else {
return {};
}
};
/**
* Why not just use immutable mergeMap?
* Because after merge can not use === to compare
* https://github.com/react-atomic/reshow/issues/123
*
* @template {StateMap} StateType
* @param {StateType} state
* @param {MaybeMapType} maybeMap
* @returns {StateType}
*/
var mergeMap = function mergeMap(state, maybeMap) {
if (STRING === typeof maybeMap) {
state = state.set("type", maybeMap);
} else {
try {
forEachMap(maybeMap,
/**
* @param {any} v
* @param {MapKeyType} k
*/
function (v, k) {
state = state.set(k, v);
});
} catch (e) {}
}
return state;
};
/**
* @template {StateMap} StateType
* @template {MaybeMapType} ActionType
* @type ReducerTypeWithMap<StateType, ActionType>
*/
var defaultReducer = function defaultReducer(state, action) {
return mergeMap(state, action);
};
/**
* @template [StateType=StateMap]
* @template [ActionType=MaybeMapType]
*
* @param {ReducerTypeWithMap<StateType, ActionType>?} [reducer]
* @param {import("reshow-flux-base").InitStateType<StateType>} [initState]
*
* @returns {[ImmutableStoreObject<StateType, ActionType>, dispatch]}
*/
export default function ImmutableStore(reducer, initState) {
var nextReducer = /**@type ReducerTypeWithMap<StateType, ActionType>*/
reducer || defaultReducer;
var stateMap = /**@type StateMap*/mergeMap(Map(), callfunc(initState));
var [store, dispatch] = createReducer(nextReducer, /**@type StateType*/stateMap);
/**
* @type ImmutableStoreObject<StateType, ActionType>
*/
var nextStore = _objectSpread(_objectSpread({}, store), {}, {
getMap: function getMap(/** @type MapKeyType */k) {
return _getMap(store.getState(), k);
}
});
return [nextStore, dispatch];
}
export { equal, fromJS, mergeMap, Map, Set };