@dynamic-labs/store 2.1.0-alpha.21

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) 2022 Dynamic Labs, Inc.
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
File without changes
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@dynamic-labs/store",
3
+ "version": "2.1.0-alpha.21",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/dynamic-labs/DynamicAuth.git",
7
+ "directory": "packages/store"
8
+ },
9
+ "description": "Store ",
10
+ "bugs": {
11
+ "url": "https://github.com/dynamic-labs/DynamicAuth/issues"
12
+ },
13
+ "homepage": "https://github.com/dynamic-labs/DynamicAuth#readme",
14
+ "author": "Dynamic Labs, Inc.",
15
+ "license": "MIT",
16
+ "main": "./src/index.cjs",
17
+ "module": "./src/index.js",
18
+ "types": "./src/index.d.ts",
19
+ "type": "module",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./src/index.d.ts",
23
+ "import": "./src/index.js",
24
+ "require": "./src/index.cjs"
25
+ },
26
+ "./package.json": "./package.json"
27
+ },
28
+ "dependencies": {
29
+ "@dynamic-labs/logger": "2.1.0-alpha.21"
30
+ },
31
+ "peerDependencies": {}
32
+ }
package/src/index.cjs ADDED
@@ -0,0 +1,14 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var createStore = require('./lib/createStore/createStore.cjs');
7
+ var persist = require('./lib/persist/persist.cjs');
8
+ var subscribeWithSelector = require('./lib/subscribeWithSelector/subscribeWithSelector.cjs');
9
+
10
+
11
+
12
+ exports.createStore = createStore.createStore;
13
+ exports.createPersist = persist.createPersist;
14
+ exports.subscribeWithSelector = subscribeWithSelector.subscribeWithSelector;
package/src/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export { type StoreApi, type PersistStorage, createPersist, createStore, subscribeWithSelector, } from './lib';
package/src/index.js ADDED
@@ -0,0 +1,4 @@
1
+ 'use client'
2
+ export { createStore } from './lib/createStore/createStore.js';
3
+ export { createPersist } from './lib/persist/persist.js';
4
+ export { subscribeWithSelector } from './lib/subscribeWithSelector/subscribeWithSelector.js';
@@ -0,0 +1,58 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ /**
7
+ * Creates a store with the specified initial state and provides methods to manage and subscribe to the state.
8
+ *
9
+ * @template T - The type of the state object.
10
+ * @param getInitialState - A function that returns the initial state object.
11
+ * @returns An object that represents the store and provides methods to interact with the state.
12
+ */
13
+ const createStore = (getInitialState) => {
14
+ const subscriptions = new Set();
15
+ let state = getInitialState();
16
+ const notifySubscribers = () => {
17
+ for (const subscription of subscriptions) {
18
+ subscription(state);
19
+ }
20
+ };
21
+ return {
22
+ /**
23
+ * Returns the initial state object.
24
+ *
25
+ * @returns The initial state object.
26
+ */
27
+ getInitialState: () => getInitialState(),
28
+ /**
29
+ * Returns the current state object.
30
+ *
31
+ * @returns The current state object.
32
+ */
33
+ getState: () => state,
34
+ /**
35
+ * Updates the state object with the provided partial state and notifies all subscribers.
36
+ *
37
+ * @param partialState - The partial state object to merge with the current state.
38
+ */
39
+ setState: (partialState) => {
40
+ state = Object.assign(Object.assign({}, state), partialState);
41
+ notifySubscribers();
42
+ },
43
+ /**
44
+ * Subscribes to state changes and invokes the provided callback whenever the state is updated.
45
+ *
46
+ * @param subscription - A callback function that receives the updated state as a parameter.
47
+ * @returns A function that can be called to unsubscribe from state changes.
48
+ */
49
+ subscribe: (subscription) => {
50
+ subscriptions.add(subscription);
51
+ return () => {
52
+ subscriptions.delete(subscription);
53
+ };
54
+ },
55
+ };
56
+ };
57
+
58
+ exports.createStore = createStore;
@@ -0,0 +1,9 @@
1
+ import { StoreApi } from '../types';
2
+ /**
3
+ * Creates a store with the specified initial state and provides methods to manage and subscribe to the state.
4
+ *
5
+ * @template T - The type of the state object.
6
+ * @param getInitialState - A function that returns the initial state object.
7
+ * @returns An object that represents the store and provides methods to interact with the state.
8
+ */
9
+ export declare const createStore: <T extends object>(getInitialState: () => T) => StoreApi<T>;
@@ -0,0 +1,54 @@
1
+ 'use client'
2
+ /**
3
+ * Creates a store with the specified initial state and provides methods to manage and subscribe to the state.
4
+ *
5
+ * @template T - The type of the state object.
6
+ * @param getInitialState - A function that returns the initial state object.
7
+ * @returns An object that represents the store and provides methods to interact with the state.
8
+ */
9
+ const createStore = (getInitialState) => {
10
+ const subscriptions = new Set();
11
+ let state = getInitialState();
12
+ const notifySubscribers = () => {
13
+ for (const subscription of subscriptions) {
14
+ subscription(state);
15
+ }
16
+ };
17
+ return {
18
+ /**
19
+ * Returns the initial state object.
20
+ *
21
+ * @returns The initial state object.
22
+ */
23
+ getInitialState: () => getInitialState(),
24
+ /**
25
+ * Returns the current state object.
26
+ *
27
+ * @returns The current state object.
28
+ */
29
+ getState: () => state,
30
+ /**
31
+ * Updates the state object with the provided partial state and notifies all subscribers.
32
+ *
33
+ * @param partialState - The partial state object to merge with the current state.
34
+ */
35
+ setState: (partialState) => {
36
+ state = Object.assign(Object.assign({}, state), partialState);
37
+ notifySubscribers();
38
+ },
39
+ /**
40
+ * Subscribes to state changes and invokes the provided callback whenever the state is updated.
41
+ *
42
+ * @param subscription - A callback function that receives the updated state as a parameter.
43
+ * @returns A function that can be called to unsubscribe from state changes.
44
+ */
45
+ subscribe: (subscription) => {
46
+ subscriptions.add(subscription);
47
+ return () => {
48
+ subscriptions.delete(subscription);
49
+ };
50
+ },
51
+ };
52
+ };
53
+
54
+ export { createStore };
@@ -0,0 +1 @@
1
+ export { createStore } from './createStore';
@@ -0,0 +1,4 @@
1
+ export type { StoreApi } from './types';
2
+ export { createStore } from './createStore';
3
+ export { createPersist, type PersistStorage } from './persist';
4
+ export { subscribeWithSelector } from './subscribeWithSelector';
@@ -0,0 +1,2 @@
1
+ export { createPersist } from './persist';
2
+ export type { PersistStorage } from './persist';
@@ -0,0 +1,30 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var logger = require('../utils/logger/logger.cjs');
7
+
8
+ /**
9
+ * Creates a persist middleware that saves the store state to the storage.
10
+ */
11
+ const createPersist = ({ name, version = 0, storage }) => (store) => {
12
+ store.subscribe((state) => {
13
+ storage.setItem(name, JSON.stringify({ state, version }));
14
+ });
15
+ const storedState = storage.getItem(name);
16
+ if (storedState) {
17
+ try {
18
+ const { state: storedStateData, version: storedVersion } = JSON.parse(storedState);
19
+ if (storedVersion === version) {
20
+ store.setState(storedStateData);
21
+ }
22
+ }
23
+ catch (error) {
24
+ logger.logger.error('Failed to load state from storage', error);
25
+ }
26
+ }
27
+ return store;
28
+ };
29
+
30
+ exports.createPersist = createPersist;
@@ -0,0 +1,15 @@
1
+ import { StoreApi } from '../types';
2
+ export type PersistStorage = {
3
+ getItem: (key: string) => string | null;
4
+ setItem: (key: string, value: string) => void;
5
+ };
6
+ type CreatePersistProps = {
7
+ name: string;
8
+ version?: number;
9
+ storage: PersistStorage;
10
+ };
11
+ /**
12
+ * Creates a persist middleware that saves the store state to the storage.
13
+ */
14
+ export declare const createPersist: ({ name, version, storage }: CreatePersistProps) => <TStore extends object>(store: StoreApi<TStore>) => StoreApi<TStore>;
15
+ export {};
@@ -0,0 +1,26 @@
1
+ 'use client'
2
+ import { logger } from '../utils/logger/logger.js';
3
+
4
+ /**
5
+ * Creates a persist middleware that saves the store state to the storage.
6
+ */
7
+ const createPersist = ({ name, version = 0, storage }) => (store) => {
8
+ store.subscribe((state) => {
9
+ storage.setItem(name, JSON.stringify({ state, version }));
10
+ });
11
+ const storedState = storage.getItem(name);
12
+ if (storedState) {
13
+ try {
14
+ const { state: storedStateData, version: storedVersion } = JSON.parse(storedState);
15
+ if (storedVersion === version) {
16
+ store.setState(storedStateData);
17
+ }
18
+ }
19
+ catch (error) {
20
+ logger.error('Failed to load state from storage', error);
21
+ }
22
+ }
23
+ return store;
24
+ };
25
+
26
+ export { createPersist };
@@ -0,0 +1 @@
1
+ export { shallow } from './shallow';
@@ -0,0 +1,53 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ /**
7
+ * Shallow compare two objects
8
+ * source: https://github.com/pmndrs/zustand/blob/main/src/vanilla/shallow.ts
9
+ */
10
+ const shallow = (objA, objB) => {
11
+ if (Object.is(objA, objB)) {
12
+ return true;
13
+ }
14
+ if (typeof objA !== 'object' ||
15
+ objA === null ||
16
+ typeof objB !== 'object' ||
17
+ objB === null) {
18
+ return false;
19
+ }
20
+ if (objA instanceof Map && objB instanceof Map) {
21
+ if (objA.size !== objB.size)
22
+ return false;
23
+ for (const [key, value] of objA) {
24
+ if (!Object.is(value, objB.get(key))) {
25
+ return false;
26
+ }
27
+ }
28
+ return true;
29
+ }
30
+ if (objA instanceof Set && objB instanceof Set) {
31
+ if (objA.size !== objB.size)
32
+ return false;
33
+ for (const value of objA) {
34
+ if (!objB.has(value)) {
35
+ return false;
36
+ }
37
+ }
38
+ return true;
39
+ }
40
+ const keysA = Object.keys(objA);
41
+ if (keysA.length !== Object.keys(objB).length) {
42
+ return false;
43
+ }
44
+ for (const keyA of keysA) {
45
+ if (!Object.prototype.hasOwnProperty.call(objB, keyA) ||
46
+ !Object.is(objA[keyA], objB[keyA])) {
47
+ return false;
48
+ }
49
+ }
50
+ return true;
51
+ };
52
+
53
+ exports.shallow = shallow;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Shallow compare two objects
3
+ * source: https://github.com/pmndrs/zustand/blob/main/src/vanilla/shallow.ts
4
+ */
5
+ export declare const shallow: <T>(objA: T, objB: T) => boolean;
@@ -0,0 +1,49 @@
1
+ 'use client'
2
+ /**
3
+ * Shallow compare two objects
4
+ * source: https://github.com/pmndrs/zustand/blob/main/src/vanilla/shallow.ts
5
+ */
6
+ const shallow = (objA, objB) => {
7
+ if (Object.is(objA, objB)) {
8
+ return true;
9
+ }
10
+ if (typeof objA !== 'object' ||
11
+ objA === null ||
12
+ typeof objB !== 'object' ||
13
+ objB === null) {
14
+ return false;
15
+ }
16
+ if (objA instanceof Map && objB instanceof Map) {
17
+ if (objA.size !== objB.size)
18
+ return false;
19
+ for (const [key, value] of objA) {
20
+ if (!Object.is(value, objB.get(key))) {
21
+ return false;
22
+ }
23
+ }
24
+ return true;
25
+ }
26
+ if (objA instanceof Set && objB instanceof Set) {
27
+ if (objA.size !== objB.size)
28
+ return false;
29
+ for (const value of objA) {
30
+ if (!objB.has(value)) {
31
+ return false;
32
+ }
33
+ }
34
+ return true;
35
+ }
36
+ const keysA = Object.keys(objA);
37
+ if (keysA.length !== Object.keys(objB).length) {
38
+ return false;
39
+ }
40
+ for (const keyA of keysA) {
41
+ if (!Object.prototype.hasOwnProperty.call(objB, keyA) ||
42
+ !Object.is(objA[keyA], objB[keyA])) {
43
+ return false;
44
+ }
45
+ }
46
+ return true;
47
+ };
48
+
49
+ export { shallow };
@@ -0,0 +1 @@
1
+ export { subscribeWithSelector } from './subscribeWithSelector';
@@ -0,0 +1,38 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var shallow = require('../shallow/shallow.cjs');
7
+
8
+ /**
9
+ * Subscribes to a specific slice of the store state and invokes a callback whenever the slice changes.
10
+ * @template TStore - The type of the store.
11
+ * @template StateSlice - The type of the selected slice of the store state.
12
+ * @param {StoreApi<TStore>} store - The store object.
13
+ * @param {(state: TStore) => StateSlice} selector - A function that selects the desired slice of the store state.
14
+ * @returns {SliceSubscription<StateSlice>} - A function that can be used to subscribe.
15
+ * @example
16
+ * const store = createStore<{ counter: number; }>(() => ({ counter: 0 }));
17
+ * const subscribeToCounter = subscribeWithSelector(
18
+ * store,
19
+ * (state) => state.counter,
20
+ * );
21
+ * const unsubscribe = subscribeToCounter((counter) => {
22
+ * console.log('Counter changed:', counter);
23
+ * });
24
+ * // To unsubscribe, call the returned function
25
+ * unsubscribe();
26
+ */
27
+ const subscribeWithSelector = (store, selector) => (callback) => {
28
+ let lastSlice = selector(store.getState());
29
+ return store.subscribe((state) => {
30
+ const nextSlice = selector(state);
31
+ if (shallow.shallow(lastSlice, nextSlice) === false) {
32
+ lastSlice = nextSlice;
33
+ callback(nextSlice);
34
+ }
35
+ });
36
+ };
37
+
38
+ exports.subscribeWithSelector = subscribeWithSelector;
@@ -0,0 +1,23 @@
1
+ import { StoreApi } from '../types';
2
+ type SliceSubscription<StateSlice> = (subscription: (state: StateSlice) => void) => () => void;
3
+ /**
4
+ * Subscribes to a specific slice of the store state and invokes a callback whenever the slice changes.
5
+ * @template TStore - The type of the store.
6
+ * @template StateSlice - The type of the selected slice of the store state.
7
+ * @param {StoreApi<TStore>} store - The store object.
8
+ * @param {(state: TStore) => StateSlice} selector - A function that selects the desired slice of the store state.
9
+ * @returns {SliceSubscription<StateSlice>} - A function that can be used to subscribe.
10
+ * @example
11
+ * const store = createStore<{ counter: number; }>(() => ({ counter: 0 }));
12
+ * const subscribeToCounter = subscribeWithSelector(
13
+ * store,
14
+ * (state) => state.counter,
15
+ * );
16
+ * const unsubscribe = subscribeToCounter((counter) => {
17
+ * console.log('Counter changed:', counter);
18
+ * });
19
+ * // To unsubscribe, call the returned function
20
+ * unsubscribe();
21
+ */
22
+ export declare const subscribeWithSelector: <TStore extends object, StateSlice>(store: StoreApi<TStore>, selector: (state: TStore) => StateSlice) => SliceSubscription<StateSlice>;
23
+ export {};
@@ -0,0 +1,34 @@
1
+ 'use client'
2
+ import { shallow } from '../shallow/shallow.js';
3
+
4
+ /**
5
+ * Subscribes to a specific slice of the store state and invokes a callback whenever the slice changes.
6
+ * @template TStore - The type of the store.
7
+ * @template StateSlice - The type of the selected slice of the store state.
8
+ * @param {StoreApi<TStore>} store - The store object.
9
+ * @param {(state: TStore) => StateSlice} selector - A function that selects the desired slice of the store state.
10
+ * @returns {SliceSubscription<StateSlice>} - A function that can be used to subscribe.
11
+ * @example
12
+ * const store = createStore<{ counter: number; }>(() => ({ counter: 0 }));
13
+ * const subscribeToCounter = subscribeWithSelector(
14
+ * store,
15
+ * (state) => state.counter,
16
+ * );
17
+ * const unsubscribe = subscribeToCounter((counter) => {
18
+ * console.log('Counter changed:', counter);
19
+ * });
20
+ * // To unsubscribe, call the returned function
21
+ * unsubscribe();
22
+ */
23
+ const subscribeWithSelector = (store, selector) => (callback) => {
24
+ let lastSlice = selector(store.getState());
25
+ return store.subscribe((state) => {
26
+ const nextSlice = selector(state);
27
+ if (shallow(lastSlice, nextSlice) === false) {
28
+ lastSlice = nextSlice;
29
+ callback(nextSlice);
30
+ }
31
+ });
32
+ };
33
+
34
+ export { subscribeWithSelector };
@@ -0,0 +1,6 @@
1
+ export type StoreApi<T extends object> = {
2
+ getState: () => T;
3
+ setState: (partialState: Partial<T>) => void;
4
+ getInitialState: () => T;
5
+ subscribe: (subscription: (state: T) => void) => () => void;
6
+ };
@@ -0,0 +1 @@
1
+ export { logger } from './logger';
@@ -0,0 +1,10 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var logger$1 = require('@dynamic-labs/logger');
7
+
8
+ const logger = new logger$1.Logger('@dynamic-labs/logger');
9
+
10
+ exports.logger = logger;
@@ -0,0 +1,2 @@
1
+ import { Logger } from '@dynamic-labs/logger';
2
+ export declare const logger: Logger;
@@ -0,0 +1,6 @@
1
+ 'use client'
2
+ import { Logger } from '@dynamic-labs/logger';
3
+
4
+ const logger = new Logger('@dynamic-labs/logger');
5
+
6
+ export { logger };