@tempots/dom 7.0.0 → 7.1.1

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.1",
6
6
  "scripts": {
7
7
  "watch": "tsc --watch",
8
8
  "build": "tsc",
package/prop.d.ts CHANGED
@@ -35,6 +35,12 @@ export declare class Signal<T> {
35
35
  export declare class Prop<T> extends Signal<T> {
36
36
  static isProp<T = unknown>(x: unknown): x is Prop<T>;
37
37
  static of<T>(value: T): Prop<T>;
38
+ static ofStorage<T>(key: string, defaultValue: T, store: {
39
+ getItem: (key: string) => string | null;
40
+ setItem: (key: string, value: string) => void;
41
+ }, serialize?: (v: T) => string, deserilize?: (v: string) => T): Prop<T>;
42
+ static ofLocalStorage<T>(key: string, defaultValue: T, serialize?: (v: T) => string, deserilize?: (v: string) => T): Prop<T>;
43
+ static ofSessionStorage<T>(key: string, defaultValue: T, serialize?: (v: T) => string, deserilize?: (v: string) => T): Prop<T>;
38
44
  readonly [$isProp] = true;
39
45
  readonly set: (value: T) => void;
40
46
  readonly update: (f: (value: T) => T) => void;
package/prop.js CHANGED
@@ -188,6 +188,20 @@ export class Prop extends Signal {
188
188
  static of(value) {
189
189
  return new Prop(value);
190
190
  }
191
+ static ofStorage(key, defaultValue, store, serialize = JSON.stringify, deserilize = JSON.parse) {
192
+ const initialValue = store.getItem(key);
193
+ const prop = new Prop(initialValue !== null ? deserilize(initialValue) : defaultValue);
194
+ prop.subscribe((value) => {
195
+ store.setItem(key, serialize(value));
196
+ });
197
+ return prop;
198
+ }
199
+ static ofLocalStorage(key, defaultValue, serialize = JSON.stringify, deserilize = JSON.parse) {
200
+ return Prop.ofStorage(key, defaultValue, window.localStorage, serialize, deserilize);
201
+ }
202
+ static ofSessionStorage(key, defaultValue, serialize = JSON.stringify, deserilize = JSON.parse) {
203
+ return Prop.ofStorage(key, defaultValue, window.sessionStorage, serialize, deserilize);
204
+ }
191
205
  [$isProp] = true;
192
206
  set = (value) => {
193
207
  if (this._value === value)