seitu 0.5.1 → 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 CHANGED
@@ -264,26 +264,25 @@ function I(e = {}) {
264
264
  }
265
265
  //#endregion
266
266
  //#region src/react/hooks.ts
267
- function L(t, n) {
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
- if (!s.get || !s.subscribe) throw Error("Subscription is not valid. It must have a get and subscribe method.");
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
- }, t = s.subscribe(e);
276
+ }, n = s.subscribe(e);
278
277
  return e(s.get()), () => {
279
- t(), s.destroy?.();
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 R({ value: e, selector: t, children: n }) {
286
- return n(L(e, { selector: t }));
284
+ function z({ value: e, selector: t, children: n }) {
285
+ return n(R(e, { selector: t }));
287
286
  }
288
287
  //#endregion
289
- export { R as Subscription, L as useSubscription };
288
+ export { z as Subscription, R as useSubscription };
@@ -6,21 +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
- /**
10
- * If true, the value will be cleared if the validation fails.
11
- *
12
- * @default true
13
- */
14
- clearOnValidationFailure?: boolean;
9
+ onValidationError?: (props: {
10
+ issues: StandardSchemaV1.Issue[];
11
+ value: unknown;
12
+ }) => void;
15
13
  }
16
14
  export interface WebStorageValueOptionsWithSchema<S extends StandardSchemaV1<unknown>> {
17
15
  schema: S;
18
16
  key: string;
19
17
  defaultValue: StandardSchemaV1.InferOutput<S>;
20
- /**
21
- * If true, the value will be cleared if the validation fails.
22
- */
23
- clearOnValidationFailure?: boolean;
18
+ onValidationError?: (props: {
19
+ issues: StandardSchemaV1.Issue[];
20
+ value: unknown;
21
+ }) => void | StandardSchemaV1.InferOutput<S>;
24
22
  }
25
23
  export declare function createWebStorageValue<Storage extends WebStorage<any>, K extends keyof Storage['~']['output']>(options: WebStorageValueOptionsWithStorage<Storage, K>): WebStorageValue<Storage['~']['output'][K]>;
26
24
  export declare function createWebStorageValue<S extends StandardSchemaV1<unknown>>(options: WebStorageValueOptionsWithSchema<S> & {
@@ -1,12 +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;
4
- /**
5
- * If true, the stored value will be cleared if the validation fails.
6
- *
7
- * @default true
8
- */
9
- clearOnValidationFailure?: boolean;
5
+ onValidationError?: <K extends keyof S>(props: {
6
+ issues: StandardSchemaV1.Issue[];
7
+ key: K;
8
+ value: unknown;
9
+ }) => void | StandardSchemaV1.InferOutput<S[K]>;
10
10
  }
11
11
  export interface WebStorage<O extends Record<string, unknown>> extends SchemaStore<O> {
12
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 { kind: r, keyTransform: i, defaultValues: a, schemas: o, clearOnValidationFailure: s } = n, c = !1, l = t({
21
- defaultValues: a,
22
- schemas: o,
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 a;
26
- let t = window[r], n = { ...a };
27
- for (let r in n) {
28
- let c = t.getItem(i ? i(r) : r);
29
- if (c === null) {
30
- n[r] = a[r];
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 l = e(c), u = o[r]["~standard"].validate(l);
34
- if (u instanceof Promise) throw TypeError("[createWebSchemaStore] Validation schema should not return a Promise.");
35
- u.issues && (s ? t.removeItem(i ? i(r) : r) : console.warn(JSON.stringify(u.issues, null, 2), { cause: u.issues })), n[r] = u.issues ? a[r] : u.value;
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 n;
49
+ return r;
38
50
  },
39
51
  set: (e) => {
40
52
  if (typeof window > "u") return;
41
- let t = window[r];
42
- c = !0, Object.entries(e).forEach(([e, n]) => {
43
- let r = typeof n == "string" ? n : JSON.stringify(n);
44
- t.setItem(i ? i(e) : e, r);
45
- }), window.dispatchEvent(new Event("storage")), c = !1;
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
- }), u = () => {
49
- c || l["~"].notify();
60
+ }), a = () => {
61
+ r || i["~"].notify();
50
62
  };
51
- return typeof window < "u" && window.addEventListener("storage", u), {
52
- ...l,
63
+ return typeof window < "u" && window.addEventListener("storage", a), {
64
+ ...i,
53
65
  destroy: () => {
54
- l.destroy?.(), typeof window < "u" && window.removeEventListener("storage", u);
66
+ i.destroy?.(), typeof window < "u" && window.removeEventListener("storage", a);
55
67
  },
56
68
  "~": {
57
- kind: r,
58
- ...l["~"]
69
+ kind: n.kind,
70
+ ...i["~"]
59
71
  }
60
72
  };
61
73
  }
@@ -76,17 +88,32 @@ function o(t) {
76
88
  if (!("schema" in t || "storage" in t)) throw Error(`[${a}] Either schema or storage must be provided`);
77
89
  let o = ("schema" in t ? t.defaultValue : t.storage.getDefaultValue(t.key)) ?? null, { subscribe: s, notify: c } = n(), l = () => {
78
90
  if (typeof window > "u") return o;
79
- let n = window[r], i = n.getItem(t.key);
80
- if (i === null) return o;
81
- let a = e(i);
91
+ let n = window[r].getItem(t.key);
92
+ if (n === null) return o;
93
+ let i = e(n);
82
94
  try {
83
95
  if ("schema" in t) {
84
- let e = t.schema["~standard"].validate(a);
96
+ let e = t.schema["~standard"].validate(i);
85
97
  if (e instanceof Promise) throw TypeError("Validation schema should not return a Promise.");
86
- return e.issues ? (t.clearOnValidationFailure ? n.removeItem(t.key) : console.warn(JSON.stringify(e.issues, null, 2), { cause: e.issues }), o) : e.value;
87
- } else return a;
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;
114
+ } else return i;
88
115
  } catch {
89
- return o !== void 0 && typeof o != "string" ? o : a;
116
+ return o !== void 0 && typeof o != "string" ? o : i;
90
117
  }
91
118
  }, u = (e) => {
92
119
  i || e.key === t.key && c();
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "seitu",
3
3
  "displayName": "Seitu",
4
4
  "type": "module",
5
- "version": "0.5.1",
5
+ "version": "0.5.2",
6
6
  "private": false,
7
7
  "author": "Valerii Strilets",
8
8
  "license": "MIT",