seitu 0.2.4 → 0.3.0
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 +6 -4
- package/dist/{core-CuCUF5Aj.js → core-CO_rFBtK.js} +15 -5
- package/dist/core.js +2 -2
- package/dist/react/components.d.ts +56 -0
- package/dist/react/components.test.d.ts +1 -0
- package/dist/react/index.d.ts +1 -0
- package/dist/react.js +4 -1
- package/dist/web.js +22 -22
- package/package.json +1 -1
|
@@ -1,6 +1,11 @@
|
|
|
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>;
|
|
4
9
|
export type SchemaStoreSchema = Record<string, StandardSchemaV1<unknown, unknown>>;
|
|
5
10
|
export type SchemaStoreOutput<S extends SchemaStoreSchema> = Simplify<{
|
|
6
11
|
[K in keyof S]: StandardSchemaV1.InferOutput<S[K]>;
|
|
@@ -11,9 +16,6 @@ export interface SchemaStore<O extends Record<string, unknown>> extends Subscrib
|
|
|
11
16
|
export interface SchemaStoreOptions<S extends Record<string, StandardSchemaV1>> {
|
|
12
17
|
schemas: S;
|
|
13
18
|
defaultValues: SchemaStoreOutput<S>;
|
|
14
|
-
provider:
|
|
15
|
-
get: () => SchemaStoreOutput<S>;
|
|
16
|
-
set: (value: Partial<SchemaStoreOutput<S>>) => void;
|
|
17
|
-
};
|
|
19
|
+
provider: SchemaStoreProvider<S>;
|
|
18
20
|
}
|
|
19
21
|
export declare function createSchemaStore<S extends Record<string, StandardSchemaV1>>(options: SchemaStoreOptions<S>): SchemaStore<SchemaStoreOutput<S>>;
|
|
@@ -6,8 +6,18 @@ function e(e) {
|
|
|
6
6
|
return e;
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
|
-
function t(
|
|
10
|
-
let
|
|
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 = () => {
|
|
11
21
|
let n = { ...a };
|
|
12
22
|
for (let [r, i] of Object.entries(t.schemas)) {
|
|
13
23
|
let o = t.provider.get()[r], s = i["~standard"].validate(e(o));
|
|
@@ -23,7 +33,7 @@ function t(t) {
|
|
|
23
33
|
t.provider.set(n), i();
|
|
24
34
|
},
|
|
25
35
|
getDefaultValue: (e) => a[e],
|
|
26
|
-
subscribe: (e) =>
|
|
36
|
+
subscribe: (e) => n(() => {
|
|
27
37
|
e(o());
|
|
28
38
|
}),
|
|
29
39
|
"~": {
|
|
@@ -32,7 +42,7 @@ function t(t) {
|
|
|
32
42
|
}
|
|
33
43
|
};
|
|
34
44
|
}
|
|
35
|
-
function
|
|
45
|
+
function r() {
|
|
36
46
|
let e = new Set();
|
|
37
47
|
return {
|
|
38
48
|
subscribe(t) {
|
|
@@ -45,4 +55,4 @@ function n() {
|
|
|
45
55
|
}
|
|
46
56
|
};
|
|
47
57
|
}
|
|
48
|
-
export {
|
|
58
|
+
export { e as i, n, t as r, r as t };
|
package/dist/core.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as e, t } from "./core-
|
|
2
|
-
export { e as createSchemaStore, t as createSubscription };
|
|
1
|
+
import { n as e, r as t, t as n } from "./core-CO_rFBtK.js";
|
|
2
|
+
export { e as createSchemaStore, t as createSchemaStoreMemoryProvider, n as createSubscription };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { Readable, Subscribable } from '../core';
|
|
2
|
+
import type { UseSubscriptionOptions } from './hooks';
|
|
3
|
+
export interface SubscriptionProps<S extends Subscribable<any> & Readable<any>, R = S['~']['output']> extends Pick<UseSubscriptionOptions<S, R>, 'selector'> {
|
|
4
|
+
value: S;
|
|
5
|
+
children: (value: R) => React.ReactNode;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Declarative component that subscribes to a reactive value and passes it to a render function.
|
|
9
|
+
* Re-renders when the value (or selected value) changes. Use when you prefer a component API
|
|
10
|
+
* over the useSubscription hook.
|
|
11
|
+
*
|
|
12
|
+
* @example Basic usage
|
|
13
|
+
* ```tsx twoslash title="/app/page.tsx"
|
|
14
|
+
* 'use client'
|
|
15
|
+
*
|
|
16
|
+
* import { createSessionStorage } from 'seitu/web'
|
|
17
|
+
* import { Subscription } from 'seitu/react'
|
|
18
|
+
* import * as z from 'zod'
|
|
19
|
+
*
|
|
20
|
+
* const sessionStorage = createSessionStorage({
|
|
21
|
+
* schemas: { count: z.number(), name: z.string() },
|
|
22
|
+
* defaultValues: { count: 0, name: '' },
|
|
23
|
+
* })
|
|
24
|
+
*
|
|
25
|
+
* export default function Page() {
|
|
26
|
+
* return (
|
|
27
|
+
* <Subscription value={sessionStorage}>
|
|
28
|
+
* {(value) => <div>{value.count}</div>}
|
|
29
|
+
* </Subscription>
|
|
30
|
+
* )
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @example With selector
|
|
35
|
+
* ```tsx twoslash title="/app/page.tsx"
|
|
36
|
+
* 'use client'
|
|
37
|
+
*
|
|
38
|
+
* import { createSessionStorage } from 'seitu/web'
|
|
39
|
+
* import { Subscription } from 'seitu/react'
|
|
40
|
+
* import * as z from 'zod'
|
|
41
|
+
*
|
|
42
|
+
* const sessionStorage = createSessionStorage({
|
|
43
|
+
* schemas: { count: z.number(), name: z.string() },
|
|
44
|
+
* defaultValues: { count: 0, name: '' },
|
|
45
|
+
* })
|
|
46
|
+
*
|
|
47
|
+
* export default function Page() {
|
|
48
|
+
* return (
|
|
49
|
+
* <Subscription value={sessionStorage} selector={(v) => v.count}>
|
|
50
|
+
* {(count) => <div>{count}</div>}
|
|
51
|
+
* </Subscription>
|
|
52
|
+
* )
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function Subscription<S extends Subscribable<any> & Readable<any>, R = S['~']['output']>({ value, selector, children, }: SubscriptionProps<S, R>): import("react").ReactNode;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@testing-library/jest-dom/vitest';
|
package/dist/react/index.d.ts
CHANGED
package/dist/react.js
CHANGED
|
@@ -278,4 +278,7 @@ function L(t, n) {
|
|
|
278
278
|
return e(l.get()), t;
|
|
279
279
|
}, [l, r]), u;
|
|
280
280
|
}
|
|
281
|
-
|
|
281
|
+
function R({ value: e, selector: t, children: n }) {
|
|
282
|
+
return n(L(e, { selector: t }));
|
|
283
|
+
}
|
|
284
|
+
export { R as Subscription, L as useSubscription };
|
package/dist/web.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i as e, n as t, t as n } from "./core-CO_rFBtK.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);
|