nexa-persistence 0.6.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/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/persistSignal.d.ts +7 -0
- package/dist/persistSignal.js +22 -0
- package/package.json +28 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './persistSignal.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './persistSignal.js';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Signal } from 'nexa-reactivity';
|
|
2
|
+
export interface PersistenceOptions {
|
|
3
|
+
storage?: Storage;
|
|
4
|
+
serialize?: (value: any) => string;
|
|
5
|
+
deserialize?: (value: string) => any;
|
|
6
|
+
}
|
|
7
|
+
export declare function persistSignal<T>(signal: Signal<T>, key: string, options?: PersistenceOptions): Signal<T>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { effect } from 'nexa-reactivity';
|
|
2
|
+
export function persistSignal(signal, key, options = {}) {
|
|
3
|
+
const storage = options.storage || window.localStorage;
|
|
4
|
+
const serialize = options.serialize || JSON.stringify;
|
|
5
|
+
const deserialize = options.deserialize || JSON.parse;
|
|
6
|
+
// Load initial value
|
|
7
|
+
const saved = storage.getItem(key);
|
|
8
|
+
if (saved !== null) {
|
|
9
|
+
try {
|
|
10
|
+
signal.value = deserialize(saved);
|
|
11
|
+
}
|
|
12
|
+
catch (e) {
|
|
13
|
+
console.error(`[Nexa Persistence] Failed to deserialize key "${key}":`, e);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
// Save on changes
|
|
17
|
+
const e = effect(() => {
|
|
18
|
+
storage.setItem(key, serialize(signal.value));
|
|
19
|
+
});
|
|
20
|
+
signal.dispose = () => e.dispose();
|
|
21
|
+
return signal;
|
|
22
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nexa-persistence",
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"nexa-reactivity": "0.6.0"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsc",
|
|
26
|
+
"clean": "rm -rf dist"
|
|
27
|
+
}
|
|
28
|
+
}
|