seitu 0.4.1 → 0.4.3

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,11 +1,11 @@
1
1
  import type { StandardSchemaV1 } from '@standard-schema/spec';
2
2
  import type { Simplify } from '../utils';
3
- import type { Readable, Subscribable, Writable } from './index';
3
+ import type { Destroyable, Readable, Subscribable, Writable } from './index';
4
4
  export type SchemaStoreSchema = Record<string, StandardSchemaV1<unknown, unknown>>;
5
5
  export type SchemaStoreOutput<S extends SchemaStoreSchema> = Simplify<{
6
6
  [K in keyof S]: StandardSchemaV1.InferOutput<S[K]>;
7
7
  }>;
8
- export interface SchemaStore<O extends Record<string, unknown>> extends Subscribable<O>, Readable<O>, Writable<Partial<O>, O> {
8
+ export interface SchemaStore<O extends Record<string, unknown>> extends Subscribable<O>, Readable<O>, Writable<Partial<O>, O>, Destroyable {
9
9
  getDefaultValue: <K extends keyof O>(key: K) => O[K];
10
10
  }
11
11
  export interface SchemaStoreOptions<S extends Record<string, StandardSchemaV1>> {
@@ -39,6 +39,7 @@ export declare function createSchemaStore<S extends Record<string, StandardSchem
39
39
  export interface SchemaStoreProvider<S extends SchemaStoreSchema> {
40
40
  get: () => SchemaStoreOutput<S>;
41
41
  set: (value: Partial<SchemaStoreOutput<S>>) => void;
42
+ destroy?: () => void;
42
43
  }
43
44
  /**
44
45
  * Creates an in-memory provider for a schema store. Use as the state backing when you don't
@@ -17,6 +17,9 @@ export interface Readable<T> {
17
17
  export interface Writable<T, P = T> {
18
18
  set: (value: T | ((prev: P) => T)) => any;
19
19
  }
20
+ export interface Destroyable {
21
+ destroy: () => void;
22
+ }
20
23
  export declare function createSubscription<T = void>(): {
21
24
  subscribe: (callback: (payload: T) => any) => () => void;
22
25
  notify: (payload: T) => void;
@@ -1,5 +1,6 @@
1
+ //#region src/core/subscription.ts
1
2
  function e() {
2
- let e = new Set();
3
+ let e = /* @__PURE__ */ new Set();
3
4
  return {
4
5
  subscribe(t) {
5
6
  return e.add(t), () => {
@@ -11,6 +12,8 @@ function e() {
11
12
  }
12
13
  };
13
14
  }
15
+ //#endregion
16
+ //#region src/core/computed.ts
14
17
  function t(t, n) {
15
18
  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
19
  for (let e of a) e.subscribe(() => i());
@@ -25,6 +28,8 @@ function t(t, n) {
25
28
  }
26
29
  };
27
30
  }
31
+ //#endregion
32
+ //#region src/utils.ts
28
33
  function n(e) {
29
34
  if (typeof e != "string") return e;
30
35
  try {
@@ -33,6 +38,8 @@ function n(e) {
33
38
  return e;
34
39
  }
35
40
  }
41
+ //#endregion
42
+ //#region src/core/schema-store.ts
36
43
  function r(t) {
37
44
  let { subscribe: r, notify: a } = e(), o = { ...t.defaultValues }, s = t.provider ?? i(), c = () => {
38
45
  let e = { ...o };
@@ -53,6 +60,9 @@ function r(t) {
53
60
  subscribe: (e) => r(() => {
54
61
  e(c());
55
62
  }),
63
+ destroy: () => {
64
+ s.destroy?.();
65
+ },
56
66
  "~": {
57
67
  output: null,
58
68
  notify: a
@@ -68,6 +78,8 @@ function i() {
68
78
  }
69
79
  };
70
80
  }
81
+ //#endregion
82
+ //#region src/core/store.ts
71
83
  function a(t) {
72
84
  let n = t, { subscribe: r, notify: i } = e(), a = () => n;
73
85
  return {
@@ -85,4 +97,5 @@ function a(t) {
85
97
  }
86
98
  };
87
99
  }
100
+ //#endregion
88
101
  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, n as t, o as n, r, t as i } from "./core-iY5ySiQY.js";
1
+ import { a as e, n as t, o as n, r, t as i } from "./core-3fd1NXIt.js";
2
2
  export { e as createComputed, t as createSchemaStore, r as createSchemaStoreMemoryProvider, i as createStore, n as createSubscription };
@@ -1,4 +1,4 @@
1
- import type { Readable, Subscribable } from '../core/index';
1
+ import type { Destroyable, Readable, Subscribable } from '../core/index';
2
2
  import * as React from 'react';
3
3
  export interface UseSubscriptionOptions<S extends Subscribable<any> & Readable<any>, R = S['~']['output']> {
4
4
  selector?: (value: S['~']['output']) => R;
@@ -91,4 +91,4 @@ export interface UseSubscriptionOptions<S extends Subscribable<any> & Readable<a
91
91
  * }
92
92
  * ```
93
93
  */
94
- export declare function useSubscription<S extends Subscribable<any> & Readable<any>, R = S['~']['output']>(source: S | (() => S), options?: UseSubscriptionOptions<S, R>): R;
94
+ export declare function useSubscription<S extends Subscribable<any> & Readable<any> & Partial<Destroyable>, R = S['~']['output']>(source: S | (() => S), options?: UseSubscriptionOptions<S, R>): R;
package/dist/react.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import * as e from "react";
2
+ //#region ../node_modules/.pnpm/fast-equals@6.0.0/node_modules/fast-equals/dist/es/index.mjs
2
3
  var { getOwnPropertyNames: t, getOwnPropertySymbols: n } = Object, { hasOwnProperty: r } = Object.prototype;
3
4
  function i(e, t) {
4
5
  return function(n, r, i) {
@@ -176,7 +177,7 @@ function M(e) {
176
177
  }
177
178
  function N({ circular: e, comparator: t, createState: n, equals: r, strict: i }) {
178
179
  if (n) return function(a, o) {
179
- let { cache: s = e ? new WeakMap() : void 0, meta: c } = n();
180
+ let { cache: s = e ? /* @__PURE__ */ new WeakMap() : void 0, meta: c } = n();
180
181
  return t(a, o, {
181
182
  cache: s,
182
183
  equals: r,
@@ -186,7 +187,7 @@ function N({ circular: e, comparator: t, createState: n, equals: r, strict: i })
186
187
  };
187
188
  if (e) return function(e, n) {
188
189
  return t(e, n, {
189
- cache: new WeakMap(),
190
+ cache: /* @__PURE__ */ new WeakMap(),
190
191
  equals: r,
191
192
  meta: void 0,
192
193
  strict: i
@@ -261,6 +262,8 @@ function I(e = {}) {
261
262
  strict: i
262
263
  });
263
264
  }
265
+ //#endregion
266
+ //#region src/react/hooks.ts
264
267
  function L(t, n) {
265
268
  let { selector: r, deps: i = [] } = n ?? {}, a = typeof t == "function", o = e.useRef(t);
266
269
  if (!a && t !== o.current) throw Error("useSubscription detected a new object on re-render. Either create the subscription outside the component or wrap it in a factory: useSubscription(() => yourSubscription(...))");
@@ -275,10 +278,15 @@ function L(t, n) {
275
278
  let t = r ? r(e) : e;
276
279
  F(t, f()) || d(t);
277
280
  }, t = l.subscribe(e);
278
- return e(l.get()), t;
281
+ return e(l.get()), () => {
282
+ t(), l.destroy?.();
283
+ };
279
284
  }, [l, r]), u;
280
285
  }
286
+ //#endregion
287
+ //#region src/react/components.tsx
281
288
  function R({ value: e, selector: t, children: n }) {
282
289
  return n(L(e, { selector: t }));
283
290
  }
291
+ //#endregion
284
292
  export { R as Subscription, L as useSubscription };
@@ -1,7 +1,7 @@
1
1
  import type { StandardSchemaV1 } from '@standard-schema/spec';
2
- import type { Readable, Subscribable, Writable } from '../core/index';
2
+ import type { Destroyable, Readable, Subscribable, Writable } from '../core/index';
3
3
  import type { WebStorage } from './web-storage';
4
- export interface WebStorageValue<V> extends Subscribable<V>, Readable<V>, Writable<V> {
4
+ export interface WebStorageValue<V> extends Subscribable<V>, Readable<V>, Writable<V>, Destroyable {
5
5
  }
6
6
  export interface WebStorageValueOptionsWithStorage<Storage extends WebStorage<any>, K extends keyof Storage['~']['output']> {
7
7
  storage: Storage;
@@ -1,5 +1,6 @@
1
1
  import type { SchemaStore, SchemaStoreOptions, SchemaStoreOutput, SchemaStoreSchema } from '../core/index';
2
2
  export interface WebStorageOptions<S extends SchemaStoreSchema> extends Omit<SchemaStoreOptions<S>, 'provider'> {
3
+ keyTransform?: (key: keyof S) => string;
3
4
  }
4
5
  export interface WebStorage<O extends Record<string, unknown>> extends SchemaStore<O> {
5
6
  '~': {
package/dist/web.js CHANGED
@@ -1,43 +1,57 @@
1
- import { i as e, n as t, o as n } from "./core-iY5ySiQY.js";
1
+ import { i as e, n as t, o as n } from "./core-3fd1NXIt.js";
2
+ //#region src/web/web-storage.ts
2
3
  function r(n) {
3
- let { kind: r, ...i } = n, a = t({
4
- ...i,
4
+ let { kind: r, keyTransform: i, defaultValues: a, schemas: o } = n, s = !1, c = t({
5
+ defaultValues: a,
6
+ schemas: o,
5
7
  provider: {
6
8
  get: () => {
7
- if (typeof window > "u") return i.defaultValues;
8
- let t = window[r], n = { ...i.defaultValues };
9
+ if (typeof window > "u") return a;
10
+ let t = window[r], n = { ...a };
9
11
  for (let r in n) {
10
- let a = e(t.getItem(r)), o = i.schemas[r]["~standard"].validate(a);
11
- if (o instanceof Promise) throw TypeError("[createWebSchemaStore] Validation schema should not return a Promise.");
12
- o.issues && console.warn(JSON.stringify(o.issues, null, 2), { cause: o.issues }), n[r] = o.issues ? i.defaultValues[r] : o.value;
12
+ let s = t.getItem(i ? i(r) : r);
13
+ if (s === null) {
14
+ n[r] = a[r];
15
+ continue;
16
+ }
17
+ let c = e(s), l = o[r]["~standard"].validate(c);
18
+ if (l instanceof Promise) throw TypeError("[createWebSchemaStore] Validation schema should not return a Promise.");
19
+ l.issues && console.warn(JSON.stringify(l.issues, null, 2), { cause: l.issues }), n[r] = l.issues ? a[r] : l.value;
13
20
  }
14
21
  return n;
15
22
  },
16
23
  set: (e) => {
17
24
  if (typeof window > "u") return;
18
25
  let t = window[r];
19
- Object.entries(e).forEach(([e, n]) => {
20
- t.setItem(e, typeof n == "string" ? n : JSON.stringify(n));
21
- });
26
+ s = !0, Object.entries(e).forEach(([e, n]) => {
27
+ t.setItem(i ? i(e) : e, typeof n == "string" ? n : JSON.stringify(n));
28
+ }), window.dispatchEvent(new Event("storage")), s = !1;
22
29
  }
23
30
  }
24
- });
25
- return typeof window < "u" && window.addEventListener("storage", () => {
26
- a["~"].notify();
27
- }), {
28
- ...a,
31
+ }), l = () => {
32
+ s || c["~"].notify();
33
+ };
34
+ return typeof window < "u" && window.addEventListener("storage", l), {
35
+ ...c,
36
+ destroy: () => {
37
+ c.destroy?.(), typeof window < "u" && window.removeEventListener("storage", l);
38
+ },
29
39
  "~": {
30
40
  kind: r,
31
- ...a["~"]
41
+ ...c["~"]
32
42
  }
33
43
  };
34
44
  }
45
+ //#endregion
46
+ //#region src/web/local-storage.ts
35
47
  function i(e) {
36
48
  return r({
37
49
  kind: "localStorage",
38
50
  ...e
39
51
  });
40
52
  }
53
+ //#endregion
54
+ //#region src/web/web-storage-value.ts
41
55
  function a(t) {
42
56
  let r = "storage" in t ? t.storage["~"].kind : t.kind, i = `${r}Value`;
43
57
  if ("schema" in t && t.defaultValue === void 0) throw Error(`[${i}] Default value is required`);
@@ -57,21 +71,19 @@ function a(t) {
57
71
  } catch {
58
72
  return a !== void 0 && typeof a != "string" ? a : i;
59
73
  }
74
+ }, l = (e) => {
75
+ e.key === t.key && s();
60
76
  };
61
- return {
77
+ return typeof window < "u" && window.addEventListener("storage", l), {
62
78
  get: c,
63
79
  set: (e) => {
64
80
  if (typeof window > "u") return;
65
81
  let n = window[r], i = typeof e == "function" ? e(c()) : e;
66
82
  n.setItem(t.key, typeof i == "string" ? i : JSON.stringify(i)), window.dispatchEvent(new Event("storage")), s();
67
83
  },
68
- subscribe: (e) => {
69
- let n = o(() => e(c())), r = (n) => {
70
- n.key === t.key && e(c());
71
- };
72
- return typeof window < "u" && window.addEventListener("storage", r), () => {
73
- n(), typeof window < "u" && window.removeEventListener("storage", r);
74
- };
84
+ subscribe: (e) => o(() => e(c())),
85
+ destroy: () => {
86
+ typeof window < "u" && window.removeEventListener("storage", l);
75
87
  },
76
88
  "~": {
77
89
  output: null,
@@ -79,12 +91,16 @@ function a(t) {
79
91
  }
80
92
  };
81
93
  }
94
+ //#endregion
95
+ //#region src/web/local-storage-value.ts
82
96
  function o(e) {
83
97
  return "storage" in e ? a(e) : a({
84
98
  ...e,
85
99
  kind: "localStorage"
86
100
  });
87
101
  }
102
+ //#endregion
103
+ //#region src/web/media-query.ts
88
104
  function s(e) {
89
105
  let { subscribe: t, notify: r } = n(), i = () => {
90
106
  if (typeof window > "u") return e.defaultMatches ?? !1;
@@ -109,6 +125,8 @@ function s(e) {
109
125
  }
110
126
  };
111
127
  }
128
+ //#endregion
129
+ //#region src/web/scroll-state.ts
112
130
  var c = {
113
131
  reached: !1,
114
132
  remaining: 0
@@ -159,16 +177,21 @@ function l(e) {
159
177
  }
160
178
  };
161
179
  }
180
+ //#endregion
181
+ //#region src/web/session-storage.ts
162
182
  function u(e) {
163
183
  return r({
164
184
  kind: "sessionStorage",
165
185
  ...e
166
186
  });
167
187
  }
188
+ //#endregion
189
+ //#region src/web/session-storage-value.ts
168
190
  function d(e) {
169
191
  return "storage" in e ? a(e) : a({
170
192
  ...e,
171
193
  kind: "sessionStorage"
172
194
  });
173
195
  }
196
+ //#endregion
174
197
  export { i as createLocalStorage, o as createLocalStorageValue, s as createMediaQuery, l as createScrollState, u as createSessionStorage, d as createSessionStorageValue };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "seitu",
3
3
  "displayName": "Seitu",
4
4
  "type": "module",
5
- "version": "0.4.1",
5
+ "version": "0.4.3",
6
6
  "private": false,
7
7
  "author": "Valerii Strilets",
8
8
  "license": "MIT",
@@ -64,14 +64,14 @@
64
64
  "@testing-library/jest-dom": "^6.9.1",
65
65
  "@testing-library/react": "^16.3.2",
66
66
  "@types/react": "^19.2.14",
67
- "@vitejs/plugin-react": "^5.1.4",
68
- "happy-dom": "^20.8.3",
67
+ "@vitejs/plugin-react": "^6.0.1",
68
+ "happy-dom": "^20.8.4",
69
69
  "react-dom": "^19.2.4",
70
70
  "type-fest": "^5.4.4",
71
71
  "typescript": "^5.9.3",
72
- "vite": "^8.0.0-beta.16",
72
+ "vite": "^8.0.0",
73
73
  "vite-plugin-dts": "^4.5.4",
74
- "vitest": "^4.0.18",
74
+ "vitest": "^4.1.0",
75
75
  "zod": "^4.3.6"
76
76
  },
77
77
  "scripts": {