seitu 0.10.4 → 0.10.5

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.
@@ -1,19 +1,21 @@
1
1
  import type { StandardSchemaV1 } from '@standard-schema/spec';
2
- import type { ValidationObjectSchemas, ValidationObjectSchemasOutput } from '../validate';
2
+ import type { ValidationSchemaErrorProps } from '../validate';
3
3
  import type { Readable, Subscribable, Writable } from './index';
4
- export interface SchemaStore<O extends Record<string, unknown>> extends Subscribable<O>, Readable<O>, Writable<Partial<O>, O> {
4
+ export interface SchemaStore<O extends Record<string, unknown>> extends Subscribable<O>, Readable<O>, Writable<O> {
5
5
  '~': {
6
6
  getDefaultValue: <K extends keyof O>(key: K) => O[K];
7
7
  } & Subscribable<O>['~'];
8
8
  }
9
- export interface SchemaStoreOptions<S extends Record<string, StandardSchemaV1>> {
10
- schemas: S;
11
- defaultValues: ValidationObjectSchemasOutput<S>;
9
+ export interface SchemaStoreOptions<O extends Record<string, unknown>> {
10
+ schema: StandardSchemaV1<unknown, O>;
11
+ defaultValue: O;
12
12
  /**
13
- * The provider to use for the schema store. If not provided, the schema store will
14
- * use an in-memory provider.
13
+ * Handle validation errors.
14
+ *
15
+ * If returns a value, it will be validated and used as the store value.
16
+ * If returns undefined, the default value will be returned.
15
17
  */
16
- provider?: SchemaStoreProvider<S>;
18
+ onValidationError?: (props: ValidationSchemaErrorProps<O>) => void | O;
17
19
  }
18
20
  /**
19
21
  * Creates a reactive schema store: state is validated on read, supports partial `set`, and
@@ -25,33 +27,12 @@ export interface SchemaStoreOptions<S extends Record<string, StandardSchemaV1>>
25
27
  * import * as z from 'zod'
26
28
  *
27
29
  * const store = createSchemaStore({
28
- * schemas: { count: z.number(), name: z.string() },
29
- * defaultValues: { count: 0, name: '' },
30
+ * schema: z.object({ count: z.number(), name: z.string() }),
31
+ * defaultValue: { count: 0, name: '' },
30
32
  * })
31
33
  * store.get()
32
34
  * store.set({ count: 1 })
33
35
  * store.subscribe(console.log)
34
36
  * ```
35
37
  */
36
- export declare function createSchemaStore<S extends Record<string, StandardSchemaV1>>(options: SchemaStoreOptions<S>): SchemaStore<ValidationObjectSchemasOutput<S>>;
37
- export interface SchemaStoreProvider<S extends ValidationObjectSchemas> {
38
- get: () => ValidationObjectSchemasOutput<S>;
39
- set: (value: Partial<ValidationObjectSchemasOutput<S>>) => void;
40
- }
41
- /**
42
- * Creates an in-memory provider for a schema store. Use as the state backing when you don't
43
- * need persistence (e.g. for testing or ephemeral UI state).
44
- *
45
- * @example
46
- * ```ts twoslash
47
- * import { createSchemaStore, createSchemaStoreMemoryProvider } from 'seitu'
48
- * import * as z from 'zod'
49
- *
50
- * const store = createSchemaStore({
51
- * schemas: { count: z.number(), name: z.string() },
52
- * defaultValues: { count: 0, name: '' },
53
- * provider: createSchemaStoreMemoryProvider(),
54
- * })
55
- * ```
56
- */
57
- export declare function createSchemaStoreMemoryProvider<S extends ValidationObjectSchemas>(): SchemaStoreProvider<S>;
38
+ export declare function createSchemaStore<O extends Record<string, unknown>>(options: SchemaStoreOptions<O>): SchemaStore<O>;
@@ -86,38 +86,36 @@ function o(e, t, n) {
86
86
  //#endregion
87
87
  //#region src/core/schema-store.ts
88
88
  function s(n) {
89
- let { subscribe: r, notify: i } = t(), a = { ...n.defaultValues }, s = n.provider ?? c(), l = () => {
90
- let e = { ...a };
91
- for (let [t, r] of Object.entries(n.schemas)) e[t] = o(r, s.get()[t], {
92
- defaultValue: a[t],
93
- label: `createSchemaStore:${String(t)}`
89
+ let r = c(n.defaultValue), { subscribe: i, notify: a } = t(), s = { ...n.defaultValue }, l = () => {
90
+ let e = r.get(), t = {
91
+ ...s,
92
+ ...e
93
+ };
94
+ return o(n.schema, t, {
95
+ defaultValue: s,
96
+ label: "createSchemaStore",
97
+ onError: n.onValidationError ? (e, t) => n.onValidationError({
98
+ issues: [...e],
99
+ value: t,
100
+ defaultValue: s
101
+ }) : void 0
94
102
  });
95
- return e;
96
- }, u = e(l, r, i);
103
+ }, u = e(l, i, a);
97
104
  return {
98
105
  ...u,
99
106
  set: (e) => {
100
107
  let t = typeof e == "function" ? e(l()) : e;
101
- s.set(t), i();
108
+ r.set(t), a();
102
109
  },
103
110
  "~": {
104
111
  ...u["~"],
105
- getDefaultValue: (e) => a[e]
106
- }
107
- };
108
- }
109
- function c() {
110
- let e = l({});
111
- return {
112
- get: () => e.get(),
113
- set: (t) => {
114
- e.set(t);
112
+ getDefaultValue: (e) => s[e]
115
113
  }
116
114
  };
117
115
  }
118
116
  //#endregion
119
117
  //#region src/core/store.ts
120
- function l(n) {
118
+ function c(n) {
121
119
  let r = n, { subscribe: i, notify: a } = t();
122
120
  return {
123
121
  ...e(() => r, i, a),
@@ -129,7 +127,7 @@ function l(n) {
129
127
  }
130
128
  //#endregion
131
129
  //#region src/core/throttle.ts
132
- function u(n, r) {
130
+ function l(n, r) {
133
131
  let i = n.get(), a, o = !1, { subscribe: s, notify: c } = t({ onFirstSubscribe() {
134
132
  let e = n.subscribe(() => {
135
133
  if (a) {
@@ -148,7 +146,7 @@ function u(n, r) {
148
146
  }
149
147
  //#endregion
150
148
  //#region src/core/throttle-fn.ts
151
- function d(n, r) {
149
+ function u(n, r) {
152
150
  let i, a, o, { subscribe: s, notify: c } = t();
153
151
  return Object.assign((...e) => {
154
152
  if (a) {
@@ -164,4 +162,4 @@ function d(n, r) {
164
162
  }, e(() => i, s, c));
165
163
  }
166
164
  //#endregion
167
- export { c as a, i as c, e as d, t as f, s as i, r as l, u as n, o, l as r, a as s, d as t, n as u };
165
+ export { o as a, r as c, t as d, s as i, n as l, l as n, a as o, c as r, i as s, u as t, e as u };
package/dist/core.js CHANGED
@@ -1,2 +1,2 @@
1
- import { a as e, c as t, d as n, f as r, i, l as a, n as o, r as s, t as c, u as l } from "./core-BFAZmVJI.js";
2
- export { l as createComputed, a as createDebounce, t as createDebounceFn, n as createReadableSubscription, i as createSchemaStore, e as createSchemaStoreMemoryProvider, s as createStore, r as createSubscription, o as createThrottle, c as createThrottleFn };
1
+ import { c as e, d as t, i as n, l as r, n as i, r as a, s as o, t as s, u as c } from "./core-DSqQS36s.js";
2
+ export { r as createComputed, e as createDebounce, o as createDebounceFn, c as createReadableSubscription, n as createSchemaStore, a as createStore, t as createSubscription, i as createThrottle, s as createThrottleFn };
@@ -1,16 +1,18 @@
1
1
  import type { StandardSchemaV1 } from '@standard-schema/spec';
2
- import type { SchemaStore, SchemaStoreOptions } from '../core/index';
2
+ import type { Readable, Subscribable, Writable } from '../core/index';
3
3
  import type { ValidationObjectSchemas, ValidationObjectSchemasOutput, ValidationSchemasErrorProps } from '../validate';
4
- export interface WebStorageOptions<S extends ValidationObjectSchemas> extends Omit<SchemaStoreOptions<S>, 'provider'> {
4
+ export interface WebStorageOptions<S extends ValidationObjectSchemas> {
5
+ schemas: S;
6
+ defaultValues: ValidationObjectSchemasOutput<S>;
5
7
  type: 'localStorage' | 'sessionStorage';
6
8
  keyTransform?: (key: keyof S) => string;
7
9
  onValidationError?: (props: ValidationSchemasErrorProps<ValidationObjectSchemasOutput<S>>) => void | StandardSchemaV1.InferOutput<S[keyof S]>;
8
10
  }
9
- export interface WebStorage<O extends Record<string, unknown>> extends SchemaStore<O> {
11
+ export interface WebStorage<O extends Record<string, unknown>> extends Subscribable<O>, Readable<O>, Writable<Partial<O>, O> {
10
12
  '~': {
11
13
  getDefaultValue: <K extends keyof O>(key: K) => O[K];
12
14
  type: 'localStorage' | 'sessionStorage';
13
- } & SchemaStore<O>['~'];
15
+ } & Subscribable<O>['~'];
14
16
  }
15
17
  /**
16
18
  * Creates a reactive handle for a localStorage or sessionStorage instance.
package/dist/web.js CHANGED
@@ -1,18 +1,18 @@
1
- import { d as e, f as t, o as n, s as r } from "./core-BFAZmVJI.js";
1
+ import { a as e, d as t, o as n, u as r } from "./core-DSqQS36s.js";
2
2
  //#region src/web/is-online.ts
3
3
  function i() {
4
- let { subscribe: n, notify: r } = t({ onFirstSubscribe: () => (typeof window < "u" && (window.addEventListener("online", r), window.addEventListener("offline", r)), () => {
5
- typeof window < "u" && (window.removeEventListener("online", r), window.removeEventListener("offline", r));
4
+ let { subscribe: e, notify: n } = t({ onFirstSubscribe: () => (typeof window < "u" && (window.addEventListener("online", n), window.addEventListener("offline", n)), () => {
5
+ typeof window < "u" && (window.removeEventListener("online", n), window.removeEventListener("offline", n));
6
6
  }) });
7
- return e(() => typeof navigator > "u" ? !0 : navigator.onLine, n, r);
7
+ return r(() => typeof navigator > "u" ? !0 : navigator.onLine, e, n);
8
8
  }
9
9
  //#endregion
10
10
  //#region src/web/media-query.ts
11
- function a(n) {
12
- let r = typeof window > "u" ? null : window.matchMedia(n.query), { subscribe: i, notify: a } = t({ onFirstSubscribe: () => (r?.addEventListener("change", a), () => {
13
- r?.removeEventListener("change", a);
11
+ function a(e) {
12
+ let n = typeof window > "u" ? null : window.matchMedia(e.query), { subscribe: i, notify: a } = t({ onFirstSubscribe: () => (n?.addEventListener("change", a), () => {
13
+ n?.removeEventListener("change", a);
14
14
  }) });
15
- return e(() => r?.matches ?? n.defaultMatches ?? !1, i, a);
15
+ return r(() => n?.matches ?? e.defaultMatches ?? !1, i, a);
16
16
  }
17
17
  //#endregion
18
18
  //#region src/web/scroll-state.ts
@@ -66,7 +66,7 @@ function s(e) {
66
66
  }
67
67
  //#endregion
68
68
  //#region src/web/web-storage.ts
69
- function c(r) {
69
+ function c(n) {
70
70
  let i = !1, { subscribe: a, notify: o } = t({ onFirstSubscribe: () => {
71
71
  let e = () => {
72
72
  i || o();
@@ -74,40 +74,40 @@ function c(r) {
74
74
  return typeof window < "u" && window.addEventListener("storage", e), () => {
75
75
  typeof window < "u" && window.removeEventListener("storage", e);
76
76
  };
77
- } }), s = { ...r.defaultValues }, c = Object.keys(r.defaultValues), l = /* @__PURE__ */ new Map(), u, d = () => {
78
- if (typeof window > "u") return r.defaultValues;
79
- let e = window[r.type], t = u !== void 0, i = {};
80
- for (let n of c) {
81
- let a = String(r.keyTransform ? r.keyTransform(n) : n), o = e.getItem(a);
82
- i[n] = o, t && l.get(n) !== o && (t = !1);
83
- }
84
- if (t) return u;
85
- let a = { ...r.defaultValues };
77
+ } }), s = { ...n.defaultValues }, c = Object.keys(n.defaultValues), l = /* @__PURE__ */ new Map(), u, d = () => {
78
+ if (typeof window > "u") return n.defaultValues;
79
+ let t = window[n.type], r = u !== void 0, i = {};
86
80
  for (let e of c) {
87
- let t = i[e];
88
- t === null ? a[e] = r.defaultValues[e] : a[e] = n(r.schemas[e], t, {
89
- defaultValue: r.defaultValues[e],
90
- label: `createWebStorage:${String(e)}`,
91
- onError: r.onValidationError ? (t, n) => r.onValidationError({
92
- issues: [...t],
93
- key: e,
94
- value: n,
95
- defaultValue: r.defaultValues[e]
81
+ let a = String(n.keyTransform ? n.keyTransform(e) : e), o = t.getItem(a);
82
+ i[e] = o, r && l.get(e) !== o && (r = !1);
83
+ }
84
+ if (r) return u;
85
+ let a = { ...n.defaultValues };
86
+ for (let t of c) {
87
+ let r = i[t];
88
+ r === null ? a[t] = n.defaultValues[t] : a[t] = e(n.schemas[t], r, {
89
+ defaultValue: n.defaultValues[t],
90
+ label: `createWebStorage:${String(t)}`,
91
+ onError: n.onValidationError ? (e, r) => n.onValidationError({
92
+ issues: [...e],
93
+ key: t,
94
+ value: r,
95
+ defaultValue: n.defaultValues[t]
96
96
  }) : void 0
97
- }), l.set(String(e), i[String(e)]);
97
+ }), l.set(String(t), i[String(t)]);
98
98
  }
99
99
  return u = a, a;
100
- }, f = e(d, a, o);
100
+ }, f = r(d, a, o);
101
101
  return {
102
102
  ...f,
103
103
  set: (e) => {
104
104
  let t = typeof e == "function" ? e(d()) : e;
105
105
  if (typeof window > "u") return;
106
- let n = window[r.type];
106
+ let r = window[n.type];
107
107
  i = !0, Object.entries(t).forEach(([e, t]) => {
108
108
  let i = typeof t == "string" ? t : JSON.stringify(t);
109
- n.setItem(r.keyTransform ? r.keyTransform(e) : e, i), window.dispatchEvent(new StorageEvent("storage", {
110
- key: r.keyTransform ? r.keyTransform(e) : e,
109
+ r.setItem(n.keyTransform ? n.keyTransform(e) : e, i), window.dispatchEvent(new StorageEvent("storage", {
110
+ key: n.keyTransform ? n.keyTransform(e) : e,
111
111
  newValue: i
112
112
  }));
113
113
  }), i = !1, u = void 0, o();
@@ -115,7 +115,7 @@ function c(r) {
115
115
  "~": {
116
116
  ...f["~"],
117
117
  getDefaultValue: (e) => s[e],
118
- type: r.type
118
+ type: n.type
119
119
  }
120
120
  };
121
121
  }
@@ -132,12 +132,12 @@ function u(i) {
132
132
  };
133
133
  } }), d = l, f, p = () => {
134
134
  if (typeof window > "u") return s;
135
- let e = window[a].getItem(i.key);
136
- if (d !== l && e === d) return f;
137
- if (d = e, e === null) return f = s, f;
138
- let t = r(e);
135
+ let t = window[a].getItem(i.key);
136
+ if (d !== l && t === d) return f;
137
+ if (d = t, t === null) return f = s, f;
138
+ let r = n(t);
139
139
  try {
140
- return "schema" in i ? (f = n(i.schema, e, {
140
+ return "schema" in i ? (f = e(i.schema, t, {
141
141
  defaultValue: s,
142
142
  label: `createWebStorageValue:${i.key}`,
143
143
  onError: i.onValidationError ? (e, t) => i.onValidationError({
@@ -145,13 +145,13 @@ function u(i) {
145
145
  issues: [...e],
146
146
  value: t
147
147
  }) : void 0
148
- }), f) : (f = t, f);
148
+ }), f) : (f = r, f);
149
149
  } catch {
150
- return f = s !== void 0 && typeof s != "string" ? s : t, f;
150
+ return f = s !== void 0 && typeof s != "string" ? s : r, f;
151
151
  }
152
152
  };
153
153
  return {
154
- ...e(p, c, u),
154
+ ...r(p, c, u),
155
155
  set: (e) => {
156
156
  if (typeof window > "u") return;
157
157
  let t = window[a], n = typeof e == "function" ? e(p()) : e;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "seitu",
3
3
  "displayName": "Seitu",
4
4
  "type": "module",
5
- "version": "0.10.4",
5
+ "version": "0.10.5",
6
6
  "private": false,
7
7
  "author": "Valerii Strilets",
8
8
  "license": "MIT",