@slimlib/store 1.1.2 → 1.3.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 +14 -5
- package/dist/core.cjs +3 -1
- package/dist/core.d.ts +2 -1
- package/dist/core.mjs +3 -2
- package/dist/index.cjs +1 -0
- package/dist/index.mjs +1 -1
- package/dist/preact.cjs +1 -1
- package/dist/preact.d.ts +1 -1
- package/dist/preact.mjs +1 -1
- package/dist/preact.umd.js +1 -1
- package/dist/preact.umd.js.map +1 -1
- package/dist/react.cjs +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/react.mjs +1 -1
- package/dist/react.umd.js +1 -1
- package/dist/react.umd.js.map +1 -1
- package/dist/svelte.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -73,7 +73,7 @@ export function doSomething() {
|
|
|
73
73
|
state.field = value;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
export
|
|
76
|
+
export default { subscribe };
|
|
77
77
|
```
|
|
78
78
|
|
|
79
79
|
In component
|
|
@@ -95,9 +95,13 @@ $storeName
|
|
|
95
95
|
|
|
96
96
|
The only exported function. It returns createStore factory (see next) which notifies innidiately after creating store if `notifyAfterCreation` is truethy.
|
|
97
97
|
|
|
98
|
-
#### `createStore<T>(initialState: T): [T, Store<T
|
|
98
|
+
#### `createStore<T>(initialState: T): [T, Store<T>, () => void]`
|
|
99
99
|
|
|
100
|
-
Store factory function that takes initial state and returns proxy object and
|
|
100
|
+
Store factory function that takes initial state and returns proxy object, store and function to notify subscribers. Proxy object ment to be left for actions implementations, store is for subscription for changes and notification only for some edge cases when original object have been changed and listeners have to be notified.
|
|
101
|
+
|
|
102
|
+
#### `unwrapValue(value: T): T`
|
|
103
|
+
|
|
104
|
+
Unwraps potential proxy object and returns plain object if possible or value itself.
|
|
101
105
|
|
|
102
106
|
#### `Store<T>`
|
|
103
107
|
|
|
@@ -114,7 +118,7 @@ Publish/subscribe/read pattern implementation. Ment to be used in components / s
|
|
|
114
118
|
|
|
115
119
|
### `react` and `preact` exports
|
|
116
120
|
|
|
117
|
-
#### `createStore<T>(initialState: T): [T, Store<T
|
|
121
|
+
#### `createStore<T>(initialState: T): [T, Store<T>, () => void]`
|
|
118
122
|
|
|
119
123
|
Store factory created with `notifyAfterCreation` === `false`.
|
|
120
124
|
|
|
@@ -124,7 +128,7 @@ Function to subscribe to store inside component. Returns current state.
|
|
|
124
128
|
|
|
125
129
|
### `svelte` export
|
|
126
130
|
|
|
127
|
-
#### `createStore<T>(initialState: T): [T, Store<T
|
|
131
|
+
#### `createStore<T>(initialState: T): [T, Store<T>, () => void]`
|
|
128
132
|
|
|
129
133
|
Store factory created with `notifyAfterCreation` === `true`.
|
|
130
134
|
|
|
@@ -135,6 +139,11 @@ Store factory created with `notifyAfterCreation` === `true`.
|
|
|
135
139
|
Mixing proxied values and values from underlying object can fail for cases where code needs checking for equality.
|
|
136
140
|
|
|
137
141
|
For example searching array element from underlying object in proxified array will fail.
|
|
142
|
+
|
|
143
|
+
## Similar projects
|
|
144
|
+
|
|
145
|
+
[Valtio](https://github.com/pmndrs/valtio) - more sofisticated but similar approach, less limitations
|
|
146
|
+
|
|
138
147
|
# License
|
|
139
148
|
|
|
140
149
|
[MIT](./LICENSE)
|
package/dist/core.cjs
CHANGED
|
@@ -61,7 +61,8 @@ const createStoreFactory = (notifyAfterCreation) => {
|
|
|
61
61
|
cb(object);
|
|
62
62
|
}
|
|
63
63
|
return () => storeListeners.delete(cb);
|
|
64
|
-
})
|
|
64
|
+
}),
|
|
65
|
+
enqueueNotification
|
|
65
66
|
];
|
|
66
67
|
function createProxy(object) {
|
|
67
68
|
if (proxiesCache.has(object)) {
|
|
@@ -77,3 +78,4 @@ const createStoreFactory = (notifyAfterCreation) => {
|
|
|
77
78
|
};
|
|
78
79
|
|
|
79
80
|
exports.createStoreFactory = createStoreFactory;
|
|
81
|
+
exports.unwrapValue = unwrapValue;
|
package/dist/core.d.ts
CHANGED
|
@@ -4,4 +4,5 @@ export interface Store<T> {
|
|
|
4
4
|
(cb: StoreCallback<T>): UnsubscribeCallback;
|
|
5
5
|
(): Readonly<T>;
|
|
6
6
|
}
|
|
7
|
-
export declare const
|
|
7
|
+
export declare const unwrapValue: <T>(value: T) => T;
|
|
8
|
+
export declare const createStoreFactory: (notifyAfterCreation: boolean) => <T extends object>(object?: T) => [T, Store<T>, () => void];
|
package/dist/core.mjs
CHANGED
|
@@ -57,7 +57,8 @@ const createStoreFactory = (notifyAfterCreation) => {
|
|
|
57
57
|
cb(object);
|
|
58
58
|
}
|
|
59
59
|
return () => storeListeners.delete(cb);
|
|
60
|
-
})
|
|
60
|
+
}),
|
|
61
|
+
enqueueNotification
|
|
61
62
|
];
|
|
62
63
|
function createProxy(object) {
|
|
63
64
|
if (proxiesCache.has(object)) {
|
|
@@ -72,4 +73,4 @@ const createStoreFactory = (notifyAfterCreation) => {
|
|
|
72
73
|
};
|
|
73
74
|
};
|
|
74
75
|
|
|
75
|
-
export { createStoreFactory };
|
|
76
|
+
export { createStoreFactory, unwrapValue };
|
package/dist/index.cjs
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { createStoreFactory } from './core.mjs';
|
|
1
|
+
export { createStoreFactory, unwrapValue } from './core.mjs';
|
package/dist/preact.cjs
CHANGED
package/dist/preact.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { Store } from './core';
|
|
2
|
-
export declare const createStore: <T extends object>(object?: T) => [T, Store<T
|
|
2
|
+
export declare const createStore: <T extends object>(object?: T) => [T, Store<T>, () => void];
|
|
3
3
|
export declare const useStore: <T>(store: Store<T>) => Readonly<T>;
|
package/dist/preact.mjs
CHANGED
package/dist/preact.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports,require("preact/hooks"),require("./core")):"function"==typeof define&&define.amd?define(["exports","preact/hooks","./core"],o):o((e="undefined"!=typeof globalThis?globalThis:e||self).slimlibStorePreact={},e.preactHooks,e.slimlibStoreCore)}(this,(function(e,o,t){"use strict";const n=t.createStoreFactory(!1);e.createStore=n,e.useStore=e=>{const[,t]=o.useState();return o.useEffect((()=>e((()=>t({})))),[]),e()},Object.defineProperty(e,"__esModule",{value:!0})}));
|
|
1
|
+
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports,require("preact/hooks"),require("./core")):"function"==typeof define&&define.amd?define(["exports","preact/hooks","./core"],o):o((e="undefined"!=typeof globalThis?globalThis:e||self).slimlibStorePreact={},e.preactHooks,e.slimlibStoreCore)}(this,(function(e,o,t){"use strict";const n=t.createStoreFactory(!1);e.createStore=n,e.useStore=e=>{const[,t]=o.useState();return o.useEffect((()=>e((()=>t({})))),[e]),e()},Object.defineProperty(e,"__esModule",{value:!0})}));
|
|
2
2
|
//# sourceMappingURL=preact.umd.js.map
|
package/dist/preact.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"preact.umd.js","sources":["../src/preact.ts"],"sourcesContent":["import { useState, useEffect } from 'preact/hooks';\nimport { createStoreFactory, Store } from './core';\n\nexport const createStore = createStoreFactory(false);\n\nexport const useStore = <T>(store: Store<T>) => {\n const [, setState] = useState<object>();\n \n useEffect(() => {\n return store(() => setState({}));\n }, []);\n \n return store();\n};\n"],"names":["createStore","createStoreFactory","store","setState","useState","useEffect"],"mappings":"uWAGaA,EAAcC,sBAAmB,8BAElBC,IACxB,OAASC,GAAYC,aAMrB,OAJAC,aAAU,IACCH,GAAM,IAAMC,EAAS,OAC7B,
|
|
1
|
+
{"version":3,"file":"preact.umd.js","sources":["../src/preact.ts"],"sourcesContent":["import { useState, useEffect } from 'preact/hooks';\nimport { createStoreFactory, Store } from './core';\n\nexport const createStore = createStoreFactory(false);\n\nexport const useStore = <T>(store: Store<T>) => {\n const [, setState] = useState<object>();\n \n useEffect(() => {\n return store(() => setState({}));\n }, [store]);\n \n return store();\n};\n"],"names":["createStore","createStoreFactory","store","setState","useState","useEffect"],"mappings":"uWAGaA,EAAcC,sBAAmB,8BAElBC,IACxB,OAASC,GAAYC,aAMrB,OAJAC,aAAU,IACCH,GAAM,IAAMC,EAAS,OAC7B,CAACD,IAEGA"}
|
package/dist/react.cjs
CHANGED
package/dist/react.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { Store } from './core';
|
|
2
|
-
export declare const createStore: <T extends object>(object?: T) => [T, Store<T
|
|
2
|
+
export declare const createStore: <T extends object>(object?: T) => [T, Store<T>, () => void];
|
|
3
3
|
export declare const useStore: <T>(store: Store<T>) => Readonly<T>;
|
package/dist/react.mjs
CHANGED
package/dist/react.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("./core")):"function"==typeof define&&define.amd?define(["exports","react","./core"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).slimlibStoreReact={},e.react,e.slimlibStoreCore)}(this,(function(e,t,o){"use strict";const n=o.createStoreFactory(!1);e.createStore=n,e.useStore=e=>{const[,o]=t.useState();return t.useEffect((()=>e((()=>o({})))),[]),e()},Object.defineProperty(e,"__esModule",{value:!0})}));
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("./core")):"function"==typeof define&&define.amd?define(["exports","react","./core"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).slimlibStoreReact={},e.react,e.slimlibStoreCore)}(this,(function(e,t,o){"use strict";const n=o.createStoreFactory(!1);e.createStore=n,e.useStore=e=>{const[,o]=t.useState();return t.useEffect((()=>e((()=>o({})))),[e]),e()},Object.defineProperty(e,"__esModule",{value:!0})}));
|
|
2
2
|
//# sourceMappingURL=react.umd.js.map
|
package/dist/react.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.umd.js","sources":["../src/react.ts"],"sourcesContent":["import { useState, useEffect } from 'react';\nimport { createStoreFactory, Store } from './core';\n\nexport const createStore = createStoreFactory(false);\n\nexport const useStore = <T>(store: Store<T>) => {\n const [, setState] = useState<object>();\n \n useEffect(() => {\n return store(() => setState({}));\n }, []);\n \n return store();\n};\n"],"names":["createStore","createStoreFactory","store","setState","useState","useEffect"],"mappings":"kVAGaA,EAAcC,sBAAmB,8BAElBC,IACxB,OAASC,GAAYC,aAMrB,OAJAC,aAAU,IACCH,GAAM,IAAMC,EAAS,OAC7B,
|
|
1
|
+
{"version":3,"file":"react.umd.js","sources":["../src/react.ts"],"sourcesContent":["import { useState, useEffect } from 'react';\nimport { createStoreFactory, Store } from './core';\n\nexport const createStore = createStoreFactory(false);\n\nexport const useStore = <T>(store: Store<T>) => {\n const [, setState] = useState<object>();\n \n useEffect(() => {\n return store(() => setState({}));\n }, [store]);\n \n return store();\n};\n"],"names":["createStore","createStoreFactory","store","setState","useState","useEffect"],"mappings":"kVAGaA,EAAcC,sBAAmB,8BAElBC,IACxB,OAASC,GAAYC,aAMrB,OAJAC,aAAU,IACCH,GAAM,IAAMC,EAAS,OAC7B,CAACD,IAEGA"}
|
package/dist/svelte.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const createStore: <T extends object>(object?: T) => [T, import("./core").Store<T
|
|
1
|
+
export declare const createStore: <T extends object>(object?: T) => [T, import("./core").Store<T>, () => void];
|
package/package.json
CHANGED