seitu 0.2.5 → 0.4.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.
@@ -1,2 +1,3 @@
1
1
  export * from './schema-store';
2
+ export * from './store';
2
3
  export * from './subscription';
@@ -5,6 +5,14 @@ export interface SchemaStoreProvider<S extends SchemaStoreSchema> {
5
5
  get: () => SchemaStoreOutput<S>;
6
6
  set: (value: Partial<SchemaStoreOutput<S>>) => void;
7
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
+ */
8
16
  export declare function createSchemaStoreMemoryProvider<S extends SchemaStoreSchema>(): SchemaStoreProvider<S>;
9
17
  export type SchemaStoreSchema = Record<string, StandardSchemaV1<unknown, unknown>>;
10
18
  export type SchemaStoreOutput<S extends SchemaStoreSchema> = Simplify<{
@@ -18,4 +26,20 @@ export interface SchemaStoreOptions<S extends Record<string, StandardSchemaV1>>
18
26
  defaultValues: SchemaStoreOutput<S>;
19
27
  provider: SchemaStoreProvider<S>;
20
28
  }
29
+ /**
30
+ * Creates a reactive schema store: state is validated on read, supports partial `set`, and
31
+ * falls back to default values when validation fails.
32
+ *
33
+ * @example
34
+ * const provider = createSchemaStoreMemoryProvider()
35
+ * const store = createSchemaStore({
36
+ * schemas: { count: z.number(), name: z.string() },
37
+ * defaultValues: { count: 0, name: '' },
38
+ * provider,
39
+ * })
40
+ * store.get()
41
+ * store.set({ count: 1 })
42
+ * store.getDefaultValue('count') // 0
43
+ * store.subscribe(console.log)
44
+ */
21
45
  export declare function createSchemaStore<S extends Record<string, StandardSchemaV1>>(options: SchemaStoreOptions<S>): SchemaStore<SchemaStoreOutput<S>>;
@@ -0,0 +1,28 @@
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>;
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 @@
1
+ export {};
@@ -7,12 +7,11 @@ function e(e) {
7
7
  }
8
8
  }
9
9
  function t() {
10
- let e = new Map();
10
+ let e = i({});
11
11
  return {
12
- get: () => Object.fromEntries(e.entries()),
12
+ get: () => e.get(),
13
13
  set: (t) => {
14
- e.clear();
15
- for (let [n, r] of Object.entries(t)) e.set(n, r);
14
+ e.set(t);
16
15
  }
17
16
  };
18
17
  }
@@ -55,4 +54,34 @@ function r() {
55
54
  }
56
55
  };
57
56
  }
58
- export { e as i, n, t as r, r as t };
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 };
package/dist/core.js CHANGED
@@ -1,2 +1,2 @@
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 };
1
+ import { a as e, i as t, n, r, t as i } from "./core-DllnjYYg.js";
2
+ export { i as createComputed, t as createSchemaStore, e as createSchemaStoreMemoryProvider, n as createStore, r 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';
@@ -1 +1,2 @@
1
+ export * from './components';
1
2
  export * from './hooks';
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
- export { L as useSubscription };
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 { i as e, n as t, t as n } from "./core-CO_rFBtK.js";
1
+ import { i as e, o as t, r as n } from "./core-DllnjYYg.js";
2
2
  function r(n) {
3
- let { kind: r, ...i } = n, a = t({
3
+ let { kind: r, ...i } = n, a = e({
4
4
  ...i,
5
5
  provider: {
6
6
  get: () => {
7
7
  if (typeof window > "u") return i.defaultValues;
8
- let t = window[r], n = { ...i.defaultValues };
8
+ let e = window[r], n = { ...i.defaultValues };
9
9
  for (let r in n) {
10
- let a = e(t.getItem(r)), o = i.schemas[r]["~standard"].validate(a);
10
+ let a = t(e.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(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 = () => {
41
+ function a(e) {
42
+ let r = "storage" in e ? e.storage["~"].kind : e.kind, i = `${r}Value`;
43
+ if ("schema" in e && e.defaultValue === void 0) throw Error(`[${i}] Default value is required`);
44
+ if (e.key === void 0) throw Error(`[${i}] Key is required`);
45
+ if (!("schema" in e || "storage" in e)) throw Error(`[${i}] Either schema or storage must be provided`);
46
+ let a = ("schema" in e ? e.defaultValue : e.storage.getDefaultValue(e.key)) ?? null, { subscribe: o, notify: s } = n(), c = () => {
47
47
  if (typeof window > "u") return a;
48
- let n = window[r].getItem(t.key);
48
+ let n = window[r].getItem(e.key);
49
49
  if (n === null) return a;
50
- let i = e(n);
50
+ let i = t(n);
51
51
  try {
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;
52
+ if ("schema" in e) {
53
+ let t = e.schema["~standard"].validate(i);
54
+ if (t instanceof Promise) throw TypeError("Validation schema should not return a Promise.");
55
+ return t.issues ? (console.error(JSON.stringify(t.issues, null, 2), { cause: t.issues }), a) : t.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(t) {
60
60
  };
61
61
  return {
62
62
  get: c,
63
- set: (e) => {
63
+ set: (t) => {
64
64
  if (typeof window > "u") return;
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();
65
+ let n = window[r], i = typeof t == "function" ? t(c()) : t;
66
+ n.setItem(e.key, typeof i == "string" ? i : JSON.stringify(i)), window.dispatchEvent(new Event("storage")), s();
67
67
  },
68
- subscribe: (e) => {
69
- let n = o(() => e(c())), r = (n) => {
70
- n.key === t.key && e(c());
68
+ subscribe: (t) => {
69
+ let n = o(() => t(c())), r = (n) => {
70
+ n.key === e.key && t(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
@@ -2,7 +2,7 @@
2
2
  "name": "seitu",
3
3
  "displayName": "Seitu",
4
4
  "type": "module",
5
- "version": "0.2.5",
5
+ "version": "0.4.0",
6
6
  "private": false,
7
7
  "author": "Valerii Strilets",
8
8
  "license": "MIT",