@tempots/dom 7.1.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.
- package/package.json +1 -1
- package/prop.d.ts +6 -6
- package/prop.js +14 -14
package/package.json
CHANGED
package/prop.d.ts
CHANGED
|
@@ -8,12 +8,6 @@ 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>;
|
|
17
11
|
/**
|
|
18
12
|
* Combines many into one using a merging function
|
|
19
13
|
*/
|
|
@@ -41,6 +35,12 @@ export declare class Signal<T> {
|
|
|
41
35
|
export declare class Prop<T> extends Signal<T> {
|
|
42
36
|
static isProp<T = unknown>(x: unknown): x is Prop<T>;
|
|
43
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>;
|
|
44
44
|
readonly [$isProp] = true;
|
|
45
45
|
readonly set: (value: T) => void;
|
|
46
46
|
readonly update: (f: (value: T) => T) => void;
|
package/prop.js
CHANGED
|
@@ -15,20 +15,6 @@ 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
|
-
}
|
|
32
18
|
/**
|
|
33
19
|
* Combines many into one using a merging function
|
|
34
20
|
*/
|
|
@@ -202,6 +188,20 @@ export class Prop extends Signal {
|
|
|
202
188
|
static of(value) {
|
|
203
189
|
return new Prop(value);
|
|
204
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
|
+
}
|
|
205
205
|
[$isProp] = true;
|
|
206
206
|
set = (value) => {
|
|
207
207
|
if (this._value === value)
|