seitu 0.4.0 → 0.4.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/dist/core/computed.d.ts +33 -0
- package/dist/core/computed.test.d.ts +1 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/schema-store.d.ts +31 -17
- package/dist/core/store.d.ts +0 -13
- package/dist/core-iY5ySiQY.js +88 -0
- package/dist/core.js +2 -2
- package/dist/web.js +22 -22
- package/package.json +1 -1
- package/dist/core-DllnjYYg.js +0 -87
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Readable, Subscribable } from './subscription';
|
|
2
|
+
export interface Computed<T> extends Readable<T>, Subscribable<T> {
|
|
3
|
+
}
|
|
4
|
+
type Source<T = unknown> = Readable<T> & Subscribable<T>;
|
|
5
|
+
type SourceValues<S extends readonly Source[]> = {
|
|
6
|
+
[K in keyof S]: S[K] extends Source<infer V> ? V : never;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Creates a computed (derived) subscription from a subscription or an array of subscriptions.
|
|
10
|
+
* The computed value is updated whenever any subscription updates.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts twoslash
|
|
14
|
+
* import { createComputed, createStore } from 'seitu'
|
|
15
|
+
*
|
|
16
|
+
* const count = createStore({ a: 1, b: 2 })
|
|
17
|
+
* const sum = createComputed(count, s => s.a + s.b)
|
|
18
|
+
* sum.get() // 3
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts twoslash
|
|
23
|
+
* import { createComputed, createStore } from 'seitu'
|
|
24
|
+
*
|
|
25
|
+
* const a = createStore(1)
|
|
26
|
+
* const b = createStore(2)
|
|
27
|
+
* const sum = createComputed([a, b], ([a, b]) => a + b)
|
|
28
|
+
* sum.get() // 3
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare function createComputed<S extends readonly Source[], R>(sources: [...S], transform: (values: SourceValues<S>) => R): Computed<R>;
|
|
32
|
+
export declare function createComputed<T, R>(source: Source<T>, transform: (value: T) => R): Computed<R>;
|
|
33
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/core/index.d.ts
CHANGED
|
@@ -1,19 +1,6 @@
|
|
|
1
1
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
2
|
import type { Simplify } from '../utils';
|
|
3
3
|
import type { Readable, Subscribable, Writable } from './index';
|
|
4
|
-
export interface SchemaStoreProvider<S extends SchemaStoreSchema> {
|
|
5
|
-
get: () => SchemaStoreOutput<S>;
|
|
6
|
-
set: (value: Partial<SchemaStoreOutput<S>>) => void;
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Creates an in-memory provider for a schema store. Use as the state backing when you don't
|
|
10
|
-
* need persistence (e.g. for testing or ephemeral UI state).
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* const provider = createSchemaStoreMemoryProvider<typeof schemas>()
|
|
14
|
-
* const store = createSchemaStore({ schemas, defaultValues, provider })
|
|
15
|
-
*/
|
|
16
|
-
export declare function createSchemaStoreMemoryProvider<S extends SchemaStoreSchema>(): SchemaStoreProvider<S>;
|
|
17
4
|
export type SchemaStoreSchema = Record<string, StandardSchemaV1<unknown, unknown>>;
|
|
18
5
|
export type SchemaStoreOutput<S extends SchemaStoreSchema> = Simplify<{
|
|
19
6
|
[K in keyof S]: StandardSchemaV1.InferOutput<S[K]>;
|
|
@@ -24,22 +11,49 @@ export interface SchemaStore<O extends Record<string, unknown>> extends Subscrib
|
|
|
24
11
|
export interface SchemaStoreOptions<S extends Record<string, StandardSchemaV1>> {
|
|
25
12
|
schemas: S;
|
|
26
13
|
defaultValues: SchemaStoreOutput<S>;
|
|
27
|
-
|
|
14
|
+
/**
|
|
15
|
+
* The provider to use for the schema store. If not provided, the schema store will
|
|
16
|
+
* use an in-memory provider.
|
|
17
|
+
*/
|
|
18
|
+
provider?: SchemaStoreProvider<S>;
|
|
28
19
|
}
|
|
29
20
|
/**
|
|
30
21
|
* Creates a reactive schema store: state is validated on read, supports partial `set`, and
|
|
31
22
|
* falls back to default values when validation fails.
|
|
32
23
|
*
|
|
33
24
|
* @example
|
|
34
|
-
*
|
|
25
|
+
* ```ts twoslash
|
|
26
|
+
* import { createSchemaStore, createSchemaStoreMemoryProvider } from 'seitu'
|
|
27
|
+
* import * as z from 'zod'
|
|
28
|
+
*
|
|
35
29
|
* const store = createSchemaStore({
|
|
36
30
|
* schemas: { count: z.number(), name: z.string() },
|
|
37
31
|
* defaultValues: { count: 0, name: '' },
|
|
38
|
-
* provider,
|
|
39
32
|
* })
|
|
40
33
|
* store.get()
|
|
41
34
|
* store.set({ count: 1 })
|
|
42
|
-
* store.getDefaultValue('count') // 0
|
|
43
35
|
* store.subscribe(console.log)
|
|
36
|
+
* ```
|
|
44
37
|
*/
|
|
45
38
|
export declare function createSchemaStore<S extends Record<string, StandardSchemaV1>>(options: SchemaStoreOptions<S>): SchemaStore<SchemaStoreOutput<S>>;
|
|
39
|
+
export interface SchemaStoreProvider<S extends SchemaStoreSchema> {
|
|
40
|
+
get: () => SchemaStoreOutput<S>;
|
|
41
|
+
set: (value: Partial<SchemaStoreOutput<S>>) => void;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Creates an in-memory provider for a schema store. Use as the state backing when you don't
|
|
45
|
+
* need persistence (e.g. for testing or ephemeral UI state).
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts twoslash
|
|
49
|
+
* import { createSchemaStore, createSchemaStoreMemoryProvider } from 'seitu'
|
|
50
|
+
* import * as z from 'zod'
|
|
51
|
+
*
|
|
52
|
+
* const store = createSchemaStore({
|
|
53
|
+
* schemas: { count: z.number(), name: z.string() },
|
|
54
|
+
* defaultValues: { count: 0, name: '' },
|
|
55
|
+
* provider: createSchemaStoreMemoryProvider(),
|
|
56
|
+
* })
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export declare function createSchemaStoreMemoryProvider<S extends SchemaStoreSchema>(): SchemaStoreProvider<S>;
|
package/dist/core/store.d.ts
CHANGED
|
@@ -13,16 +13,3 @@ export interface Store<T> extends Readable<T>, Writable<T, T>, Subscribable<T> {
|
|
|
13
13
|
* store.subscribe(state => console.log(state))
|
|
14
14
|
*/
|
|
15
15
|
export declare function createStore<T>(initial: T): Store<T>;
|
|
16
|
-
export type Computed<T> = Readable<T> & Subscribable<T>;
|
|
17
|
-
/**
|
|
18
|
-
* Creates a computed (derived) store from a source store. The computed value is
|
|
19
|
-
* updated whenever the source updates; it is read-only (no set).
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* const count = createStore({ a: 1, b: 2 })
|
|
23
|
-
* const sum = createComputed(count, s => s.a + s.b)
|
|
24
|
-
* sum.get() // 3
|
|
25
|
-
* count.set({ a: 2, b: 2 })
|
|
26
|
-
* sum.get() // 4
|
|
27
|
-
*/
|
|
28
|
-
export declare function createComputed<T, R>(source: Readable<T> & Subscribable<T>, transform: (value: T) => R): Computed<R>;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
function e() {
|
|
2
|
+
let e = new Set();
|
|
3
|
+
return {
|
|
4
|
+
subscribe(t) {
|
|
5
|
+
return e.add(t), () => {
|
|
6
|
+
e.delete(t);
|
|
7
|
+
};
|
|
8
|
+
},
|
|
9
|
+
notify(t) {
|
|
10
|
+
e.forEach((e) => e(t));
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function t(t, n) {
|
|
15
|
+
let { subscribe: r, notify: i } = e(), a = Array.isArray(t) ? t : [t], o = !Array.isArray(t), s = () => n(o ? a[0].get() : a.map((e) => e.get()));
|
|
16
|
+
for (let e of a) e.subscribe(() => i());
|
|
17
|
+
return {
|
|
18
|
+
get: s,
|
|
19
|
+
subscribe(e) {
|
|
20
|
+
return r(() => e(s()));
|
|
21
|
+
},
|
|
22
|
+
"~": {
|
|
23
|
+
output: null,
|
|
24
|
+
notify: i
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function n(e) {
|
|
29
|
+
if (typeof e != "string") return e;
|
|
30
|
+
try {
|
|
31
|
+
return JSON.parse(e);
|
|
32
|
+
} catch {
|
|
33
|
+
return e;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function r(t) {
|
|
37
|
+
let { subscribe: r, notify: a } = e(), o = { ...t.defaultValues }, s = t.provider ?? i(), c = () => {
|
|
38
|
+
let e = { ...o };
|
|
39
|
+
for (let [r, i] of Object.entries(t.schemas)) {
|
|
40
|
+
let t = s.get()[r], a = i["~standard"].validate(n(t));
|
|
41
|
+
if (a instanceof Promise) throw TypeError("[createStorage] Validation schema should not return a Promise.");
|
|
42
|
+
a.issues && console.warn(JSON.stringify(a.issues, null, 2), { cause: a.issues }), e[r] = a.issues ? o[r] : a.value;
|
|
43
|
+
}
|
|
44
|
+
return e;
|
|
45
|
+
};
|
|
46
|
+
return {
|
|
47
|
+
get: c,
|
|
48
|
+
set: (e) => {
|
|
49
|
+
let t = typeof e == "function" ? e(c()) : e;
|
|
50
|
+
s.set(t), a();
|
|
51
|
+
},
|
|
52
|
+
getDefaultValue: (e) => o[e],
|
|
53
|
+
subscribe: (e) => r(() => {
|
|
54
|
+
e(c());
|
|
55
|
+
}),
|
|
56
|
+
"~": {
|
|
57
|
+
output: null,
|
|
58
|
+
notify: a
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function i() {
|
|
63
|
+
let e = a({});
|
|
64
|
+
return {
|
|
65
|
+
get: () => e.get(),
|
|
66
|
+
set: (t) => {
|
|
67
|
+
e.set(t);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function a(t) {
|
|
72
|
+
let n = t, { subscribe: r, notify: i } = e(), a = () => n;
|
|
73
|
+
return {
|
|
74
|
+
get: a,
|
|
75
|
+
set: (e) => {
|
|
76
|
+
let t = typeof e == "function" ? e(n) : e;
|
|
77
|
+
t !== n && (n = t, i());
|
|
78
|
+
},
|
|
79
|
+
subscribe(e) {
|
|
80
|
+
return r(() => e(a()));
|
|
81
|
+
},
|
|
82
|
+
"~": {
|
|
83
|
+
output: null,
|
|
84
|
+
notify: i
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
export { t as a, n as i, r as n, e as o, i as r, a as t };
|
package/dist/core.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as e,
|
|
2
|
-
export {
|
|
1
|
+
import { a as e, n as t, o as n, r, t as i } from "./core-iY5ySiQY.js";
|
|
2
|
+
export { e as createComputed, t as createSchemaStore, r as createSchemaStoreMemoryProvider, i as createStore, n as createSubscription };
|
package/dist/web.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { i as e,
|
|
1
|
+
import { i as e, n as t, o as n } from "./core-iY5ySiQY.js";
|
|
2
2
|
function r(n) {
|
|
3
|
-
let { kind: r, ...i } = n, a =
|
|
3
|
+
let { kind: r, ...i } = n, a = t({
|
|
4
4
|
...i,
|
|
5
5
|
provider: {
|
|
6
6
|
get: () => {
|
|
7
7
|
if (typeof window > "u") return i.defaultValues;
|
|
8
|
-
let
|
|
8
|
+
let t = window[r], n = { ...i.defaultValues };
|
|
9
9
|
for (let r in n) {
|
|
10
|
-
let a = t
|
|
10
|
+
let a = e(t.getItem(r)), o = i.schemas[r]["~standard"].validate(a);
|
|
11
11
|
if (o instanceof Promise) throw TypeError("[createWebSchemaStore] Validation schema should not return a Promise.");
|
|
12
12
|
o.issues && console.warn(JSON.stringify(o.issues, null, 2), { cause: o.issues }), n[r] = o.issues ? i.defaultValues[r] : o.value;
|
|
13
13
|
}
|
|
@@ -38,21 +38,21 @@ function i(e) {
|
|
|
38
38
|
...e
|
|
39
39
|
});
|
|
40
40
|
}
|
|
41
|
-
function a(
|
|
42
|
-
let r = "storage" in
|
|
43
|
-
if ("schema" in
|
|
44
|
-
if (
|
|
45
|
-
if (!("schema" in
|
|
46
|
-
let a = ("schema" in
|
|
41
|
+
function a(t) {
|
|
42
|
+
let r = "storage" in t ? t.storage["~"].kind : t.kind, i = `${r}Value`;
|
|
43
|
+
if ("schema" in t && t.defaultValue === void 0) throw Error(`[${i}] Default value is required`);
|
|
44
|
+
if (t.key === void 0) throw Error(`[${i}] Key is required`);
|
|
45
|
+
if (!("schema" in t || "storage" in t)) throw Error(`[${i}] Either schema or storage must be provided`);
|
|
46
|
+
let a = ("schema" in t ? t.defaultValue : t.storage.getDefaultValue(t.key)) ?? null, { subscribe: o, notify: s } = n(), c = () => {
|
|
47
47
|
if (typeof window > "u") return a;
|
|
48
|
-
let n = window[r].getItem(
|
|
48
|
+
let n = window[r].getItem(t.key);
|
|
49
49
|
if (n === null) return a;
|
|
50
|
-
let i =
|
|
50
|
+
let i = e(n);
|
|
51
51
|
try {
|
|
52
|
-
if ("schema" in
|
|
53
|
-
let
|
|
54
|
-
if (
|
|
55
|
-
return
|
|
52
|
+
if ("schema" in t) {
|
|
53
|
+
let e = t.schema["~standard"].validate(i);
|
|
54
|
+
if (e instanceof Promise) throw TypeError("Validation schema should not return a Promise.");
|
|
55
|
+
return e.issues ? (console.error(JSON.stringify(e.issues, null, 2), { cause: e.issues }), a) : e.value;
|
|
56
56
|
} else return i;
|
|
57
57
|
} catch {
|
|
58
58
|
return a !== void 0 && typeof a != "string" ? a : i;
|
|
@@ -60,14 +60,14 @@ function a(e) {
|
|
|
60
60
|
};
|
|
61
61
|
return {
|
|
62
62
|
get: c,
|
|
63
|
-
set: (
|
|
63
|
+
set: (e) => {
|
|
64
64
|
if (typeof window > "u") return;
|
|
65
|
-
let n = window[r], i = typeof
|
|
66
|
-
n.setItem(
|
|
65
|
+
let n = window[r], i = typeof e == "function" ? e(c()) : e;
|
|
66
|
+
n.setItem(t.key, typeof i == "string" ? i : JSON.stringify(i)), window.dispatchEvent(new Event("storage")), s();
|
|
67
67
|
},
|
|
68
|
-
subscribe: (
|
|
69
|
-
let n = o(() =>
|
|
70
|
-
n.key ===
|
|
68
|
+
subscribe: (e) => {
|
|
69
|
+
let n = o(() => e(c())), r = (n) => {
|
|
70
|
+
n.key === t.key && e(c());
|
|
71
71
|
};
|
|
72
72
|
return typeof window < "u" && window.addEventListener("storage", r), () => {
|
|
73
73
|
n(), typeof window < "u" && window.removeEventListener("storage", r);
|
package/package.json
CHANGED
package/dist/core-DllnjYYg.js
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
function e(e) {
|
|
2
|
-
if (typeof e != "string") return e;
|
|
3
|
-
try {
|
|
4
|
-
return JSON.parse(e);
|
|
5
|
-
} catch {
|
|
6
|
-
return e;
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
function t() {
|
|
10
|
-
let e = i({});
|
|
11
|
-
return {
|
|
12
|
-
get: () => e.get(),
|
|
13
|
-
set: (t) => {
|
|
14
|
-
e.set(t);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
function n(t) {
|
|
19
|
-
let { subscribe: n, notify: i } = r(), a = { ...t.defaultValues }, o = () => {
|
|
20
|
-
let n = { ...a };
|
|
21
|
-
for (let [r, i] of Object.entries(t.schemas)) {
|
|
22
|
-
let o = t.provider.get()[r], s = i["~standard"].validate(e(o));
|
|
23
|
-
if (s instanceof Promise) throw TypeError("[createStorage] Validation schema should not return a Promise.");
|
|
24
|
-
s.issues && console.warn(JSON.stringify(s.issues, null, 2), { cause: s.issues }), n[r] = s.issues ? a[r] : s.value;
|
|
25
|
-
}
|
|
26
|
-
return n;
|
|
27
|
-
};
|
|
28
|
-
return {
|
|
29
|
-
get: o,
|
|
30
|
-
set: (e) => {
|
|
31
|
-
let n = typeof e == "function" ? e(o()) : e;
|
|
32
|
-
t.provider.set(n), i();
|
|
33
|
-
},
|
|
34
|
-
getDefaultValue: (e) => a[e],
|
|
35
|
-
subscribe: (e) => n(() => {
|
|
36
|
-
e(o());
|
|
37
|
-
}),
|
|
38
|
-
"~": {
|
|
39
|
-
output: null,
|
|
40
|
-
notify: i
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
function r() {
|
|
45
|
-
let e = new Set();
|
|
46
|
-
return {
|
|
47
|
-
subscribe(t) {
|
|
48
|
-
return e.add(t), () => {
|
|
49
|
-
e.delete(t);
|
|
50
|
-
};
|
|
51
|
-
},
|
|
52
|
-
notify(t) {
|
|
53
|
-
e.forEach((e) => e(t));
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
function i(e) {
|
|
58
|
-
let t = e, { subscribe: n, notify: i } = r(), a = () => t;
|
|
59
|
-
return {
|
|
60
|
-
get: a,
|
|
61
|
-
set: (e) => {
|
|
62
|
-
let n = typeof e == "function" ? e(t) : e;
|
|
63
|
-
n !== t && (t = n, i());
|
|
64
|
-
},
|
|
65
|
-
subscribe(e) {
|
|
66
|
-
return n(() => e(a()));
|
|
67
|
-
},
|
|
68
|
-
"~": {
|
|
69
|
-
output: null,
|
|
70
|
-
notify: i
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
function a(e, t) {
|
|
75
|
-
let { subscribe: n, notify: i } = r(), a = () => t(e.get());
|
|
76
|
-
return e.subscribe(() => i()), {
|
|
77
|
-
get: a,
|
|
78
|
-
subscribe(e) {
|
|
79
|
-
return n(() => e(a()));
|
|
80
|
-
},
|
|
81
|
-
"~": {
|
|
82
|
-
output: null,
|
|
83
|
-
notify: i
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
export { t as a, n as i, i as n, e as o, r, a as t };
|