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.
@@ -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(t) {
10
- let { subscribe: r, notify: i } = n(), a = { ...t.defaultValues }, o = () => {
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) => r(() => {
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 n() {
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 { t as n, e as r, n as t };
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-CuCUF5Aj.js";
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';
@@ -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 { n as e, r as t, t as n } from "./core-CuCUF5Aj.js";
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 = e({
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 e = window[r], n = { ...i.defaultValues };
8
+ let t = window[r], n = { ...i.defaultValues };
9
9
  for (let r in n) {
10
- let a = t(e.getItem(r)), o = i.schemas[r]["~standard"].validate(a);
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(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 = () => {
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(e.key);
48
+ let n = window[r].getItem(t.key);
49
49
  if (n === null) return a;
50
- let i = t(n);
50
+ let i = e(n);
51
51
  try {
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;
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: (t) => {
63
+ set: (e) => {
64
64
  if (typeof window > "u") return;
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();
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: (t) => {
69
- let n = o(() => t(c())), r = (n) => {
70
- n.key === e.key && t(c());
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
@@ -2,7 +2,7 @@
2
2
  "name": "seitu",
3
3
  "displayName": "Seitu",
4
4
  "type": "module",
5
- "version": "0.2.4",
5
+ "version": "0.3.0",
6
6
  "private": false,
7
7
  "author": "Valerii Strilets",
8
8
  "license": "MIT",