seitu 0.10.3 → 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.
- package/dist/core/schema-store.d.ts +13 -32
- package/dist/{core-BFAZmVJI.js → core-DSqQS36s.js} +20 -22
- package/dist/core.js +2 -2
- package/dist/utils.js +1 -1
- package/dist/web/web-storage.d.ts +6 -4
- package/dist/web.js +41 -41
- package/package.json +1 -1
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
-
import type {
|
|
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<
|
|
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<
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
export interface SchemaStoreOptions<O extends Record<string, unknown>> {
|
|
10
|
+
schema: StandardSchemaV1<unknown, O>;
|
|
11
|
+
defaultValue: O;
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
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
|
-
|
|
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
|
-
*
|
|
29
|
-
*
|
|
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<
|
|
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:
|
|
90
|
-
let e =
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
-
|
|
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
|
-
|
|
108
|
+
r.set(t), a();
|
|
102
109
|
},
|
|
103
110
|
"~": {
|
|
104
111
|
...u["~"],
|
|
105
|
-
getDefaultValue: (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
|
|
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
|
|
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
|
|
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 {
|
|
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 {
|
|
2
|
-
export {
|
|
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 };
|
package/dist/utils.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
function e(e) {
|
|
3
3
|
return {
|
|
4
4
|
...e.defaultValue,
|
|
5
|
-
...typeof e.value == "object" && e.value !== null ? Object.fromEntries(Object.entries(e.value).filter(([t]) => t in e.defaultValue)) : {}
|
|
5
|
+
...typeof e.value == "object" && e.value !== null ? Object.fromEntries(Object.entries(e.value).filter(([t, n]) => t in e.defaultValue && typeof n == typeof e.defaultValue[t])) : {}
|
|
6
6
|
};
|
|
7
7
|
}
|
|
8
8
|
//#endregion
|
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
-
import type {
|
|
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>
|
|
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
|
|
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
|
-
} &
|
|
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 {
|
|
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:
|
|
5
|
-
typeof window < "u" && (window.removeEventListener("online",
|
|
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
|
|
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(
|
|
12
|
-
let
|
|
13
|
-
|
|
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
|
|
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(
|
|
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 = { ...
|
|
78
|
-
if (typeof window > "u") return
|
|
79
|
-
let
|
|
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
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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(
|
|
97
|
+
}), l.set(String(t), i[String(t)]);
|
|
98
98
|
}
|
|
99
99
|
return u = a, a;
|
|
100
|
-
}, f =
|
|
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
|
|
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
|
-
|
|
110
|
-
key:
|
|
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:
|
|
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
|
|
136
|
-
if (d !== l &&
|
|
137
|
-
if (d =
|
|
138
|
-
let
|
|
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 =
|
|
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 =
|
|
148
|
+
}), f) : (f = r, f);
|
|
149
149
|
} catch {
|
|
150
|
-
return f = s !== void 0 && typeof s != "string" ? s :
|
|
150
|
+
return f = s !== void 0 && typeof s != "string" ? s : r, f;
|
|
151
151
|
}
|
|
152
152
|
};
|
|
153
153
|
return {
|
|
154
|
-
...
|
|
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;
|