@codeleap/store 7.0.2 → 7.1.1
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 +2 -6
- package/dist/array.js.map +1 -1
- package/dist/globalState.js +14 -18
- package/dist/globalState.js.map +1 -1
- package/dist/index.js +3 -19
- package/dist/index.js.map +1 -1
- package/dist/types.js +1 -2
- package/dist/utils.js +11 -14
- package/dist/utils.js.map +1 -1
- package/package.json +2 -2
package/dist/array.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.arrayOps = void 0;
|
|
4
|
-
exports.arrayHandler = arrayHandler;
|
|
5
1
|
/**
|
|
6
2
|
* Returns a `Proxy` that mirrors every property access on the atom's current array value.
|
|
7
3
|
*
|
|
@@ -10,7 +6,7 @@ exports.arrayHandler = arrayHandler;
|
|
|
10
6
|
*
|
|
11
7
|
* Non-function properties (indices, `length`, etc.) are forwarded as-is.
|
|
12
8
|
*/
|
|
13
|
-
function arrayHandler(store) {
|
|
9
|
+
export function arrayHandler(store) {
|
|
14
10
|
return new Proxy([], {
|
|
15
11
|
get(target, p, receiver) {
|
|
16
12
|
const val = store.get();
|
|
@@ -30,5 +26,5 @@ function arrayHandler(store) {
|
|
|
30
26
|
* Complete list of own property names on `Array.prototype`.
|
|
31
27
|
* 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.
|
|
32
28
|
*/
|
|
33
|
-
|
|
29
|
+
export const arrayOps = Object.getOwnPropertyNames(Array.prototype);
|
|
34
30
|
//# 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":"AAEA;;;;;;;GAOG;AACH,MAAM,UAAU,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;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,MAAM,CAAC,mBAAmB,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA"}
|
package/dist/globalState.js
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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");
|
|
1
|
+
import { useStore } from '@nanostores/react';
|
|
2
|
+
import { setPersistentEngine, persistentAtom } from '@nanostores/persistent';
|
|
3
|
+
import { atom } from 'nanostores';
|
|
4
|
+
import { stateAssign, useStateSelector } from './utils';
|
|
5
|
+
import { arrayHandler, arrayOps } from './array';
|
|
10
6
|
const defaultConfig = {
|
|
11
7
|
persistKey: undefined,
|
|
12
8
|
};
|
|
@@ -15,7 +11,7 @@ const defaultConfig = {
|
|
|
15
11
|
* 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.
|
|
16
12
|
* Thin re-export of `@nanostores/persistent`'s `setPersistentEngine`.
|
|
17
13
|
*/
|
|
18
|
-
|
|
14
|
+
export const setGlobalStatePersistor = setPersistentEngine;
|
|
19
15
|
/**
|
|
20
16
|
* Creates a `GlobalState<T>` atom backed by nanostores.
|
|
21
17
|
*
|
|
@@ -28,20 +24,20 @@ exports.setGlobalStatePersistor = persistent_1.setPersistentEngine;
|
|
|
28
24
|
* When `config.persistKey` is provided, the atom is created with `persistentAtom` and serialised via `JSON.stringify` / `JSON.parse`.
|
|
29
25
|
* Call `setGlobalStatePersistor` before using persistence so the engine is ready.
|
|
30
26
|
*/
|
|
31
|
-
function globalState(value, config = defaultConfig) {
|
|
27
|
+
export function globalState(value, config = defaultConfig) {
|
|
32
28
|
const { persistKey } = config;
|
|
33
29
|
const isPersistState = typeof persistKey === 'string';
|
|
34
|
-
const store = isPersistState ?
|
|
30
|
+
const store = isPersistState ? persistentAtom(persistKey, value, {
|
|
35
31
|
encode: JSON.stringify,
|
|
36
32
|
decode: JSON.parse,
|
|
37
|
-
}) :
|
|
33
|
+
}) : atom(value);
|
|
38
34
|
return new Proxy(store, {
|
|
39
35
|
get(target, prop, receiver) {
|
|
40
36
|
if (prop === 'use') {
|
|
41
37
|
return (selector) => {
|
|
42
38
|
if (!selector)
|
|
43
|
-
return
|
|
44
|
-
return
|
|
39
|
+
return useStore(target);
|
|
40
|
+
return useStateSelector(target, selector);
|
|
45
41
|
};
|
|
46
42
|
}
|
|
47
43
|
if (prop === 'get') {
|
|
@@ -53,19 +49,19 @@ function globalState(value, config = defaultConfig) {
|
|
|
53
49
|
}
|
|
54
50
|
if (prop === 'set') {
|
|
55
51
|
return (newValue) => {
|
|
56
|
-
const value =
|
|
52
|
+
const value = stateAssign(newValue, target.get());
|
|
57
53
|
target.set(value);
|
|
58
54
|
};
|
|
59
55
|
}
|
|
60
56
|
if (prop == 'reset') {
|
|
61
57
|
return Reflect.get(target, 'set', receiver);
|
|
62
58
|
}
|
|
63
|
-
if (
|
|
59
|
+
if (arrayOps.includes(prop)) {
|
|
64
60
|
const currentValue = target.get();
|
|
65
61
|
if (!Array.isArray(currentValue)) {
|
|
66
62
|
throw new Error('Cannot call array methods on a non array store');
|
|
67
63
|
}
|
|
68
|
-
const handle =
|
|
64
|
+
const handle = arrayHandler(target);
|
|
69
65
|
return Reflect.get(handle, prop, receiver);
|
|
70
66
|
}
|
|
71
67
|
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":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAC5C,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAC5E,OAAO,EAAE,IAAI,EAAgB,MAAM,YAAY,CAAA;AAE/C,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AACvD,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAEhD,MAAM,aAAa,GAAsB;IACvC,UAAU,EAAE,SAAS;CACtB,CAAA;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,mBAAmB,CAAA;AAE1D;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,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,cAAc,CAAI,UAAU,EAA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,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC,EAAE,CAAC;oBACjC,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAA;gBACnE,CAAC;gBAED,MAAM,MAAM,GAAG,YAAY,CAAC,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,20 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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);
|
|
1
|
+
export * from './globalState';
|
|
2
|
+
export * from './types';
|
|
3
|
+
export * from './utils';
|
|
20
4
|
//# 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":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"}
|
package/dist/types.js
CHANGED
package/dist/utils.js
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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");
|
|
1
|
+
import { useStore } from '@nanostores/react';
|
|
2
|
+
import { useMemo } from 'react';
|
|
8
3
|
/** Type guard that distinguishes a functional setter from a plain value so `resolveSetter` can call it correctly. */
|
|
9
4
|
function isFunctionSetter(x) {
|
|
10
5
|
return typeof x === 'function';
|
|
@@ -22,10 +17,13 @@ function resolveSetter(setter, currentValue) {
|
|
|
22
17
|
* For object atoms the resolved value is shallow-merged onto the existing state, so callers only need to supply changed keys.
|
|
23
18
|
* For primitives and arrays the resolved value replaces the current state entirely.
|
|
24
19
|
*/
|
|
25
|
-
function stateAssign(newValue, stateValue) {
|
|
20
|
+
export function stateAssign(newValue, stateValue) {
|
|
26
21
|
const resolvedValue = resolveSetter(newValue, stateValue);
|
|
27
22
|
if (typeof stateValue === 'object' && stateValue !== null) {
|
|
28
|
-
return
|
|
23
|
+
return {
|
|
24
|
+
...stateValue,
|
|
25
|
+
...resolvedValue,
|
|
26
|
+
};
|
|
29
27
|
}
|
|
30
28
|
return resolvedValue;
|
|
31
29
|
}
|
|
@@ -38,7 +36,7 @@ function stateAssign(newValue, stateValue) {
|
|
|
38
36
|
*
|
|
39
37
|
* The returned object satisfies the `WritableAtom<R>` interface so it can be passed anywhere a nanostores atom is expected, including `useStore`.
|
|
40
38
|
*/
|
|
41
|
-
const createStateSlice = (store, selector, deselector) => ({
|
|
39
|
+
export const createStateSlice = (store, selector, deselector) => ({
|
|
42
40
|
get: () => selector(store.get()),
|
|
43
41
|
listen: (listener) => {
|
|
44
42
|
return store.listen((state) => {
|
|
@@ -57,7 +55,6 @@ const createStateSlice = (store, selector, deselector) => ({
|
|
|
57
55
|
return store.value;
|
|
58
56
|
}
|
|
59
57
|
});
|
|
60
|
-
exports.createStateSlice = createStateSlice;
|
|
61
58
|
/**
|
|
62
59
|
* React hook that subscribes a component to a derived slice of an atom.
|
|
63
60
|
*
|
|
@@ -66,8 +63,8 @@ exports.createStateSlice = createStateSlice;
|
|
|
66
63
|
*
|
|
67
64
|
* Caution: because the slice is memoised on `selector` reference, prefer stable (module-level or `useCallback`) selector functions to avoid unnecessary slice recreation.
|
|
68
65
|
*/
|
|
69
|
-
function useStateSelector(store, selector) {
|
|
70
|
-
const slice =
|
|
71
|
-
return
|
|
66
|
+
export function useStateSelector(store, selector) {
|
|
67
|
+
const slice = useMemo(() => createStateSlice(store, selector), [selector]);
|
|
68
|
+
return useStore(slice);
|
|
72
69
|
}
|
|
73
70
|
//# 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":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAE5C,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAA;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,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ACR,QAAyB;IAEzB,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,gBAAgB,CAAC,KAAK,EAAE,QAAQ,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAA;IAE1E,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAA;AACxB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codeleap/store",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.1.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"exports": {
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"directory": "packages/store"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@codeleap/config": "7.0.
|
|
25
|
+
"@codeleap/config": "7.0.2",
|
|
26
26
|
"ts-node-dev": "1.1.8"
|
|
27
27
|
},
|
|
28
28
|
"scripts": {
|