seitu 0.3.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 +2 -0
- package/dist/core/schema-store.d.ts +44 -6
- package/dist/core/store.d.ts +15 -0
- package/dist/core/store.test.d.ts +1 -0
- package/dist/core-iY5ySiQY.js +88 -0
- package/dist/core.js +2 -2
- package/dist/web.js +1 -1
- package/package.json +1 -1
- package/dist/core-CO_rFBtK.js +0 -58
|
@@ -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,11 +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
|
-
export declare function createSchemaStoreMemoryProvider<S extends SchemaStoreSchema>(): SchemaStoreProvider<S>;
|
|
9
4
|
export type SchemaStoreSchema = Record<string, StandardSchemaV1<unknown, unknown>>;
|
|
10
5
|
export type SchemaStoreOutput<S extends SchemaStoreSchema> = Simplify<{
|
|
11
6
|
[K in keyof S]: StandardSchemaV1.InferOutput<S[K]>;
|
|
@@ -16,6 +11,49 @@ export interface SchemaStore<O extends Record<string, unknown>> extends Subscrib
|
|
|
16
11
|
export interface SchemaStoreOptions<S extends Record<string, StandardSchemaV1>> {
|
|
17
12
|
schemas: S;
|
|
18
13
|
defaultValues: SchemaStoreOutput<S>;
|
|
19
|
-
|
|
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>;
|
|
20
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates a reactive schema store: state is validated on read, supports partial `set`, and
|
|
22
|
+
* falls back to default values when validation fails.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts twoslash
|
|
26
|
+
* import { createSchemaStore, createSchemaStoreMemoryProvider } from 'seitu'
|
|
27
|
+
* import * as z from 'zod'
|
|
28
|
+
*
|
|
29
|
+
* const store = createSchemaStore({
|
|
30
|
+
* schemas: { count: z.number(), name: z.string() },
|
|
31
|
+
* defaultValues: { count: 0, name: '' },
|
|
32
|
+
* })
|
|
33
|
+
* store.get()
|
|
34
|
+
* store.set({ count: 1 })
|
|
35
|
+
* store.subscribe(console.log)
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
21
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>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Readable, Subscribable, Writable } from './subscription';
|
|
2
|
+
export interface Store<T> extends Readable<T>, Writable<T, T>, Subscribable<T> {
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* Creates a simple reactive store (minimal TanStack Store–style API).
|
|
6
|
+
*
|
|
7
|
+
* - **Standalone**: use `get()`, `set(value | updater)`, `subscribe(callback)` for any state.
|
|
8
|
+
* - **With schema-store**: use as the state backing for a memory provider.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const store = createStore({ count: 0 })
|
|
12
|
+
* store.set(prev => ({ ...prev, count: prev.count + 1 }))
|
|
13
|
+
* store.subscribe(state => console.log(state))
|
|
14
|
+
*/
|
|
15
|
+
export declare function createStore<T>(initial: T): Store<T>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -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 {
|
|
2
|
-
export { e as
|
|
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
package/package.json
CHANGED
package/dist/core-CO_rFBtK.js
DELETED
|
@@ -1,58 +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 = new Map();
|
|
11
|
-
return {
|
|
12
|
-
get: () => Object.fromEntries(e.entries()),
|
|
13
|
-
set: (t) => {
|
|
14
|
-
e.clear();
|
|
15
|
-
for (let [n, r] of Object.entries(t)) e.set(n, r);
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
function n(t) {
|
|
20
|
-
let { subscribe: n, notify: i } = r(), a = { ...t.defaultValues }, o = () => {
|
|
21
|
-
let n = { ...a };
|
|
22
|
-
for (let [r, i] of Object.entries(t.schemas)) {
|
|
23
|
-
let o = t.provider.get()[r], s = i["~standard"].validate(e(o));
|
|
24
|
-
if (s instanceof Promise) throw TypeError("[createStorage] Validation schema should not return a Promise.");
|
|
25
|
-
s.issues && console.warn(JSON.stringify(s.issues, null, 2), { cause: s.issues }), n[r] = s.issues ? a[r] : s.value;
|
|
26
|
-
}
|
|
27
|
-
return n;
|
|
28
|
-
};
|
|
29
|
-
return {
|
|
30
|
-
get: o,
|
|
31
|
-
set: (e) => {
|
|
32
|
-
let n = typeof e == "function" ? e(o()) : e;
|
|
33
|
-
t.provider.set(n), i();
|
|
34
|
-
},
|
|
35
|
-
getDefaultValue: (e) => a[e],
|
|
36
|
-
subscribe: (e) => n(() => {
|
|
37
|
-
e(o());
|
|
38
|
-
}),
|
|
39
|
-
"~": {
|
|
40
|
-
output: null,
|
|
41
|
-
notify: i
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
function r() {
|
|
46
|
-
let e = new Set();
|
|
47
|
-
return {
|
|
48
|
-
subscribe(t) {
|
|
49
|
-
return e.add(t), () => {
|
|
50
|
-
e.delete(t);
|
|
51
|
-
};
|
|
52
|
-
},
|
|
53
|
-
notify(t) {
|
|
54
|
-
e.forEach((e) => e(t));
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
export { e as i, n, t as r, r as t };
|