@mapsight/lib-redux 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -0
- package/dist/README.md +20 -0
- package/dist/clone-action.d.ts +9 -0
- package/dist/clone-action.d.ts.map +1 -0
- package/dist/clone-action.js +30 -0
- package/dist/clone-action.js.map +1 -0
- package/dist/combine-sub-path-reducers.d.ts +13 -0
- package/dist/combine-sub-path-reducers.d.ts.map +1 -0
- package/dist/combine-sub-path-reducers.js +34 -0
- package/dist/combine-sub-path-reducers.js.map +1 -0
- package/dist/create-filtered-reducer-for-path.d.ts +13 -0
- package/dist/create-filtered-reducer-for-path.d.ts.map +1 -0
- package/dist/create-filtered-reducer-for-path.js +37 -0
- package/dist/create-filtered-reducer-for-path.js.map +1 -0
- package/dist/create-immutable-path-reducer.d.ts +13 -0
- package/dist/create-immutable-path-reducer.d.ts.map +1 -0
- package/dist/create-immutable-path-reducer.js +30 -0
- package/dist/create-immutable-path-reducer.js.map +1 -0
- package/dist/create-prefixed-async-action-middleware.d.ts +16 -0
- package/dist/create-prefixed-async-action-middleware.d.ts.map +1 -0
- package/dist/create-prefixed-async-action-middleware.js +25 -0
- package/dist/create-prefixed-async-action-middleware.js.map +1 -0
- package/dist/createSelectorUsingOwnProps.d.ts +23 -0
- package/dist/createSelectorUsingOwnProps.d.ts.map +1 -0
- package/dist/createSelectorUsingOwnProps.js +47 -0
- package/dist/createSelectorUsingOwnProps.js.map +1 -0
- package/dist/deep-change-state.d.ts +14 -0
- package/dist/deep-change-state.d.ts.map +1 -0
- package/dist/deep-change-state.js +29 -0
- package/dist/deep-change-state.js.map +1 -0
- package/dist/enable-async-dispatch.d.ts +13 -0
- package/dist/enable-async-dispatch.d.ts.map +1 -0
- package/dist/enable-async-dispatch.js +48 -0
- package/dist/enable-async-dispatch.js.map +1 -0
- package/dist/enable-controlled-dispatch-and-observe.d.ts +21 -0
- package/dist/enable-controlled-dispatch-and-observe.d.ts.map +1 -0
- package/dist/enable-controlled-dispatch-and-observe.js +59 -0
- package/dist/enable-controlled-dispatch-and-observe.js.map +1 -0
- package/dist/enable-initialization.d.ts +12 -0
- package/dist/enable-initialization.d.ts.map +1 -0
- package/dist/enable-initialization.js +25 -0
- package/dist/enable-initialization.js.map +1 -0
- package/dist/flatten-actions.d.ts +9 -0
- package/dist/flatten-actions.d.ts.map +1 -0
- package/dist/flatten-actions.js +18 -0
- package/dist/flatten-actions.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/local-storage.d.ts +15 -0
- package/dist/local-storage.d.ts.map +1 -0
- package/dist/local-storage.js +66 -0
- package/dist/local-storage.js.map +1 -0
- package/dist/matchPath.d.ts +15 -0
- package/dist/matchPath.d.ts.map +1 -0
- package/dist/matchPath.js +68 -0
- package/dist/matchPath.js.map +1 -0
- package/dist/matchesPath.d.ts +14 -0
- package/dist/matchesPath.d.ts.map +1 -0
- package/dist/matchesPath.js +19 -0
- package/dist/matchesPath.js.map +1 -0
- package/dist/observe-state.d.ts +8 -0
- package/dist/observe-state.d.ts.map +1 -0
- package/dist/observe-state.js +42 -0
- package/dist/observe-state.js.map +1 -0
- package/dist/package.json +52 -0
- package/dist/reducers/immutable/add-to.d.ts +4 -0
- package/dist/reducers/immutable/add-to.d.ts.map +1 -0
- package/dist/reducers/immutable/add-to.js +3 -0
- package/dist/reducers/immutable/add-to.js.map +1 -0
- package/dist/reducers/immutable/index.d.ts +4 -0
- package/dist/reducers/immutable/index.d.ts.map +1 -0
- package/dist/reducers/immutable/index.js +14 -0
- package/dist/reducers/immutable/index.js.map +1 -0
- package/dist/reducers/immutable/merge.d.ts +4 -0
- package/dist/reducers/immutable/merge.d.ts.map +1 -0
- package/dist/reducers/immutable/merge.js +9 -0
- package/dist/reducers/immutable/merge.js.map +1 -0
- package/dist/reducers/immutable/noop.d.ts +4 -0
- package/dist/reducers/immutable/noop.d.ts.map +1 -0
- package/dist/reducers/immutable/noop.js +3 -0
- package/dist/reducers/immutable/noop.js.map +1 -0
- package/dist/reducers/immutable/remove-from.d.ts +4 -0
- package/dist/reducers/immutable/remove-from.d.ts.map +1 -0
- package/dist/reducers/immutable/remove-from.js +3 -0
- package/dist/reducers/immutable/remove-from.js.map +1 -0
- package/dist/reducers/immutable/set.d.ts +4 -0
- package/dist/reducers/immutable/set.d.ts.map +1 -0
- package/dist/reducers/immutable/set.js +3 -0
- package/dist/reducers/immutable/set.js.map +1 -0
- package/dist/reducers/immutable-path/add-to.d.ts +3 -0
- package/dist/reducers/immutable-path/add-to.d.ts.map +1 -0
- package/dist/reducers/immutable-path/add-to.js +4 -0
- package/dist/reducers/immutable-path/add-to.js.map +1 -0
- package/dist/reducers/immutable-path/index.d.ts +11 -0
- package/dist/reducers/immutable-path/index.d.ts.map +1 -0
- package/dist/reducers/immutable-path/index.js +14 -0
- package/dist/reducers/immutable-path/index.js.map +1 -0
- package/dist/reducers/immutable-path/merge.d.ts +3 -0
- package/dist/reducers/immutable-path/merge.d.ts.map +1 -0
- package/dist/reducers/immutable-path/merge.js +4 -0
- package/dist/reducers/immutable-path/merge.js.map +1 -0
- package/dist/reducers/immutable-path/noop.d.ts +2 -0
- package/dist/reducers/immutable-path/noop.d.ts.map +1 -0
- package/dist/reducers/immutable-path/noop.js +4 -0
- package/dist/reducers/immutable-path/noop.js.map +1 -0
- package/dist/reducers/immutable-path/remove-from.d.ts +3 -0
- package/dist/reducers/immutable-path/remove-from.d.ts.map +1 -0
- package/dist/reducers/immutable-path/remove-from.js +4 -0
- package/dist/reducers/immutable-path/remove-from.js.map +1 -0
- package/dist/reducers/immutable-path/set.d.ts +3 -0
- package/dist/reducers/immutable-path/set.d.ts.map +1 -0
- package/dist/reducers/immutable-path/set.js +4 -0
- package/dist/reducers/immutable-path/set.js.map +1 -0
- package/dist/reducers/reduce-by-keys.d.ts +2 -0
- package/dist/reducers/reduce-by-keys.d.ts.map +1 -0
- package/dist/reducers/reduce-by-keys.js +14 -0
- package/dist/reducers/reduce-by-keys.js.map +1 -0
- package/package.json +51 -0
- package/src/js/clone-action.ts +36 -0
- package/src/js/combine-sub-path-reducers.ts +44 -0
- package/src/js/create-filtered-reducer-for-path.ts +52 -0
- package/src/js/create-immutable-path-reducer.ts +41 -0
- package/src/js/create-prefixed-async-action-middleware.ts +47 -0
- package/src/js/createSelectorUsingOwnProps.ts +84 -0
- package/src/js/deep-change-state.ts +37 -0
- package/src/js/enable-async-dispatch.ts +61 -0
- package/src/js/enable-controlled-dispatch-and-observe.ts +128 -0
- package/src/js/enable-initialization.ts +36 -0
- package/src/js/flatten-actions.ts +22 -0
- package/src/js/index.ts +1 -0
- package/src/js/local-storage.ts +96 -0
- package/src/js/matchPath.ts +78 -0
- package/src/js/matchesPath.ts +23 -0
- package/src/js/observe-state.ts +109 -0
- package/src/js/reducers/immutable/add-to.ts +6 -0
- package/src/js/reducers/immutable/index.ts +17 -0
- package/src/js/reducers/immutable/merge.ts +18 -0
- package/src/js/reducers/immutable/noop.ts +5 -0
- package/src/js/reducers/immutable/remove-from.ts +8 -0
- package/src/js/reducers/immutable/set.ts +5 -0
- package/src/js/reducers/immutable-path/add-to.ts +4 -0
- package/src/js/reducers/immutable-path/index.ts +17 -0
- package/src/js/reducers/immutable-path/merge.ts +4 -0
- package/src/js/reducers/immutable-path/noop.ts +3 -0
- package/src/js/reducers/immutable-path/remove-from.ts +4 -0
- package/src/js/reducers/immutable-path/set.ts +4 -0
- package/src/js/reducers/reduce-by-keys.ts +19 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import addTo from "./add-to";
|
|
2
|
+
import merge from "./merge";
|
|
3
|
+
import noop from "./noop";
|
|
4
|
+
import removeFrom from "./remove-from";
|
|
5
|
+
import set from "./set";
|
|
6
|
+
const reducers = {
|
|
7
|
+
addTo: addTo,
|
|
8
|
+
merge: merge,
|
|
9
|
+
noop: noop,
|
|
10
|
+
removeFrom: removeFrom,
|
|
11
|
+
set: set,
|
|
12
|
+
};
|
|
13
|
+
export default reducers;
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/js/reducers/immutable-path/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,MAAM,UAAU,CAAC;AAC7B,OAAO,KAAK,MAAM,SAAS,CAAC;AAC5B,OAAO,IAAI,MAAM,QAAQ,CAAC;AAC1B,OAAO,UAAU,MAAM,eAAe,CAAC;AACvC,OAAO,GAAG,MAAM,OAAO,CAAC;AAExB,MAAM,QAAQ,GAAG;IAChB,KAAK,EAAE,KAAK;IACZ,KAAK,EAAE,KAAK;IACZ,IAAI,EAAE,IAAI;IACV,UAAU,EAAE,UAAU;IACtB,GAAG,EAAE,GAAG;CAC0B,CAAC;AAEpC,eAAe,QAAQ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merge.d.ts","sourceRoot":"","sources":["../../../src/js/reducers/immutable-path/merge.ts"],"names":[],"mappings":";AAGA,wBAAiD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merge.js","sourceRoot":"","sources":["../../../src/js/reducers/immutable-path/merge.ts"],"names":[],"mappings":"AAAA,OAAO,0BAA0B,MAAM,qCAAqC,CAAC;AAC7E,OAAO,KAAK,MAAM,oBAAoB,CAAC;AAEvC,eAAe,0BAA0B,CAAC,KAAK,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"noop.d.ts","sourceRoot":"","sources":["../../../src/js/reducers/immutable-path/noop.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,UAAU,WAAW,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,KAE9C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"noop.js","sourceRoot":"","sources":["../../../src/js/reducers/immutable-path/noop.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,UAAU,WAAW,CAAI,KAAQ;IAC9C,OAAO,KAAK,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remove-from.d.ts","sourceRoot":"","sources":["../../../src/js/reducers/immutable-path/remove-from.ts"],"names":[],"mappings":";AAGA,wBAAsD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remove-from.js","sourceRoot":"","sources":["../../../src/js/reducers/immutable-path/remove-from.ts"],"names":[],"mappings":"AAAA,OAAO,0BAA0B,MAAM,qCAAqC,CAAC;AAC7E,OAAO,UAAU,MAAM,0BAA0B,CAAC;AAElD,eAAe,0BAA0B,CAAC,UAAU,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"set.d.ts","sourceRoot":"","sources":["../../../src/js/reducers/immutable-path/set.ts"],"names":[],"mappings":";AAGA,wBAA+C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"set.js","sourceRoot":"","sources":["../../../src/js/reducers/immutable-path/set.ts"],"names":[],"mappings":"AAAA,OAAO,0BAA0B,MAAM,qCAAqC,CAAC;AAC7E,OAAO,GAAG,MAAM,kBAAkB,CAAC;AAEnC,eAAe,0BAA0B,CAAC,GAAG,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reduce-by-keys.d.ts","sourceRoot":"","sources":["../../src/js/reducers/reduce-by-keys.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,OAAO,UAAU,YAAY,CACnC,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,EACnB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,MAc9B"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import unique from "lodash/uniq";
|
|
2
|
+
export default function reduceByKeys(keys, state) {
|
|
3
|
+
if (keys && state) {
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
5
|
+
const uniqueKeys = unique(keys);
|
|
6
|
+
return uniqueKeys.reduce(function reduceByKey(newState, key) {
|
|
7
|
+
return state[key] !== undefined
|
|
8
|
+
? { ...newState, [key]: state[key] }
|
|
9
|
+
: newState;
|
|
10
|
+
}, {});
|
|
11
|
+
}
|
|
12
|
+
return state;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=reduce-by-keys.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reduce-by-keys.js","sourceRoot":"","sources":["../../src/js/reducers/reduce-by-keys.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAEjC,MAAM,CAAC,OAAO,UAAU,YAAY,CACnC,IAAmB,EACnB,KAA8B;IAE9B,IAAI,IAAI,IAAI,KAAK,EAAE,CAAC;QACnB,6DAA6D;QAC7D,MAAM,UAAU,GAAkB,MAAM,CAAC,IAAI,CAAC,CAAC;QAE/C,OAAO,UAAU,CAAC,MAAM,CAAC,SAAS,WAAW,CAAC,QAAQ,EAAE,GAAG;YAC1D,OAAO,KAAK,CAAC,GAAG,CAAC,KAAK,SAAS;gBAC9B,CAAC,CAAC,EAAC,GAAG,QAAQ,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,EAAC;gBAClC,CAAC,CAAC,QAAQ,CAAC;QACb,CAAC,EAAE,EAAE,CAAC,CAAC;IACR,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mapsight/lib-redux",
|
|
3
|
+
"description": "Mapsight Redux Library",
|
|
4
|
+
"version": "2.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"lodash": "^4.18.1",
|
|
8
|
+
"redux-thunk": "^2.4.2",
|
|
9
|
+
"@mapsight/lib-js": "^3.0.0"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@types/lodash": "^4.17.24",
|
|
13
|
+
"cpx2": "^8.0.2",
|
|
14
|
+
"redux": "^4.2.1"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./*": {
|
|
22
|
+
"types": "./dist/*.d.ts",
|
|
23
|
+
"import": "./dist/*.js"
|
|
24
|
+
},
|
|
25
|
+
"./reducers/immutable": {
|
|
26
|
+
"types": "./dist/reducers/immutable/index.d.ts",
|
|
27
|
+
"import": "./dist/reducers/immutable/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./reducers/immutable-path": {
|
|
30
|
+
"types": "./dist/reducers/immutable-path/index.d.ts",
|
|
31
|
+
"import": "./dist/reducers/immutable-path/index.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"license": "UNLICENSED",
|
|
35
|
+
"main": "index.js",
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"redux": "^4.2.1"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "npm-run-all build:setup build:modules build:copy",
|
|
41
|
+
"build:copy": "cpx '{package.json,README.md}' dist/",
|
|
42
|
+
"build:modules": "tsc",
|
|
43
|
+
"build:setup": "mkdirp dist",
|
|
44
|
+
"clean": "rimraf dist/*",
|
|
45
|
+
"clean-build": "run-s clean build",
|
|
46
|
+
"typecheck": "tsc --noEmit",
|
|
47
|
+
"watch": "run-p watch:*",
|
|
48
|
+
"watch:copy": "pnpm run build:copy --watch",
|
|
49
|
+
"watch:modules": "pnpm run build:modules --watch"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {AnyAction} from "redux";
|
|
2
|
+
|
|
3
|
+
function shallowCloneObject<T>(o: T): T {
|
|
4
|
+
return {...o};
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new action (object or function) with the same content but new reference.
|
|
9
|
+
*
|
|
10
|
+
* @param action action
|
|
11
|
+
* @returns cloned action
|
|
12
|
+
*/
|
|
13
|
+
export default function cloneAction<T = AnyAction>(action: T): T {
|
|
14
|
+
// special case for redux-thunk where the action is a function
|
|
15
|
+
if (typeof action === "function") {
|
|
16
|
+
const newAction: T = action.bind({}) as T;
|
|
17
|
+
|
|
18
|
+
if ("meta" in action) {
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
22
|
+
newAction.meta = shallowCloneObject(action.meta);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (action.name) {
|
|
26
|
+
Object.defineProperty(newAction, "name", {
|
|
27
|
+
value: action.name + "__cloned",
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return newAction;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// otherwise just make a shallow copy of the plan object action
|
|
35
|
+
return shallowCloneObject(action);
|
|
36
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {AnyAction, Reducer, combineReducers} from "redux";
|
|
2
|
+
|
|
3
|
+
import cloneAction from "./clone-action";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Combines the given reducers, so they only get actions without a path or
|
|
7
|
+
* a path matching the map key.
|
|
8
|
+
*
|
|
9
|
+
* @param reducerMap a map of reducers and their keys representing their part of the store
|
|
10
|
+
* @param pathKey key of the action parameter that contains the path array
|
|
11
|
+
* @returns combined reducer
|
|
12
|
+
*/
|
|
13
|
+
export default function combineSubPathReducers(
|
|
14
|
+
reducerMap: Record<string, Reducer>,
|
|
15
|
+
pathKey = "path",
|
|
16
|
+
) {
|
|
17
|
+
const reducerEntries = Object.entries(reducerMap).map(([name, reducer]) => {
|
|
18
|
+
const wrapperReducer = <T>(state: T, action: AnyAction): any => {
|
|
19
|
+
if (typeof action.meta === "object") {
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
|
|
21
|
+
const path = action.meta[pathKey];
|
|
22
|
+
|
|
23
|
+
if (Array.isArray(path) && path.length) {
|
|
24
|
+
if (path[0] !== name) {
|
|
25
|
+
return state;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const controllerAction = cloneAction(action);
|
|
29
|
+
controllerAction[pathKey] = path.slice(1);
|
|
30
|
+
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
32
|
+
return reducer(state, controllerAction);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
37
|
+
return reducer(state, action);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
return [name, wrapperReducer] as const;
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return combineReducers(Object.fromEntries(reducerEntries));
|
|
44
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import {AnyAction, Reducer} from "redux";
|
|
2
|
+
|
|
3
|
+
import cloneAction from "./clone-action";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Filters the actions, so the reducer only gets actions without a path or
|
|
7
|
+
* a path matching the given key.
|
|
8
|
+
*
|
|
9
|
+
* @param reducer the base reducer that should get filtered actions only
|
|
10
|
+
* @param key key of the action parameter that contains the path array
|
|
11
|
+
* @param [statePathMetaKey] name of the meta field that contains the path array (action.meta.path), default: 'path'
|
|
12
|
+
* @returns resulting reducer
|
|
13
|
+
*/
|
|
14
|
+
function createFilteredReducerForPath(
|
|
15
|
+
reducer: Reducer,
|
|
16
|
+
key: string,
|
|
17
|
+
statePathMetaKey = "path",
|
|
18
|
+
): Reducer {
|
|
19
|
+
const filteredReducer: Reducer = <T extends Record<string, unknown>>(
|
|
20
|
+
state: T,
|
|
21
|
+
action: AnyAction,
|
|
22
|
+
) => {
|
|
23
|
+
if (typeof action.meta === "object") {
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
|
|
25
|
+
const path = action.meta[statePathMetaKey];
|
|
26
|
+
if (Array.isArray(path) && path.length) {
|
|
27
|
+
if (path[0] !== key) {
|
|
28
|
+
return state;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const slicedAction = cloneAction(action);
|
|
32
|
+
slicedAction[statePathMetaKey] = path.slice(1);
|
|
33
|
+
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
35
|
+
return reducer(state, slicedAction);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
40
|
+
return reducer(state, action);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
if (reducer.name) {
|
|
44
|
+
Object.defineProperty(filteredReducer, "name", {
|
|
45
|
+
value: reducer.name + "__filtered-by-path",
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return filteredReducer;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default createFilteredReducerForPath;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import get from "lodash/get";
|
|
2
|
+
import {AnyAction, Reducer} from "redux";
|
|
3
|
+
|
|
4
|
+
import deepChangeState from "./deep-change-state";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Creates a new reducer, that replaces an value at the given path using the given reducer,
|
|
8
|
+
* replacing all parent objects, instead of mutating the existing object(s).
|
|
9
|
+
*
|
|
10
|
+
* The given reducer has to work immutable as well to get the full desired effect!
|
|
11
|
+
*
|
|
12
|
+
* @param reducer reducer to reduce the value at the path
|
|
13
|
+
* @param [pathKey] path in the object to reduce, default: 'path'
|
|
14
|
+
* @returns resulting immutable path reducer
|
|
15
|
+
*/
|
|
16
|
+
export default function createImmutablePathReducer(
|
|
17
|
+
reducer: Reducer,
|
|
18
|
+
pathKey = "path",
|
|
19
|
+
): Reducer {
|
|
20
|
+
// noinspection UnnecessaryLocalVariableJS
|
|
21
|
+
const immutablePathReducer: Reducer = <T>(
|
|
22
|
+
state: T,
|
|
23
|
+
action: AnyAction,
|
|
24
|
+
): T => {
|
|
25
|
+
const path = action[pathKey] as Array<string>;
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
27
|
+
const oldValue = (path?.length ? get(state, path) : state) as T;
|
|
28
|
+
const newValue = reducer(oldValue, action) as T;
|
|
29
|
+
if (newValue === oldValue) {
|
|
30
|
+
return state;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return deepChangeState(state, path, newValue);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
//if (reducer.name) {
|
|
37
|
+
// Object.defineProperty(aImmutablePathReducer, 'name', {value: reducer.name + '__immutable-path'});
|
|
38
|
+
//}
|
|
39
|
+
|
|
40
|
+
return immutablePathReducer;
|
|
41
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a redux middleware that allows dispatching of functions as actions, when the action
|
|
3
|
+
* exposes the defined actionFlag. If a prefix is provided, the action function will receive a
|
|
4
|
+
* prefixed getState, meaning it will retrieve the sub store with the prefix key (store.getState()[prefix]).
|
|
5
|
+
*
|
|
6
|
+
* Works similar to thunk, but with additional flag and optional sub store getState.
|
|
7
|
+
*
|
|
8
|
+
* @param [actionFlag] action flag, default: 'isAsync'
|
|
9
|
+
* @param [prefix] prefix state prefix
|
|
10
|
+
* @param [extraArgument] extra argument to be passed to action function
|
|
11
|
+
*
|
|
12
|
+
* @returns {function(*=): function(*): Function} resulting middleware
|
|
13
|
+
*/
|
|
14
|
+
import {Middleware, MiddlewareAPI} from "redux";
|
|
15
|
+
|
|
16
|
+
export default function createPrefixedAsyncActionMiddleware<
|
|
17
|
+
Prefix extends string | undefined,
|
|
18
|
+
>(
|
|
19
|
+
actionFlag = "isAsync",
|
|
20
|
+
prefix?: Prefix,
|
|
21
|
+
extraArgument: unknown = undefined,
|
|
22
|
+
): Middleware {
|
|
23
|
+
return function prefixedAsyncActionMiddleware(api: MiddlewareAPI) {
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
25
|
+
let getState = api.getState;
|
|
26
|
+
if (typeof prefix === "string") {
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
28
|
+
const baseGetState = api.getState as () => Record<string, unknown>;
|
|
29
|
+
getState = function getStateWithPrefix() {
|
|
30
|
+
return baseGetState()[prefix];
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return function (next) {
|
|
35
|
+
return function prefixedAsyncActionMiddlewareWithAction(action) {
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
37
|
+
if (typeof action === "function" && action.meta?.[actionFlag]) {
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-return
|
|
39
|
+
return action(api.dispatch, getState, extraArgument);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
43
|
+
return next(action);
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
function strictEqualityComparison<T>(a: T, b: T): boolean {
|
|
2
|
+
return a === b;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export type SelectorUsingPropsOptions = {
|
|
6
|
+
equals?: typeof strictEqualityComparison;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
type OwnPropsSelector<OwnProps, T = unknown> = (ownProps: OwnProps) => T;
|
|
10
|
+
|
|
11
|
+
type SelectorWithOwnProps<
|
|
12
|
+
OwnProps extends Array<unknown> = Array<unknown>,
|
|
13
|
+
State = unknown,
|
|
14
|
+
Value = unknown,
|
|
15
|
+
> = (state: State, ownProps: OwnProps) => Value;
|
|
16
|
+
type SelectorFactory<
|
|
17
|
+
OwnProps extends Array<unknown> = Array<unknown>,
|
|
18
|
+
State = unknown,
|
|
19
|
+
Value = unknown,
|
|
20
|
+
> = (
|
|
21
|
+
...selectedValues: unknown[]
|
|
22
|
+
) => SelectorWithOwnProps<OwnProps, State, Value>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Create a selector whenever some (own) properties change. To be used with react-redux's connect() mapState.
|
|
26
|
+
*
|
|
27
|
+
* @param ownPropsSelectorOrSelectors one selector function or an array of selector functions that receive
|
|
28
|
+
* the ownProps and should return one property value that will be used to determine changes
|
|
29
|
+
* @param selectorFactory factory function receives the selected props as arguments followed by the state and
|
|
30
|
+
* ownProps at the time of invalidation
|
|
31
|
+
* @param [options] optional options
|
|
32
|
+
* @param [options.equals] function that check equality, defaults to strict equality (===)
|
|
33
|
+
* @returns selector function to be used as mapState in react-redux's connect() function.
|
|
34
|
+
* May be combined with createSelector/createStructuredSelector or any other function as
|
|
35
|
+
* long as the selector is called with state as first and own props as second argument.
|
|
36
|
+
*/
|
|
37
|
+
export default function createSelectorUsingOwnProps<
|
|
38
|
+
State = unknown,
|
|
39
|
+
Value = unknown,
|
|
40
|
+
OwnProps extends Array<unknown> = Array<unknown>,
|
|
41
|
+
>(
|
|
42
|
+
ownPropsSelectorOrSelectors:
|
|
43
|
+
| OwnPropsSelector<OwnProps>
|
|
44
|
+
| Array<OwnPropsSelector<OwnProps>>,
|
|
45
|
+
selectorFactory: SelectorFactory<OwnProps, State, Value>,
|
|
46
|
+
options: SelectorUsingPropsOptions = {},
|
|
47
|
+
) {
|
|
48
|
+
const equals = options.equals || strictEqualityComparison;
|
|
49
|
+
let propsCache: Array<unknown>;
|
|
50
|
+
let cachedSelector: SelectorWithOwnProps<OwnProps, State, Value>;
|
|
51
|
+
|
|
52
|
+
const ownPropsSelectors = Array.isArray(ownPropsSelectorOrSelectors)
|
|
53
|
+
? ownPropsSelectorOrSelectors
|
|
54
|
+
: [ownPropsSelectorOrSelectors];
|
|
55
|
+
|
|
56
|
+
function updateSelector(state: State, ownProps: OwnProps) {
|
|
57
|
+
let hasChanged = false;
|
|
58
|
+
|
|
59
|
+
if (!propsCache) {
|
|
60
|
+
propsCache = new Array(ownPropsSelectorOrSelectors.length);
|
|
61
|
+
hasChanged = true;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
ownPropsSelectors.forEach(function updateProps(ownPropsSelector, i) {
|
|
65
|
+
const prev = propsCache[i];
|
|
66
|
+
const next = ownPropsSelector(ownProps);
|
|
67
|
+
|
|
68
|
+
if (!hasChanged && !equals(prev, next)) {
|
|
69
|
+
hasChanged = true;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
propsCache[i] = next;
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
if (hasChanged || !cachedSelector) {
|
|
76
|
+
cachedSelector = selectorFactory(...propsCache, state, ownProps);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return function selector(state: State, ownProps: OwnProps): Value {
|
|
81
|
+
updateSelector(state, ownProps);
|
|
82
|
+
return cachedSelector(state, ownProps);
|
|
83
|
+
};
|
|
84
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import setWith from "lodash/setWith";
|
|
2
|
+
|
|
3
|
+
function shallowCloneObject<T>(o: T): T {
|
|
4
|
+
return {...o};
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new state from {state} with the value at the {path} changed to the given {value} and
|
|
9
|
+
* reassigns all objects down along the path of the change so they aren't strictly equal anymore
|
|
10
|
+
* and can be easily observed as changed by redux/react.
|
|
11
|
+
*
|
|
12
|
+
* **Note:** Returns a new state object! Does NOT mutate the original state object!
|
|
13
|
+
*
|
|
14
|
+
* @param {object} state The original state
|
|
15
|
+
* @param {*[]|string} [path] The path at which the change gets applied.
|
|
16
|
+
* @param {*} [value] The new value to be set (or undefined).
|
|
17
|
+
* @returns {*} The new state with changed value and new objects along the path.
|
|
18
|
+
*/
|
|
19
|
+
export default function deepChangeState<State = unknown, Value = unknown>(
|
|
20
|
+
state: State,
|
|
21
|
+
path: Array<string>,
|
|
22
|
+
value: Value,
|
|
23
|
+
) {
|
|
24
|
+
const hasPath = path?.length;
|
|
25
|
+
if (!hasPath) {
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const newState = shallowCloneObject(state);
|
|
30
|
+
|
|
31
|
+
if (newState && typeof newState === "object") {
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
33
|
+
setWith(newState, path, value, shallowCloneObject);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return newState;
|
|
37
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import {AnyAction, Store} from "redux";
|
|
2
|
+
|
|
3
|
+
function runAsync(fn: () => void) {
|
|
4
|
+
return setTimeout(() => fn(), 10);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function isRecord(val: unknown): val is Record<keyof any, unknown> {
|
|
8
|
+
return typeof val === "object" && val !== null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Enhances the stores dispatch function, so you can dispatch actions flagged as async.
|
|
13
|
+
* This allows "dispatching while dispatching" by queueing actions that are dispatched while dispatching is already
|
|
14
|
+
* in progress and dispatching them after finishing with the current action.
|
|
15
|
+
*
|
|
16
|
+
* The action is guaranteed to be dispatched asynchronously in any case (even when dispatching is possible immediately).
|
|
17
|
+
*
|
|
18
|
+
* @param store redux store to be enhanced (having dispatch)
|
|
19
|
+
* @param asyncActionFlag flag that async actions expose (action[actionFlag] == true)
|
|
20
|
+
*/
|
|
21
|
+
export default function enableAsyncDispatch(
|
|
22
|
+
store: Store,
|
|
23
|
+
asyncActionFlag: string | symbol = "isAsync",
|
|
24
|
+
) {
|
|
25
|
+
// This function will override the store.dispatch method, deferring the actual dispatch of actions flagged as
|
|
26
|
+
// async by queueing them and only dispatching them in the next microtask by scheduling them with setTimeout.
|
|
27
|
+
let isAsyncScheduled = false;
|
|
28
|
+
const actionQueue: Array<AnyAction> = [];
|
|
29
|
+
|
|
30
|
+
const baseDispatch = store.dispatch;
|
|
31
|
+
|
|
32
|
+
function isActionAsync(action: AnyAction) {
|
|
33
|
+
return isRecord(action.meta) && !!action.meta[asyncActionFlag];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function runScheduleAsyncDispatch() {
|
|
37
|
+
isAsyncScheduled = false;
|
|
38
|
+
|
|
39
|
+
while (actionQueue.length) {
|
|
40
|
+
const action = actionQueue.shift() as AnyAction;
|
|
41
|
+
baseDispatch(action);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function scheduleAsyncDispatch() {
|
|
46
|
+
if (!isAsyncScheduled) {
|
|
47
|
+
isAsyncScheduled = true;
|
|
48
|
+
runAsync(runScheduleAsyncDispatch);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
store.dispatch = (action) => {
|
|
53
|
+
if (isActionAsync(action)) {
|
|
54
|
+
actionQueue.push(action);
|
|
55
|
+
scheduleAsyncDispatch();
|
|
56
|
+
return action;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return baseDispatch(action);
|
|
60
|
+
};
|
|
61
|
+
}
|