@codeleap/store 7.0.1 → 7.0.2
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/dist/array.js +6 -2
- package/dist/array.js.map +1 -1
- package/dist/globalState.js +18 -14
- package/dist/globalState.js.map +1 -1
- package/dist/index.js +19 -3
- package/dist/index.js.map +1 -1
- package/dist/types.js +2 -1
- package/dist/utils.js +13 -7
- package/dist/utils.js.map +1 -1
- package/package.json +1 -1
package/dist/array.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.arrayOps = void 0;
|
|
4
|
+
exports.arrayHandler = arrayHandler;
|
|
1
5
|
/**
|
|
2
6
|
* Returns a `Proxy` that mirrors every property access on the atom's current array value.
|
|
3
7
|
*
|
|
@@ -6,7 +10,7 @@
|
|
|
6
10
|
*
|
|
7
11
|
* Non-function properties (indices, `length`, etc.) are forwarded as-is.
|
|
8
12
|
*/
|
|
9
|
-
|
|
13
|
+
function arrayHandler(store) {
|
|
10
14
|
return new Proxy([], {
|
|
11
15
|
get(target, p, receiver) {
|
|
12
16
|
const val = store.get();
|
|
@@ -26,5 +30,5 @@ export function arrayHandler(store) {
|
|
|
26
30
|
* Complete list of own property names on `Array.prototype`.
|
|
27
31
|
* Used by the `globalState` proxy to detect when a caller is accessing an array method so it can delegate to `arrayHandler` instead of the raw atom.
|
|
28
32
|
*/
|
|
29
|
-
|
|
33
|
+
exports.arrayOps = Object.getOwnPropertyNames(Array.prototype);
|
|
30
34
|
//# sourceMappingURL=array.js.map
|
package/dist/array.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array.js","sourceRoot":"","sources":["../src/array.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"array.js","sourceRoot":"","sources":["../src/array.ts"],"names":[],"mappings":";;;AAUA,oCAoBC;AA5BD;;;;;;;GAOG;AACH,SAAgB,YAAY,CAAkB,KAAsB;IAClE,OAAO,IAAI,KAAK,CAAC,EAAE,EAAE;QACnB,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,QAAQ;YACrB,MAAM,GAAG,GAAG,KAAK,CAAC,GAAG,EAAkC,CAAA;YAEvD,MAAM,QAAQ,GAAG,GAAG,CAAC,CAAC,CAAC,CAAA;YAEvB,IAAG,OAAO,QAAQ,IAAI,UAAU,EAAE,CAAC;gBACjC,OAAO,CAAC,GAAG,IAAe,EAAE,EAAE;oBAC5B,MAAM,CAAC,GAAI,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAA;oBAE1B,KAAK,CAAC,GAAG,CAAC,GAAQ,CAAC,CAAA;oBAEnB,OAAO,CAAC,CAAA;gBACV,CAAC,CAAA;YACH,CAAC;YAED,OAAO,QAAQ,CAAA;QACjB,CAAC;KACF,CAAC,CAAA;AACJ,CAAC;AAED;;;GAGG;AACU,QAAA,QAAQ,GAAG,MAAM,CAAC,mBAAmB,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA"}
|
package/dist/globalState.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setGlobalStatePersistor = void 0;
|
|
4
|
+
exports.globalState = globalState;
|
|
5
|
+
const react_1 = require("@nanostores/react");
|
|
6
|
+
const persistent_1 = require("@nanostores/persistent");
|
|
7
|
+
const nanostores_1 = require("nanostores");
|
|
8
|
+
const utils_1 = require("./utils");
|
|
9
|
+
const array_1 = require("./array");
|
|
6
10
|
const defaultConfig = {
|
|
7
11
|
persistKey: undefined,
|
|
8
12
|
};
|
|
@@ -11,7 +15,7 @@ const defaultConfig = {
|
|
|
11
15
|
* Must be called once at app startup — before any `globalState` with a `persistKey` is created — otherwise the persistent atom falls back to an in-memory store.
|
|
12
16
|
* Thin re-export of `@nanostores/persistent`'s `setPersistentEngine`.
|
|
13
17
|
*/
|
|
14
|
-
|
|
18
|
+
exports.setGlobalStatePersistor = persistent_1.setPersistentEngine;
|
|
15
19
|
/**
|
|
16
20
|
* Creates a `GlobalState<T>` atom backed by nanostores.
|
|
17
21
|
*
|
|
@@ -24,20 +28,20 @@ export const setGlobalStatePersistor = setPersistentEngine;
|
|
|
24
28
|
* When `config.persistKey` is provided, the atom is created with `persistentAtom` and serialised via `JSON.stringify` / `JSON.parse`.
|
|
25
29
|
* Call `setGlobalStatePersistor` before using persistence so the engine is ready.
|
|
26
30
|
*/
|
|
27
|
-
|
|
31
|
+
function globalState(value, config = defaultConfig) {
|
|
28
32
|
const { persistKey } = config;
|
|
29
33
|
const isPersistState = typeof persistKey === 'string';
|
|
30
|
-
const store = isPersistState ? persistentAtom(persistKey, value, {
|
|
34
|
+
const store = isPersistState ? (0, persistent_1.persistentAtom)(persistKey, value, {
|
|
31
35
|
encode: JSON.stringify,
|
|
32
36
|
decode: JSON.parse,
|
|
33
|
-
}) : atom(value);
|
|
37
|
+
}) : (0, nanostores_1.atom)(value);
|
|
34
38
|
return new Proxy(store, {
|
|
35
39
|
get(target, prop, receiver) {
|
|
36
40
|
if (prop === 'use') {
|
|
37
41
|
return (selector) => {
|
|
38
42
|
if (!selector)
|
|
39
|
-
return useStore(target);
|
|
40
|
-
return useStateSelector(target, selector);
|
|
43
|
+
return (0, react_1.useStore)(target);
|
|
44
|
+
return (0, utils_1.useStateSelector)(target, selector);
|
|
41
45
|
};
|
|
42
46
|
}
|
|
43
47
|
if (prop === 'get') {
|
|
@@ -49,19 +53,19 @@ export function globalState(value, config = defaultConfig) {
|
|
|
49
53
|
}
|
|
50
54
|
if (prop === 'set') {
|
|
51
55
|
return (newValue) => {
|
|
52
|
-
const value = stateAssign(newValue, target.get());
|
|
56
|
+
const value = (0, utils_1.stateAssign)(newValue, target.get());
|
|
53
57
|
target.set(value);
|
|
54
58
|
};
|
|
55
59
|
}
|
|
56
60
|
if (prop == 'reset') {
|
|
57
61
|
return Reflect.get(target, 'set', receiver);
|
|
58
62
|
}
|
|
59
|
-
if (arrayOps.includes(prop)) {
|
|
63
|
+
if (array_1.arrayOps.includes(prop)) {
|
|
60
64
|
const currentValue = target.get();
|
|
61
65
|
if (!Array.isArray(currentValue)) {
|
|
62
66
|
throw new Error('Cannot call array methods on a non array store');
|
|
63
67
|
}
|
|
64
|
-
const handle = arrayHandler(target);
|
|
68
|
+
const handle = (0, array_1.arrayHandler)(target);
|
|
65
69
|
return Reflect.get(handle, prop, receiver);
|
|
66
70
|
}
|
|
67
71
|
return Reflect.get(target, prop, receiver);
|
package/dist/globalState.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"globalState.js","sourceRoot":"","sources":["../src/globalState.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"globalState.js","sourceRoot":"","sources":["../src/globalState.ts"],"names":[],"mappings":";;;AA8BA,kCAoDC;AAlFD,6CAA4C;AAC5C,uDAA4E;AAC5E,2CAA+C;AAE/C,mCAAuD;AACvD,mCAAgD;AAEhD,MAAM,aAAa,GAAsB;IACvC,UAAU,EAAE,SAAS;CACtB,CAAA;AAED;;;;GAIG;AACU,QAAA,uBAAuB,GAAG,gCAAmB,CAAA;AAE1D;;;;;;;;;;;GAWG;AACH,SAAgB,WAAW,CAAI,KAAQ,EAAE,SAA4B,aAAa;IAChF,MAAM,EAAE,UAAU,EAAE,GAAG,MAAM,CAAA;IAE7B,MAAM,cAAc,GAAG,OAAO,UAAU,KAAK,QAAQ,CAAA;IAErD,MAAM,KAAK,GAAG,cAAc,CAAC,CAAC,CAAC,IAAA,2BAAc,EAAI,UAAU,EAAE,KAAK,EAAE;QAClE,MAAM,EAAE,IAAI,CAAC,SAAS;QACtB,MAAM,EAAE,IAAI,CAAC,KAAK;KACnB,CAAC,CAAC,CAAC,CAAC,IAAA,iBAAI,E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AAC,CAAA;gBACnE,CAAC;gBAED,MAAM,MAAM,GAAG,IAAA,oBAAY,EAAC,MAA6B,CAAC,CAAA;gBAE1D,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAA;YAC5C,CAAC;YAED,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAA;QAC5C,CAAC;KACF,CAA8B,CAAA;AACjC,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./globalState"), exports);
|
|
18
|
+
__exportStar(require("./types"), exports);
|
|
19
|
+
__exportStar(require("./utils"), exports);
|
|
4
20
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gDAA6B;AAC7B,0CAAuB;AACvB,0CAAuB"}
|
package/dist/types.js
CHANGED
package/dist/utils.js
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createStateSlice = void 0;
|
|
4
|
+
exports.stateAssign = stateAssign;
|
|
5
|
+
exports.useStateSelector = useStateSelector;
|
|
6
|
+
const react_1 = require("@nanostores/react");
|
|
7
|
+
const react_2 = require("react");
|
|
3
8
|
/** Type guard that distinguishes a functional setter from a plain value so `resolveSetter` can call it correctly. */
|
|
4
9
|
function isFunctionSetter(x) {
|
|
5
10
|
return typeof x === 'function';
|
|
@@ -17,7 +22,7 @@ function resolveSetter(setter, currentValue) {
|
|
|
17
22
|
* For object atoms the resolved value is shallow-merged onto the existing state, so callers only need to supply changed keys.
|
|
18
23
|
* For primitives and arrays the resolved value replaces the current state entirely.
|
|
19
24
|
*/
|
|
20
|
-
|
|
25
|
+
function stateAssign(newValue, stateValue) {
|
|
21
26
|
const resolvedValue = resolveSetter(newValue, stateValue);
|
|
22
27
|
if (typeof stateValue === 'object' && stateValue !== null) {
|
|
23
28
|
return Object.assign(Object.assign({}, stateValue), resolvedValue);
|
|
@@ -33,7 +38,7 @@ export function stateAssign(newValue, stateValue) {
|
|
|
33
38
|
*
|
|
34
39
|
* The returned object satisfies the `WritableAtom<R>` interface so it can be passed anywhere a nanostores atom is expected, including `useStore`.
|
|
35
40
|
*/
|
|
36
|
-
|
|
41
|
+
const createStateSlice = (store, selector, deselector) => ({
|
|
37
42
|
get: () => selector(store.get()),
|
|
38
43
|
listen: (listener) => {
|
|
39
44
|
return store.listen((state) => {
|
|
@@ -52,6 +57,7 @@ export const createStateSlice = (store, selector, deselector) => ({
|
|
|
52
57
|
return store.value;
|
|
53
58
|
}
|
|
54
59
|
});
|
|
60
|
+
exports.createStateSlice = createStateSlice;
|
|
55
61
|
/**
|
|
56
62
|
* React hook that subscribes a component to a derived slice of an atom.
|
|
57
63
|
*
|
|
@@ -60,8 +66,8 @@ export const createStateSlice = (store, selector, deselector) => ({
|
|
|
60
66
|
*
|
|
61
67
|
* Caution: because the slice is memoised on `selector` reference, prefer stable (module-level or `useCallback`) selector functions to avoid unnecessary slice recreation.
|
|
62
68
|
*/
|
|
63
|
-
|
|
64
|
-
const slice = useMemo(() => createStateSlice(store, selector), [selector]);
|
|
65
|
-
return useStore(slice);
|
|
69
|
+
function useStateSelector(store, selector) {
|
|
70
|
+
const slice = (0, react_2.useMemo)(() => (0, exports.createStateSlice)(store, selector), [selector]);
|
|
71
|
+
return (0, react_1.useStore)(slice);
|
|
66
72
|
}
|
|
67
73
|
//# sourceMappingURL=utils.js.map
|
package/dist/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAyBA,kCAYC;AA8CD,4CAOC;AA1FD,6CAA4C;AAE5C,iCAA+B;AAG/B,qHAAqH;AACrH,SAAS,gBAAgB,CAAI,CAAM;IACjC,OAAO,OAAO,CAAC,KAAK,UAAU,CAAA;AAChC,CAAC;AAED,+GAA+G;AAC/G,SAAS,aAAa,CAAI,MAAsB,EAAE,YAAc;IAC9D,IAAI,gBAAgB,CAAI,MAAM,CAAC,EAAE,CAAC;QAChC,OAAO,MAAM,CAAC,YAAY,CAAC,CAAA;IAC7B,CAAC;IAED,OAAO,MAAW,CAAA;AACpB,CAAC;AAED;;;;;GAKG;AACH,SAAgB,WAAW,CAAI,QAAiC,EAAE,UAAa;IAC7E,MAAM,aAAa,GAAG,aAAa,CAAC,QAAQ,EAAE,UAAU,CAAC,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