seitu 0.5.0 → 0.5.2
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/react.js +9 -10
- package/dist/web/web-storage-value.d.ts +8 -0
- package/dist/web/web-storage.d.ts +6 -0
- package/dist/web.js +53 -26
- package/package.json +1 -1
package/dist/react.js
CHANGED
|
@@ -264,26 +264,25 @@ function I(e = {}) {
|
|
|
264
264
|
}
|
|
265
265
|
//#endregion
|
|
266
266
|
//#region src/react/hooks.ts
|
|
267
|
-
|
|
267
|
+
var L = typeof window < "u" ? e.useLayoutEffect : e.useEffect;
|
|
268
|
+
function R(t, n) {
|
|
268
269
|
let { selector: r, deps: i = [] } = n ?? {}, a = typeof t == "function" ? t : () => t, o = e.useRef(a);
|
|
269
270
|
o.current = a;
|
|
270
|
-
let s = e.useMemo(() => o.current(), i);
|
|
271
|
-
|
|
272
|
-
let [c, l] = e.useState(() => r ? r(s.get()) : s.get()), u = e.useEffectEvent(() => c);
|
|
273
|
-
return e.useEffect(() => {
|
|
271
|
+
let s = e.useMemo(() => o.current(), i), [c, l] = e.useState(() => r ? r(s.get()) : s.get()), u = e.useEffectEvent(() => c);
|
|
272
|
+
return L(() => {
|
|
274
273
|
let e = (e) => {
|
|
275
274
|
let t = r ? r(e) : e;
|
|
276
275
|
F(t, u()) || l(t);
|
|
277
|
-
},
|
|
276
|
+
}, n = s.subscribe(e);
|
|
278
277
|
return e(s.get()), () => {
|
|
279
|
-
|
|
278
|
+
n(), typeof t == "function" && s.destroy?.();
|
|
280
279
|
};
|
|
281
280
|
}, [s, r]), c;
|
|
282
281
|
}
|
|
283
282
|
//#endregion
|
|
284
283
|
//#region src/react/components.tsx
|
|
285
|
-
function
|
|
286
|
-
return n(
|
|
284
|
+
function z({ value: e, selector: t, children: n }) {
|
|
285
|
+
return n(R(e, { selector: t }));
|
|
287
286
|
}
|
|
288
287
|
//#endregion
|
|
289
|
-
export {
|
|
288
|
+
export { z as Subscription, R as useSubscription };
|
|
@@ -6,11 +6,19 @@ export interface WebStorageValue<V> extends Subscribable<V>, Readable<V>, Writab
|
|
|
6
6
|
export interface WebStorageValueOptionsWithStorage<Storage extends WebStorage<any>, K extends keyof Storage['~']['output']> {
|
|
7
7
|
storage: Storage;
|
|
8
8
|
key: K;
|
|
9
|
+
onValidationError?: (props: {
|
|
10
|
+
issues: StandardSchemaV1.Issue[];
|
|
11
|
+
value: unknown;
|
|
12
|
+
}) => void;
|
|
9
13
|
}
|
|
10
14
|
export interface WebStorageValueOptionsWithSchema<S extends StandardSchemaV1<unknown>> {
|
|
11
15
|
schema: S;
|
|
12
16
|
key: string;
|
|
13
17
|
defaultValue: StandardSchemaV1.InferOutput<S>;
|
|
18
|
+
onValidationError?: (props: {
|
|
19
|
+
issues: StandardSchemaV1.Issue[];
|
|
20
|
+
value: unknown;
|
|
21
|
+
}) => void | StandardSchemaV1.InferOutput<S>;
|
|
14
22
|
}
|
|
15
23
|
export declare function createWebStorageValue<Storage extends WebStorage<any>, K extends keyof Storage['~']['output']>(options: WebStorageValueOptionsWithStorage<Storage, K>): WebStorageValue<Storage['~']['output'][K]>;
|
|
16
24
|
export declare function createWebStorageValue<S extends StandardSchemaV1<unknown>>(options: WebStorageValueOptionsWithSchema<S> & {
|
|
@@ -1,6 +1,12 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
1
2
|
import type { SchemaStore, SchemaStoreOptions, SchemaStoreOutput, SchemaStoreSchema } from '../core/index';
|
|
2
3
|
export interface WebStorageOptions<S extends SchemaStoreSchema> extends Omit<SchemaStoreOptions<S>, 'provider'> {
|
|
3
4
|
keyTransform?: (key: keyof S) => string;
|
|
5
|
+
onValidationError?: <K extends keyof S>(props: {
|
|
6
|
+
issues: StandardSchemaV1.Issue[];
|
|
7
|
+
key: K;
|
|
8
|
+
value: unknown;
|
|
9
|
+
}) => void | StandardSchemaV1.InferOutput<S[K]>;
|
|
4
10
|
}
|
|
5
11
|
export interface WebStorage<O extends Record<string, unknown>> extends SchemaStore<O> {
|
|
6
12
|
'~': {
|
package/dist/web.js
CHANGED
|
@@ -17,45 +17,57 @@ function r() {
|
|
|
17
17
|
//#endregion
|
|
18
18
|
//#region src/web/web-storage.ts
|
|
19
19
|
function i(n) {
|
|
20
|
-
let
|
|
21
|
-
defaultValues:
|
|
22
|
-
schemas:
|
|
20
|
+
let r = !1, i = t({
|
|
21
|
+
defaultValues: n.defaultValues,
|
|
22
|
+
schemas: n.schemas,
|
|
23
23
|
provider: {
|
|
24
24
|
get: () => {
|
|
25
|
-
if (typeof window > "u") return
|
|
26
|
-
let t = window[
|
|
27
|
-
for (let
|
|
28
|
-
let
|
|
29
|
-
if (
|
|
30
|
-
|
|
25
|
+
if (typeof window > "u") return n.defaultValues;
|
|
26
|
+
let t = window[n.kind], r = { ...n.defaultValues };
|
|
27
|
+
for (let i in r) {
|
|
28
|
+
let a = t.getItem(n.keyTransform ? n.keyTransform(i) : i);
|
|
29
|
+
if (a === null) {
|
|
30
|
+
r[i] = n.defaultValues[i];
|
|
31
31
|
continue;
|
|
32
32
|
}
|
|
33
|
-
let
|
|
34
|
-
if (
|
|
35
|
-
|
|
33
|
+
let o = e(a), s = n.schemas[i]["~standard"].validate(o);
|
|
34
|
+
if (s instanceof Promise) throw TypeError("[createWebSchemaStore] Validation schema should not return a Promise.");
|
|
35
|
+
if (s.issues) if (n.onValidationError) {
|
|
36
|
+
let e = n.onValidationError({
|
|
37
|
+
issues: [...s.issues],
|
|
38
|
+
key: i,
|
|
39
|
+
value: o
|
|
40
|
+
});
|
|
41
|
+
if (e !== void 0) {
|
|
42
|
+
let t = n.schemas[i]["~standard"].validate(e);
|
|
43
|
+
if (t instanceof Promise) throw TypeError("Validation schema should not return a Promise.");
|
|
44
|
+
t.issues ? console.error("Returned value invalid, returned default value instead", JSON.stringify(t.issues, null, 2), { cause: t.issues }) : r[i] = t.value;
|
|
45
|
+
}
|
|
46
|
+
} else console.warn(JSON.stringify(s.issues, null, 2), { cause: s.issues }), r[i] = n.defaultValues[i];
|
|
47
|
+
else r[i] = s.value;
|
|
36
48
|
}
|
|
37
|
-
return
|
|
49
|
+
return r;
|
|
38
50
|
},
|
|
39
51
|
set: (e) => {
|
|
40
52
|
if (typeof window > "u") return;
|
|
41
|
-
let t = window[
|
|
42
|
-
|
|
43
|
-
let
|
|
44
|
-
t.setItem(
|
|
45
|
-
}), window.dispatchEvent(new Event("storage")),
|
|
53
|
+
let t = window[n.kind];
|
|
54
|
+
r = !0, Object.entries(e).forEach(([e, r]) => {
|
|
55
|
+
let i = typeof r == "string" ? r : JSON.stringify(r);
|
|
56
|
+
t.setItem(n.keyTransform ? n.keyTransform(e) : e, i);
|
|
57
|
+
}), window.dispatchEvent(new Event("storage")), r = !1;
|
|
46
58
|
}
|
|
47
59
|
}
|
|
48
|
-
}),
|
|
49
|
-
|
|
60
|
+
}), a = () => {
|
|
61
|
+
r || i["~"].notify();
|
|
50
62
|
};
|
|
51
|
-
return typeof window < "u" && window.addEventListener("storage",
|
|
52
|
-
...
|
|
63
|
+
return typeof window < "u" && window.addEventListener("storage", a), {
|
|
64
|
+
...i,
|
|
53
65
|
destroy: () => {
|
|
54
|
-
|
|
66
|
+
i.destroy?.(), typeof window < "u" && window.removeEventListener("storage", a);
|
|
55
67
|
},
|
|
56
68
|
"~": {
|
|
57
|
-
kind:
|
|
58
|
-
...
|
|
69
|
+
kind: n.kind,
|
|
70
|
+
...i["~"]
|
|
59
71
|
}
|
|
60
72
|
};
|
|
61
73
|
}
|
|
@@ -83,7 +95,22 @@ function o(t) {
|
|
|
83
95
|
if ("schema" in t) {
|
|
84
96
|
let e = t.schema["~standard"].validate(i);
|
|
85
97
|
if (e instanceof Promise) throw TypeError("Validation schema should not return a Promise.");
|
|
86
|
-
|
|
98
|
+
if (e.issues) {
|
|
99
|
+
if (t.onValidationError) {
|
|
100
|
+
let n = t.onValidationError({
|
|
101
|
+
issues: [...e.issues],
|
|
102
|
+
value: i
|
|
103
|
+
});
|
|
104
|
+
if (n !== void 0) {
|
|
105
|
+
let e = t.schema["~standard"].validate(n);
|
|
106
|
+
if (e instanceof Promise) throw TypeError("Validation schema should not return a Promise.");
|
|
107
|
+
if (e.issues) console.error("Returned value invalid, returned default value instead", JSON.stringify(e.issues, null, 2), { cause: e.issues });
|
|
108
|
+
else return e.value;
|
|
109
|
+
}
|
|
110
|
+
} else console.warn(JSON.stringify(e.issues, null, 2), { cause: e.issues });
|
|
111
|
+
return o;
|
|
112
|
+
}
|
|
113
|
+
return e.value;
|
|
87
114
|
} else return i;
|
|
88
115
|
} catch {
|
|
89
116
|
return o !== void 0 && typeof o != "string" ? o : i;
|