@nexus-state/persist 0.1.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/.eslintrc.js ADDED
@@ -0,0 +1,10 @@
1
+ module.exports = {
2
+ root: true,
3
+ extends: [
4
+ '../../.eslintrc.js'
5
+ ],
6
+ parserOptions: {
7
+ project: './tsconfig.json',
8
+ tsconfigRootDir: __dirname,
9
+ },
10
+ };
@@ -0,0 +1,5 @@
1
+ ⠙
2
+ > @nexus-state/persist@0.1.0 build
3
+ > tsc
4
+
5
+ ⠙⠙
@@ -0,0 +1,5 @@
1
+ ⠙
2
+ > @nexus-state/persist@0.0.0 lint
3
+ > eslint . --ext .ts
4
+
5
+ ⠙⠙
package/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ # @nexus-state/persist
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - feat: Add family atoms support
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies
12
+ - @nexus-state/core@0.1.0
@@ -0,0 +1,51 @@
1
+ import { Atom, Store } from '@nexus-state/core';
2
+ /**
3
+ * Represents a storage interface for persisting atom values.
4
+ * @typedef {Object} PersistStorage
5
+ * @property {Function} getItem - Function to get an item from storage
6
+ * @property {Function} setItem - Function to set an item in storage
7
+ * @property {Function} removeItem - Function to remove an item from storage
8
+ */
9
+ export type PersistStorage = {
10
+ getItem: (key: string) => string | null;
11
+ setItem: (key: string, value: string) => void;
12
+ removeItem: (key: string) => void;
13
+ };
14
+ /**
15
+ * Configuration options for the persist plugin.
16
+ * @typedef {Object} PersistConfig
17
+ * @property {string} key - The key to use for storing the atom's value
18
+ * @property {PersistStorage} storage - The storage implementation to use
19
+ * @property {Function} [serialize] - Function to serialize the value (defaults to JSON.stringify)
20
+ * @property {Function} [deserialize] - Function to deserialize the value (defaults to JSON.parse)
21
+ */
22
+ export type PersistConfig<T> = {
23
+ key: string;
24
+ storage: PersistStorage;
25
+ serialize?: (value: T) => string;
26
+ deserialize?: (value: string) => T;
27
+ };
28
+ /**
29
+ * Plugin to persist atom values to storage.
30
+ * @template T - The type of the atom's value
31
+ * @param {Atom<T>} atom - The atom to persist
32
+ * @param {PersistConfig<T>} config - Configuration options for the plugin
33
+ * @returns {Function} A function that applies the plugin to a store
34
+ * @example
35
+ * const store = createStore([
36
+ * persist(countAtom, {
37
+ * key: 'count',
38
+ * storage: localStorageStorage
39
+ * })
40
+ * ]);
41
+ */
42
+ export declare function persist<T>(atom: Atom<T>, config: PersistConfig<T>): (store: Store) => void;
43
+ /**
44
+ * Predefined localStorage storage implementation.
45
+ */
46
+ export declare const localStorageStorage: PersistStorage;
47
+ /**
48
+ * Predefined sessionStorage storage implementation.
49
+ */
50
+ export declare const sessionStorageStorage: PersistStorage;
51
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAEhD;;;;;;GAMG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,CAAC;IACxC,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAC9C,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;CACnC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI;IAC7B,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,cAAc,CAAC;IACxB,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,MAAM,CAAC;IACjC,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,CAAC,CAAC;CACpC,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CA0B1F;AAED;;GAEG;AACH,eAAO,MAAM,mBAAmB,EAAE,cAIjC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,qBAAqB,EAAE,cAInC,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Plugin to persist atom values to storage.
3
+ * @template T - The type of the atom's value
4
+ * @param {Atom<T>} atom - The atom to persist
5
+ * @param {PersistConfig<T>} config - Configuration options for the plugin
6
+ * @returns {Function} A function that applies the plugin to a store
7
+ * @example
8
+ * const store = createStore([
9
+ * persist(countAtom, {
10
+ * key: 'count',
11
+ * storage: localStorageStorage
12
+ * })
13
+ * ]);
14
+ */
15
+ export function persist(atom, config) {
16
+ return (store) => {
17
+ const { key, storage, serialize = JSON.stringify, deserialize = JSON.parse } = config;
18
+ // Attempt to restore the value from storage
19
+ const savedValue = storage.getItem(key);
20
+ if (savedValue !== null) {
21
+ try {
22
+ const deserializedValue = deserialize(savedValue);
23
+ store.set(atom, deserializedValue);
24
+ }
25
+ catch (error) {
26
+ console.error(`Failed to deserialize persisted value for atom ${key}:`, error);
27
+ }
28
+ }
29
+ // Subscribe to atom changes to save to storage
30
+ store.subscribe(atom, () => {
31
+ const value = store.get(atom);
32
+ try {
33
+ const serializedValue = serialize(value);
34
+ storage.setItem(key, serializedValue);
35
+ }
36
+ catch (error) {
37
+ console.error(`Failed to serialize value for atom ${key}:`, error);
38
+ }
39
+ });
40
+ };
41
+ }
42
+ /**
43
+ * Predefined localStorage storage implementation.
44
+ */
45
+ export const localStorageStorage = {
46
+ getItem: (key) => typeof localStorage !== 'undefined' ? localStorage.getItem(key) : null,
47
+ setItem: (key, value) => typeof localStorage !== 'undefined' && localStorage.setItem(key, value),
48
+ removeItem: (key) => typeof localStorage !== 'undefined' && localStorage.removeItem(key),
49
+ };
50
+ /**
51
+ * Predefined sessionStorage storage implementation.
52
+ */
53
+ export const sessionStorageStorage = {
54
+ getItem: (key) => typeof sessionStorage !== 'undefined' ? sessionStorage.getItem(key) : null,
55
+ setItem: (key, value) => typeof sessionStorage !== 'undefined' && sessionStorage.setItem(key, value),
56
+ removeItem: (key) => typeof sessionStorage !== 'undefined' && sessionStorage.removeItem(key),
57
+ };
58
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AA+BA;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,OAAO,CAAI,IAAa,EAAE,MAAwB;IAChE,OAAO,CAAC,KAAY,EAAE,EAAE;QACtB,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,SAAS,GAAG,IAAI,CAAC,SAAS,EAAE,WAAW,GAAG,IAAI,CAAC,KAAK,EAAE,GAAG,MAAM,CAAC;QAEtF,4CAA4C;QAC5C,MAAM,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACxC,IAAI,UAAU,KAAK,IAAI,EAAE,CAAC;YACxB,IAAI,CAAC;gBACH,MAAM,iBAAiB,GAAG,WAAW,CAAC,UAAU,CAAC,CAAC;gBAClD,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,iBAAiB,CAAC,CAAC;YACrC,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO,CAAC,KAAK,CAAC,kDAAkD,GAAG,GAAG,EAAE,KAAK,CAAC,CAAC;YACjF,CAAC;QACH,CAAC;QAED,+CAA+C;QAC/C,KAAK,CAAC,SAAS,CAAC,IAAI,EAAE,GAAG,EAAE;YACzB,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YAC9B,IAAI,CAAC;gBACH,MAAM,eAAe,GAAG,SAAS,CAAC,KAA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Persist plugin for nexus-state
2
+ import { Atom, Store } from '@nexus-state/core';
3
+
4
+ /**
5
+ * Represents a storage interface for persisting atom values.
6
+ * @typedef {Object} PersistStorage
7
+ * @property {Function} getItem - Function to get an item from storage
8
+ * @property {Function} setItem - Function to set an item in storage
9
+ * @property {Function} removeItem - Function to remove an item from storage
10
+ */
11
+ export type PersistStorage = {
12
+ getItem: (key: string) => string | null;
13
+ setItem: (key: string, value: string) => void;
14
+ removeItem: (key: string) => void;
15
+ };
16
+
17
+ /**
18
+ * Configuration options for the persist plugin.
19
+ * @typedef {Object} PersistConfig
20
+ * @property {string} key - The key to use for storing the atom's value
21
+ * @property {PersistStorage} storage - The storage implementation to use
22
+ * @property {Function} [serialize] - Function to serialize the value (defaults to JSON.stringify)
23
+ * @property {Function} [deserialize] - Function to deserialize the value (defaults to JSON.parse)
24
+ */
25
+ export type PersistConfig<T> = {
26
+ key: string;
27
+ storage: PersistStorage;
28
+ serialize?: (value: T) => string;
29
+ deserialize?: (value: string) => T;
30
+ };
31
+
32
+ /**
33
+ * Plugin to persist atom values to storage.
34
+ * @template T - The type of the atom's value
35
+ * @param {Atom<T>} atom - The atom to persist
36
+ * @param {PersistConfig<T>} config - Configuration options for the plugin
37
+ * @returns {Function} A function that applies the plugin to a store
38
+ * @example
39
+ * const store = createStore([
40
+ * persist(countAtom, {
41
+ * key: 'count',
42
+ * storage: localStorageStorage
43
+ * })
44
+ * ]);
45
+ */
46
+ export function persist<T>(atom: Atom<T>, config: PersistConfig<T>): (store: Store) => void {
47
+ return (store: Store) => {
48
+ const { key, storage, serialize = JSON.stringify, deserialize = JSON.parse } = config;
49
+
50
+ // Attempt to restore the value from storage
51
+ const savedValue = storage.getItem(key);
52
+ if (savedValue !== null) {
53
+ try {
54
+ const deserializedValue = deserialize(savedValue);
55
+ store.set(atom, deserializedValue);
56
+ } catch (error) {
57
+ console.error(`Failed to deserialize persisted value for atom ${key}:`, error);
58
+ }
59
+ }
60
+
61
+ // Subscribe to atom changes to save to storage
62
+ store.subscribe(atom, () => {
63
+ const value = store.get(atom);
64
+ try {
65
+ const serializedValue = serialize(value);
66
+ storage.setItem(key, serializedValue);
67
+ } catch (error) {
68
+ console.error(`Failed to serialize value for atom ${key}:`, error);
69
+ }
70
+ });
71
+ };
72
+ }
73
+
74
+ /**
75
+ * Predefined localStorage storage implementation.
76
+ */
77
+ export const localStorageStorage: PersistStorage = {
78
+ getItem: (key) => typeof localStorage !== 'undefined' ? localStorage.getItem(key) : null,
79
+ setItem: (key, value) => typeof localStorage !== 'undefined' && localStorage.setItem(key, value),
80
+ removeItem: (key) => typeof localStorage !== 'undefined' && localStorage.removeItem(key),
81
+ };
82
+
83
+ /**
84
+ * Predefined sessionStorage storage implementation.
85
+ */
86
+ export const sessionStorageStorage: PersistStorage = {
87
+ getItem: (key) => typeof sessionStorage !== 'undefined' ? sessionStorage.getItem(key) : null,
88
+ setItem: (key, value) => typeof sessionStorage !== 'undefined' && sessionStorage.setItem(key, value),
89
+ removeItem: (key) => typeof sessionStorage !== 'undefined' && sessionStorage.removeItem(key),
90
+ };
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@nexus-state/persist",
3
+ "version": "0.1.0",
4
+ "main": "index.ts",
5
+ "scripts": {
6
+ "build": "tsc",
7
+ "dev": "tsc -w",
8
+ "test": "vitest",
9
+ "lint": "eslint . --ext .ts"
10
+ },
11
+ "dependencies": {
12
+ "@nexus-state/core": "*"
13
+ },
14
+ "devDependencies": {
15
+ "typescript": "^5.0.0",
16
+ "vitest": "^0.34.0",
17
+ "eslint": "^8.40.0"
18
+ }
19
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "ESNext",
5
+ "moduleResolution": "node",
6
+ "declaration": true,
7
+ "declarationMap": true,
8
+ "sourceMap": true,
9
+ "outDir": "./dist",
10
+ "rootDir": "./",
11
+ "strict": true,
12
+ "esModuleInterop": true,
13
+ "skipLibCheck": true,
14
+ "forceConsistentCasingInFileNames": true,
15
+ "resolveJsonModule": true
16
+ },
17
+ "include": ["**/*.ts"],
18
+ "exclude": ["node_modules", "dist"]
19
+ }