@mapsight/lib-redux 2.0.3 → 2.2.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 +16 -13
- package/dist/README.md +16 -13
- package/dist/clone-action.d.ts +1 -1
- package/dist/clone-action.d.ts.map +1 -1
- package/dist/combine-sub-path-reducers.d.ts +6 -4
- package/dist/combine-sub-path-reducers.d.ts.map +1 -1
- package/dist/combine-sub-path-reducers.js +1 -1
- package/dist/combine-sub-path-reducers.js.map +1 -1
- package/dist/create-filtered-reducer-for-path.d.ts +1 -1
- package/dist/create-filtered-reducer-for-path.d.ts.map +1 -1
- package/dist/create-immutable-path-reducer.d.ts +1 -1
- package/dist/create-immutable-path-reducer.d.ts.map +1 -1
- package/dist/create-immutable-path-reducer.js.map +1 -1
- package/dist/create-prefixed-async-action-middleware.d.ts +1 -1
- package/dist/create-prefixed-async-action-middleware.d.ts.map +1 -1
- package/dist/create-prefixed-async-action-middleware.js +17 -3
- package/dist/create-prefixed-async-action-middleware.js.map +1 -1
- package/dist/createSelectorUsingOwnProps.d.ts +1 -1
- package/dist/createSelectorUsingOwnProps.js +1 -1
- package/dist/enable-async-dispatch.d.ts +1 -1
- package/dist/enable-async-dispatch.d.ts.map +1 -1
- package/dist/enable-controlled-dispatch-and-observe.d.ts +1 -1
- package/dist/enable-controlled-dispatch-and-observe.d.ts.map +1 -1
- package/dist/enable-initialization.d.ts +1 -1
- package/dist/enable-initialization.d.ts.map +1 -1
- package/dist/flatten-actions.d.ts +1 -1
- package/dist/flatten-actions.d.ts.map +1 -1
- package/dist/local-storage.d.ts +1 -1
- package/dist/local-storage.d.ts.map +1 -1
- package/dist/local-storage.js.map +1 -1
- package/dist/observe-state.d.ts +4 -4
- package/dist/observe-state.d.ts.map +1 -1
- package/dist/package.json +10 -12
- package/dist/reducers/immutable/add-to.d.ts +1 -1
- package/dist/reducers/immutable/add-to.d.ts.map +1 -1
- package/dist/reducers/immutable/index.d.ts +1 -1
- package/dist/reducers/immutable/index.d.ts.map +1 -1
- package/dist/reducers/immutable/merge.d.ts +1 -1
- package/dist/reducers/immutable/merge.d.ts.map +1 -1
- package/dist/reducers/immutable/merge.js.map +1 -1
- package/dist/reducers/immutable/noop.d.ts +1 -1
- package/dist/reducers/immutable/noop.d.ts.map +1 -1
- package/dist/reducers/immutable/remove-from.d.ts +1 -1
- package/dist/reducers/immutable/remove-from.d.ts.map +1 -1
- package/dist/reducers/immutable/set.d.ts +1 -1
- package/dist/reducers/immutable/set.d.ts.map +1 -1
- package/dist/reducers/immutable-path/add-to.d.ts +1 -1
- package/dist/reducers/immutable-path/index.d.ts +1 -1
- package/dist/reducers/immutable-path/index.d.ts.map +1 -1
- package/dist/reducers/immutable-path/merge.d.ts +1 -1
- package/dist/reducers/immutable-path/remove-from.d.ts +1 -1
- package/dist/reducers/immutable-path/set.d.ts +1 -1
- package/package.json +10 -12
- package/src/js/clone-action.ts +0 -36
- package/src/js/combine-sub-path-reducers.ts +0 -44
- package/src/js/create-filtered-reducer-for-path.ts +0 -52
- package/src/js/create-immutable-path-reducer.ts +0 -41
- package/src/js/create-prefixed-async-action-middleware.ts +0 -47
- package/src/js/createSelectorUsingOwnProps.ts +0 -84
- package/src/js/deep-change-state.ts +0 -37
- package/src/js/enable-async-dispatch.ts +0 -61
- package/src/js/enable-controlled-dispatch-and-observe.ts +0 -128
- package/src/js/enable-initialization.ts +0 -36
- package/src/js/flatten-actions.ts +0 -22
- package/src/js/index.ts +0 -1
- package/src/js/local-storage.ts +0 -96
- package/src/js/matchPath.ts +0 -78
- package/src/js/matchesPath.ts +0 -23
- package/src/js/observe-state.ts +0 -109
- package/src/js/reducers/immutable/add-to.ts +0 -6
- package/src/js/reducers/immutable/index.ts +0 -17
- package/src/js/reducers/immutable/merge.ts +0 -18
- package/src/js/reducers/immutable/noop.ts +0 -5
- package/src/js/reducers/immutable/remove-from.ts +0 -8
- package/src/js/reducers/immutable/set.ts +0 -5
- package/src/js/reducers/immutable-path/add-to.ts +0 -4
- package/src/js/reducers/immutable-path/index.ts +0 -17
- package/src/js/reducers/immutable-path/merge.ts +0 -4
- package/src/js/reducers/immutable-path/noop.ts +0 -3
- package/src/js/reducers/immutable-path/remove-from.ts +0 -4
- package/src/js/reducers/immutable-path/set.ts +0 -4
- package/src/js/reducers/reduce-by-keys.ts +0 -19
package/src/js/matchPath.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import {zip} from "@mapsight/lib-js/array";
|
|
2
|
-
|
|
3
|
-
type PatternSegment = {isParam: boolean; name: string};
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Parses a pattern segment.
|
|
7
|
-
*
|
|
8
|
-
* @param patternSegment segment string
|
|
9
|
-
* @returns parsed segment
|
|
10
|
-
*/
|
|
11
|
-
function parsePatternSegment(patternSegment: string): PatternSegment {
|
|
12
|
-
if (patternSegment.startsWith(":")) {
|
|
13
|
-
return {
|
|
14
|
-
isParam: true,
|
|
15
|
-
name: patternSegment.slice(1),
|
|
16
|
-
};
|
|
17
|
-
} else {
|
|
18
|
-
return {
|
|
19
|
-
isParam: false,
|
|
20
|
-
name: patternSegment,
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// TODO: lru/limit cache?
|
|
26
|
-
const parsePathPatternCache: Record<string, Array<PatternSegment>> = {};
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Parses a path pattern
|
|
30
|
-
*
|
|
31
|
-
* @param pattern path pattern
|
|
32
|
-
* @returns parsed pattern
|
|
33
|
-
*/
|
|
34
|
-
function parsePathPattern(pattern: string): Array<PatternSegment> {
|
|
35
|
-
const fromCache = parsePathPatternCache[pattern];
|
|
36
|
-
if (fromCache !== undefined) {
|
|
37
|
-
return fromCache;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const patternSegments = pattern.split("/").map(parsePatternSegment);
|
|
41
|
-
parsePathPatternCache[pattern] = patternSegments;
|
|
42
|
-
return patternSegments;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* This function matches the given path array to a path pattern. The pattern consists of / delimited names and/or
|
|
47
|
-
* named parameters defined as `:name` where name may be any string which will be used in the returned object to
|
|
48
|
-
* access the parameter value.
|
|
49
|
-
*
|
|
50
|
-
* If the path matches the pattern, the return value will be an object containing any present
|
|
51
|
-
* named parameter values that may be in the pattern, otherwise it will return false if any path segment does not
|
|
52
|
-
* match the specified pattern.
|
|
53
|
-
*
|
|
54
|
-
* @param pathArr path array
|
|
55
|
-
* @param pattern pattern to match
|
|
56
|
-
* @returns object containing matched named parameter values or false if the path did not patch the pattern
|
|
57
|
-
*/
|
|
58
|
-
export default function matchPath(
|
|
59
|
-
pathArr: Array<string>,
|
|
60
|
-
pattern: string,
|
|
61
|
-
): false | Record<string, string> {
|
|
62
|
-
const patternSegments = parsePathPattern(pattern);
|
|
63
|
-
if (patternSegments.length !== pathArr.length) {
|
|
64
|
-
return false;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const params: Record<string, string> = {};
|
|
68
|
-
|
|
69
|
-
for (const [pathValue, patternSegment] of zip(pathArr, patternSegments)) {
|
|
70
|
-
if (patternSegment.isParam) {
|
|
71
|
-
params[patternSegment.name] = pathValue;
|
|
72
|
-
} else if (pathValue !== patternSegment.name) {
|
|
73
|
-
return false;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return params;
|
|
78
|
-
}
|
package/src/js/matchesPath.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import matchPath from "./matchPath";
|
|
2
|
-
|
|
3
|
-
const noMatch: Record<string, string> = {};
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* This function matches the given path array to a path pattern. The pattern consists of / delimited names and/or
|
|
7
|
-
* named parameters defined as `:name` where name may be any string which will be used in the returned object to
|
|
8
|
-
* access the parameter value.
|
|
9
|
-
*
|
|
10
|
-
* If the path matches the pattern it will return an object containing any present
|
|
11
|
-
* named parameter values that may be in the pattern otherwise an empty object is returned.
|
|
12
|
-
*
|
|
13
|
-
* @param pathArr path array
|
|
14
|
-
* @param pattern pattern to match
|
|
15
|
-
* @returns 2-ary array with boolean that is true if the path did match the pattern and an object containing matched named parameter values
|
|
16
|
-
*/
|
|
17
|
-
export default function matchesPath(
|
|
18
|
-
pathArr: Array<string>,
|
|
19
|
-
pattern: string,
|
|
20
|
-
): [boolean, Record<string, string>] {
|
|
21
|
-
const match = matchPath(pathArr, pattern);
|
|
22
|
-
return match ? [true, match] : [false, noMatch];
|
|
23
|
-
}
|
package/src/js/observe-state.ts
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import {Store} from "redux";
|
|
2
|
-
|
|
3
|
-
function strictEqualCompare<T>(a: T, b: T): boolean {
|
|
4
|
-
return a === b;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
//const jsonCompare = (a, b) => JSON.stringify(a) !== JSON.stringify(b);
|
|
8
|
-
|
|
9
|
-
export const AbortObserving = Symbol(); // TODO: Use Symbol()
|
|
10
|
-
|
|
11
|
-
function internalObserveState<State = unknown, Value = unknown>(
|
|
12
|
-
store: Store<State>,
|
|
13
|
-
selector: (state: State) => Value,
|
|
14
|
-
listener: (
|
|
15
|
-
newValue: Value,
|
|
16
|
-
oldValue: Value | null,
|
|
17
|
-
state: State,
|
|
18
|
-
) => void | typeof AbortObserving,
|
|
19
|
-
compare = strictEqualCompare,
|
|
20
|
-
initialValue: Value | null = null,
|
|
21
|
-
) {
|
|
22
|
-
let currentValue = initialValue;
|
|
23
|
-
const unsubscribe = store.subscribe(function onStateChange() {
|
|
24
|
-
const state = store.getState();
|
|
25
|
-
const newValue = selector(state);
|
|
26
|
-
|
|
27
|
-
if (!compare(currentValue, newValue)) {
|
|
28
|
-
const oldValue = currentValue;
|
|
29
|
-
currentValue = newValue;
|
|
30
|
-
|
|
31
|
-
const returnValue = listener(newValue, oldValue, state);
|
|
32
|
-
if (returnValue === AbortObserving) {
|
|
33
|
-
unsubscribe();
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
return unsubscribe;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function observeState<State = unknown, Value = unknown>(
|
|
42
|
-
store: Store<State>,
|
|
43
|
-
selector: (state: State) => Value,
|
|
44
|
-
listener: (
|
|
45
|
-
newValue: Value,
|
|
46
|
-
oldValue: Value | null,
|
|
47
|
-
state: State,
|
|
48
|
-
) => void | typeof AbortObserving,
|
|
49
|
-
compare = strictEqualCompare,
|
|
50
|
-
) {
|
|
51
|
-
const initialValue = selector(store.getState());
|
|
52
|
-
return internalObserveState(
|
|
53
|
-
store,
|
|
54
|
-
selector,
|
|
55
|
-
listener,
|
|
56
|
-
compare,
|
|
57
|
-
initialValue,
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function observeStateOnce<State = unknown, Value = unknown>(
|
|
62
|
-
store: Store,
|
|
63
|
-
selector: (state: State) => Value,
|
|
64
|
-
listener: (
|
|
65
|
-
newValue: Value,
|
|
66
|
-
oldValue: Value | null,
|
|
67
|
-
state: State,
|
|
68
|
-
) => void | typeof AbortObserving,
|
|
69
|
-
compare = strictEqualCompare,
|
|
70
|
-
) {
|
|
71
|
-
const unsubscribe = observeState(
|
|
72
|
-
store,
|
|
73
|
-
selector,
|
|
74
|
-
function handleChange(newValue, oldValue, state: State) {
|
|
75
|
-
listener(newValue, oldValue, state);
|
|
76
|
-
unsubscribe();
|
|
77
|
-
},
|
|
78
|
-
compare,
|
|
79
|
-
);
|
|
80
|
-
|
|
81
|
-
return unsubscribe;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function getAndObserveState<State = unknown, Value = unknown>(
|
|
85
|
-
store: Store<State>,
|
|
86
|
-
selector: (state: State) => Value,
|
|
87
|
-
listener: (
|
|
88
|
-
newValue: Value,
|
|
89
|
-
oldValue: Value | null,
|
|
90
|
-
state: State,
|
|
91
|
-
) => void | typeof AbortObserving,
|
|
92
|
-
compare = strictEqualCompare,
|
|
93
|
-
) {
|
|
94
|
-
const initialState = store.getState();
|
|
95
|
-
const initialValue = selector(initialState);
|
|
96
|
-
|
|
97
|
-
const initialReturnValue = listener(initialValue, null, initialState);
|
|
98
|
-
if (initialReturnValue === AbortObserving) {
|
|
99
|
-
return () => undefined;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return internalObserveState(
|
|
103
|
-
store,
|
|
104
|
-
selector,
|
|
105
|
-
listener,
|
|
106
|
-
compare,
|
|
107
|
-
initialValue,
|
|
108
|
-
);
|
|
109
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import {Reducer} from "redux";
|
|
2
|
-
|
|
3
|
-
import addTo from "./add-to";
|
|
4
|
-
import merge from "./merge";
|
|
5
|
-
import noop from "./noop";
|
|
6
|
-
import removeFrom from "./remove-from";
|
|
7
|
-
import set from "./set";
|
|
8
|
-
|
|
9
|
-
const reducers: Record<string, Reducer> = {
|
|
10
|
-
addTo: addTo,
|
|
11
|
-
merge: merge,
|
|
12
|
-
noop: noop,
|
|
13
|
-
removeFrom: removeFrom,
|
|
14
|
-
set: set,
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export default reducers;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import merge from "lodash/merge";
|
|
2
|
-
import {AnyAction, Reducer} from "redux";
|
|
3
|
-
|
|
4
|
-
const mergeReducer: Reducer = <
|
|
5
|
-
T extends Array<unknown> | Record<string | number | symbol, unknown>,
|
|
6
|
-
>(
|
|
7
|
-
state: T,
|
|
8
|
-
action: AnyAction,
|
|
9
|
-
): T => {
|
|
10
|
-
const result: T = (Array.isArray(state) ? [] : {}) as T;
|
|
11
|
-
|
|
12
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
13
|
-
merge(result, state, action.value);
|
|
14
|
-
|
|
15
|
-
return result;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export default mergeReducer;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import {Reducer} from "redux";
|
|
2
|
-
|
|
3
|
-
import addTo from "./add-to";
|
|
4
|
-
import merge from "./merge";
|
|
5
|
-
import noop from "./noop";
|
|
6
|
-
import removeFrom from "./remove-from";
|
|
7
|
-
import set from "./set";
|
|
8
|
-
|
|
9
|
-
const reducers = {
|
|
10
|
-
addTo: addTo,
|
|
11
|
-
merge: merge,
|
|
12
|
-
noop: noop,
|
|
13
|
-
removeFrom: removeFrom,
|
|
14
|
-
set: set,
|
|
15
|
-
} satisfies Record<string, Reducer>;
|
|
16
|
-
|
|
17
|
-
export default reducers;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import unique from "lodash/uniq";
|
|
2
|
-
|
|
3
|
-
export default function reduceByKeys(
|
|
4
|
-
keys: Array<string>,
|
|
5
|
-
state: Record<string, unknown>,
|
|
6
|
-
) {
|
|
7
|
-
if (keys && state) {
|
|
8
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
9
|
-
const uniqueKeys: Array<string> = unique(keys);
|
|
10
|
-
|
|
11
|
-
return uniqueKeys.reduce(function reduceByKey(newState, key) {
|
|
12
|
-
return state[key] !== undefined
|
|
13
|
-
? {...newState, [key]: state[key]}
|
|
14
|
-
: newState;
|
|
15
|
-
}, {});
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return state;
|
|
19
|
-
}
|