@tempots/dom 7.0.0 → 7.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.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/prop.d.ts +6 -0
  3. package/prop.js +14 -0
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@tempots/dom",
3
3
  "main": "index.js",
4
4
  "types": "index.d.ts",
5
- "version": "7.0.0",
5
+ "version": "7.1.0",
6
6
  "scripts": {
7
7
  "watch": "tsc --watch",
8
8
  "build": "tsc",
package/prop.d.ts CHANGED
@@ -8,6 +8,12 @@ export declare class Signal<T> {
8
8
  static ofValue<T>(value: Value<T> | null | undefined): Signal<T> | undefined;
9
9
  static wrap<T>(value: T | Signal<T>): Signal<T>;
10
10
  static isSignal<T = unknown>(x: unknown): x is Signal<T>;
11
+ static ofStorage<T>(key: string, defaultValue: T, store: {
12
+ getItem: (key: string) => string | null;
13
+ setItem: (key: string, value: string) => void;
14
+ }, serialize?: (v: T) => string, deserilize?: (v: string) => T): Signal<T>;
15
+ static ofLocalStorage<T>(key: string, defaultValue: T, serialize?: (v: T) => string, deserilize?: (v: string) => T): Signal<T>;
16
+ static ofSessionStorage<T>(key: string, defaultValue: T, serialize?: (v: T) => string, deserilize?: (v: string) => T): Signal<T>;
11
17
  /**
12
18
  * Combines many into one using a merging function
13
19
  */
package/prop.js CHANGED
@@ -15,6 +15,20 @@ export class Signal {
15
15
  const s = x;
16
16
  return s != null && typeof s.get === 'function' && typeof s.subscribe === 'function';
17
17
  }
18
+ static ofStorage(key, defaultValue, store, serialize = JSON.stringify, deserilize = JSON.parse) {
19
+ const initialValue = store.getItem(key);
20
+ const prop = new Prop(initialValue !== null ? deserilize(initialValue) : defaultValue);
21
+ prop.subscribe((value) => {
22
+ store.setItem(key, serialize(value));
23
+ });
24
+ return prop;
25
+ }
26
+ static ofLocalStorage(key, defaultValue, serialize = JSON.stringify, deserilize = JSON.parse) {
27
+ return Signal.ofStorage(key, defaultValue, window.localStorage, serialize, deserilize);
28
+ }
29
+ static ofSessionStorage(key, defaultValue, serialize = JSON.stringify, deserilize = JSON.parse) {
30
+ return Signal.ofStorage(key, defaultValue, window.sessionStorage, serialize, deserilize);
31
+ }
18
32
  /**
19
33
  * Combines many into one using a merging function
20
34
  */