@slimlib/store 1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Konstantin Shutkin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # Store
2
+
3
+ Proxy-based store for SPAs.
4
+
5
+ # Installation
6
+
7
+ Using npm:
8
+ ```
9
+ npm install --save-dev @slimlib/store
10
+ ```
11
+
12
+ # Usage
13
+
14
+ TBD
15
+
16
+ ## API
17
+
18
+ ### `createStoreFactory(notifyAfterCreation: boolean)`
19
+
20
+ The only exported function. It returns createStore factory (see next).
21
+
22
+ ### `createStore<T>(initialState: T): [T, Store<T>]`
23
+
24
+ Store factory function that takes initial state and returns proxy object and store tuple. Proxy object ment to be left for actions implementations and store is for subscription for changes.
25
+
26
+ ### `Store<T>`
27
+
28
+ ```typescript
29
+ type StoreCallback<T> = (value: T) => void;
30
+ type UnsubscribeCallback = () => void;
31
+ interface Store<T> {
32
+ (cb: StoreCallback<T>): UnsubscribeCallback;
33
+ (): T;
34
+ }
35
+ ```
36
+
37
+ Publish/subscribe/read pattern implementation. Ment to be used in components / services that want to subscribe for store changes.
38
+
39
+ ## Limitations
40
+
41
+ `Map`, `Set`, `WeakMap`, `WeakSet` cannot be used as values in current implementation.
42
+
43
+ # License
44
+
45
+ [MIT](./LICENSE)
package/dist/index.cjs ADDED
@@ -0,0 +1,74 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const createStoreFactory = (notifyAfterCreation) => {
6
+ return (object = {}) => {
7
+ let willNotifyNextTick = false;
8
+ const proxiesCache = new WeakMap();
9
+ const storeListeners = new Set();
10
+ const enqueueNotification = () => {
11
+ if (!willNotifyNextTick) {
12
+ willNotifyNextTick = true;
13
+ queueMicrotask(() => {
14
+ willNotifyNextTick = false;
15
+ for (const listener of storeListeners) {
16
+ listener(object);
17
+ }
18
+ });
19
+ }
20
+ };
21
+ const handler = {
22
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
+ set(target, p, value, receiver) {
24
+ if (Reflect.get(target, p, receiver) !== value) {
25
+ Reflect.set(target, p, value, receiver);
26
+ enqueueNotification();
27
+ }
28
+ return true;
29
+ },
30
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
31
+ get(target, p, receiver) {
32
+ const value = Reflect.get(target, p, receiver);
33
+ return value !== null && typeof value === 'object' && !(value instanceof RegExp) ? createProxy(value) : value;
34
+ },
35
+ defineProperty(...args) {
36
+ enqueueNotification();
37
+ return Reflect.defineProperty(...args);
38
+ },
39
+ deleteProperty(target, p) {
40
+ const result = Reflect.deleteProperty(target, p);
41
+ if (result) {
42
+ enqueueNotification();
43
+ }
44
+ return result;
45
+ }
46
+ };
47
+ const proxy = createProxy(object);
48
+ return [
49
+ proxy,
50
+ ((cb) => {
51
+ if (!cb) {
52
+ return object;
53
+ }
54
+ storeListeners.add(cb);
55
+ if (notifyAfterCreation) {
56
+ cb(object);
57
+ }
58
+ return () => storeListeners.delete(cb);
59
+ })
60
+ ];
61
+ function createProxy(object) {
62
+ if (proxiesCache.has(object)) {
63
+ return proxiesCache.get(object);
64
+ }
65
+ else {
66
+ const proxy = new Proxy(object, handler);
67
+ proxiesCache.set(object, proxy);
68
+ return proxy;
69
+ }
70
+ }
71
+ };
72
+ };
73
+
74
+ exports.createStoreFactory = createStoreFactory;
@@ -0,0 +1,7 @@
1
+ export declare type StoreCallback<T> = (value: T) => void;
2
+ export declare type UnsubscribeCallback = () => void;
3
+ export interface Store<T> {
4
+ (cb: StoreCallback<T>): UnsubscribeCallback;
5
+ (): T;
6
+ }
7
+ export declare const createStoreFactory: (notifyAfterCreation: boolean) => <T extends object>(object?: T) => [T, Store<T>];
package/dist/index.mjs ADDED
@@ -0,0 +1,70 @@
1
+ const createStoreFactory = (notifyAfterCreation) => {
2
+ return (object = {}) => {
3
+ let willNotifyNextTick = false;
4
+ const proxiesCache = new WeakMap();
5
+ const storeListeners = new Set();
6
+ const enqueueNotification = () => {
7
+ if (!willNotifyNextTick) {
8
+ willNotifyNextTick = true;
9
+ queueMicrotask(() => {
10
+ willNotifyNextTick = false;
11
+ for (const listener of storeListeners) {
12
+ listener(object);
13
+ }
14
+ });
15
+ }
16
+ };
17
+ const handler = {
18
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
+ set(target, p, value, receiver) {
20
+ if (Reflect.get(target, p, receiver) !== value) {
21
+ Reflect.set(target, p, value, receiver);
22
+ enqueueNotification();
23
+ }
24
+ return true;
25
+ },
26
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
+ get(target, p, receiver) {
28
+ const value = Reflect.get(target, p, receiver);
29
+ return value !== null && typeof value === 'object' && !(value instanceof RegExp) ? createProxy(value) : value;
30
+ },
31
+ defineProperty(...args) {
32
+ enqueueNotification();
33
+ return Reflect.defineProperty(...args);
34
+ },
35
+ deleteProperty(target, p) {
36
+ const result = Reflect.deleteProperty(target, p);
37
+ if (result) {
38
+ enqueueNotification();
39
+ }
40
+ return result;
41
+ }
42
+ };
43
+ const proxy = createProxy(object);
44
+ return [
45
+ proxy,
46
+ ((cb) => {
47
+ if (!cb) {
48
+ return object;
49
+ }
50
+ storeListeners.add(cb);
51
+ if (notifyAfterCreation) {
52
+ cb(object);
53
+ }
54
+ return () => storeListeners.delete(cb);
55
+ })
56
+ ];
57
+ function createProxy(object) {
58
+ if (proxiesCache.has(object)) {
59
+ return proxiesCache.get(object);
60
+ }
61
+ else {
62
+ const proxy = new Proxy(object, handler);
63
+ proxiesCache.set(object, proxy);
64
+ return proxy;
65
+ }
66
+ }
67
+ };
68
+ };
69
+
70
+ export { createStoreFactory };
@@ -0,0 +1,2 @@
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).slimlibStore={})}(this,(function(e){"use strict";e.createStoreFactory=e=>(t={})=>{let n=!1;const o=new WeakMap,f=new Set,r=()=>{n||(n=!0,queueMicrotask((()=>{n=!1;for(const e of f)e(t)})))},c={set:(e,t,n,o)=>(Reflect.get(e,t,o)!==n&&(Reflect.set(e,t,n,o),r()),!0),get(e,t,n){const o=Reflect.get(e,t,n);return null===o||"object"!=typeof o||o instanceof RegExp?o:s(o)},defineProperty:(...e)=>(r(),Reflect.defineProperty(...e)),deleteProperty(e,t){const n=Reflect.deleteProperty(e,t);return n&&r(),n}};return[s(t),n=>n?(f.add(n),e&&n(t),()=>f.delete(n)):t];function s(e){if(o.has(e))return o.get(e);{const t=new Proxy(e,c);return o.set(e,t),t}}},Object.defineProperty(e,"__esModule",{value:!0})}));
2
+ //# sourceMappingURL=index.umd.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.umd.js","sources":["../src/index.ts"],"sourcesContent":["export type StoreCallback<T> = (value: T) => void;\nexport type UnsubscribeCallback = () => void;\n\nexport interface Store<T> {\n (cb: StoreCallback<T>): UnsubscribeCallback;\n (): T; // return bare object without Proxy, put into documentation not to change state through it\n}\n\nexport const createStoreFactory = (notifyAfterCreation: boolean) => {\n return <T extends object>(object: T = {} as T): [T, Store<T>] => {\n let willNotifyNextTick = false;\n const proxiesCache = new WeakMap<T, T>();\n const storeListeners = new Set<StoreCallback<T>>();\n const enqueueNotification = () => {\n if (!willNotifyNextTick) {\n willNotifyNextTick = true;\n queueMicrotask(() => {\n willNotifyNextTick = false;\n for (const listener of storeListeners) {\n listener(object);\n }\n });\n }\n };\n const handler = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n set(target: T, p: string | symbol, value: any, receiver: any) {\n if (Reflect.get(target, p, receiver) !== value) {\n Reflect.set(target, p, value, receiver);\n enqueueNotification();\n }\n return true;\n },\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n get(target: T, p: string | symbol, receiver: any) {\n const value = Reflect.get(target, p, receiver);\n return value !== null && typeof value === 'object' && !(value instanceof RegExp) ? createProxy(value) : value;\n },\n defineProperty(...args: [T, string | symbol, PropertyDescriptor]) {\n enqueueNotification();\n return Reflect.defineProperty(...args);\n },\n deleteProperty(target: T, p: string | symbol) {\n const result = Reflect.deleteProperty(target, p);\n if (result) {\n enqueueNotification();\n }\n return result;\n }\n };\n const proxy = createProxy(object);\n return [\n proxy,\n ((cb?: StoreCallback<T>): UnsubscribeCallback | T => {\n if (!cb) {\n return object;\n }\n storeListeners.add(cb);\n if (notifyAfterCreation) {\n cb(object);\n }\n return () => storeListeners.delete(cb);\n }) as Store<T>\n ];\n function createProxy(object: T): T {\n if (proxiesCache.has(object)) {\n return proxiesCache.get(object) as T;\n } else {\n const proxy = new Proxy(object, handler);\n proxiesCache.set(object, proxy);\n return proxy;\n }\n }\n };\n};"],"names":["notifyAfterCreation","object","willNotifyNextTick","proxiesCache","WeakMap","storeListeners","Set","enqueueNotification","queueMicrotask","listener","handler","set","target","p","value","receiver","Reflect","get","RegExp","createProxy","defineProperty","args","deleteProperty","result","cb","add","delete","has","proxy","Proxy"],"mappings":"yQAQmCA,GACxB,CAAmBC,EAAY,MAClC,IAAIC,GAAqB,EACzB,MAAMC,EAAe,IAAIC,QACnBC,EAAiB,IAAIC,IACrBC,EAAsB,KACnBL,IACDA,GAAqB,EACrBM,gBAAe,KACXN,GAAqB,EACrB,IAAK,MAAMO,KAAYJ,EACnBI,EAASR,QAKnBS,EAAU,CAEZC,IAAG,CAACC,EAAWC,EAAoBC,EAAYC,KACvCC,QAAQC,IAAIL,EAAQC,EAAGE,KAAcD,IACrCE,QAAQL,IAAIC,EAAQC,EAAGC,EAAOC,GAC9BR,MAEG,GAGXU,IAAIL,EAAWC,EAAoBE,GAC/B,MAAMD,EAAQE,QAAQC,IAAIL,EAAQC,EAAGE,GACrC,OAAiB,OAAVD,GAAmC,iBAAVA,GAAwBA,aAAiBI,OAA+BJ,EAArBK,EAAYL,IAEnGM,eAAc,IAAIC,KACdd,IACOS,QAAQI,kBAAkBC,IAErCC,eAAeV,EAAWC,GACtB,MAAMU,EAASP,QAAQM,eAAeV,EAAQC,GAI9C,OAHIU,GACAhB,IAEGgB,IAIf,MAAO,CADOJ,EAAYlB,GAGpBuB,GACOA,GAGLnB,EAAeoB,IAAID,GACfxB,GACAwB,EAAGvB,GAEA,IAAMI,EAAeqB,OAAOF,IANxBvB,GASnB,SAASkB,EAAYlB,GACjB,GAAIE,EAAawB,IAAI1B,GACjB,OAAOE,EAAac,IAAIhB,GACrB,CACH,MAAM2B,EAAQ,IAAIC,MAAM5B,EAAQS,GAEhC,OADAP,EAAaQ,IAAIV,EAAQ2B,GAClBA"}
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "version": "1.0.0",
3
+ "license": "MIT",
4
+ "name": "@slimlib/store",
5
+ "author": "Konstantin Shutkin",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "require": "./dist/index.cjs",
10
+ "default": "./dist/index.mjs"
11
+ }
12
+ },
13
+ "main": "./dist/index.cjs",
14
+ "module": "./dist/index.mjs",
15
+ "typings": "./dist/index.d.ts",
16
+ "unpkg": "./dist/index.umd.js",
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "engines": {
21
+ "node": ">=15"
22
+ },
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git+https://github.com/kshutkin/slimlib.git"
26
+ },
27
+ "bugs": {
28
+ "url": "https://github.com/kshutkin/slimlib/issues"
29
+ },
30
+ "homepage": "https://github.com/kshutkin/slimlib/store/README.md",
31
+ "scripts": {
32
+ "build": "pkgbld --umd=index",
33
+ "test": "jest --collectCoverage",
34
+ "lint": "eslint ./src",
35
+ "semantic-release": "npx semantic-release"
36
+ },
37
+ "devDependencies": {
38
+ "@semantic-release/changelog": "6.0.1",
39
+ "@semantic-release/commit-analyzer": "9.0.2",
40
+ "@semantic-release/git": "10.0.1",
41
+ "@semantic-release/npm": "9.0.0",
42
+ "@semantic-release/release-notes-generator": "10.0.3",
43
+ "@types/jest": "27.4.0",
44
+ "@typescript-eslint/eslint-plugin": "5.11.0",
45
+ "@typescript-eslint/parser": "5.11.0",
46
+ "conventional-changelog-angular": "5.0.13",
47
+ "eslint": "8.8.0",
48
+ "jest": "27.5.1",
49
+ "semantic-release": "19.0.2",
50
+ "semantic-release-monorepo": "7.0.5",
51
+ "ts-jest": "27.1.3",
52
+ "typescript": "4.5.x",
53
+ "update-monorepo-package-json": "0.2.0",
54
+ "pkgbld": "1.1.9"
55
+ },
56
+ "dependencies": {},
57
+ "description": "Simple Proxy-based store for SPA"
58
+ }