seitu 0.8.0 → 0.9.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.
@@ -0,0 +1,21 @@
1
+ import type { Readable, Subscribable } from './subscription';
2
+ export interface DebouncedFn<A extends unknown[], R> extends Readable<R | undefined>, Subscribable<R | undefined> {
3
+ (...args: A): void;
4
+ }
5
+ /**
6
+ * Wraps a plain function in a debounced callable that also acts as a subscribable.
7
+ *
8
+ * Each call resets the debounce timer. When the timer fires, the wrapped function
9
+ * is invoked and its return value becomes the current state, notifying subscribers.
10
+ *
11
+ * @example
12
+ * ```ts twoslash
13
+ * import { createDebounceFn } from 'seitu'
14
+ *
15
+ * const search = createDebounceFn((query: string) => fetch(`/api?q=${query}`), 300)
16
+ * search.subscribe(result => console.log('result:', result))
17
+ * search('hello') // debounced — fires after 300ms of inactivity
18
+ * search.get() // latest return value (undefined until first call)
19
+ * ```
20
+ */
21
+ export declare function createDebounceFn<A extends unknown[], R>(fn: (...args: A) => R, wait: number): DebouncedFn<A, R>;
@@ -0,0 +1,19 @@
1
+ import type { Readable, Subscribable } from './subscription';
2
+ export interface Debounced<T> extends Readable<T>, Subscribable<T> {
3
+ }
4
+ /**
5
+ * Creates a debounced readable that delays updates from a source subscribable.
6
+ *
7
+ * The debounced store will only emit the latest value after `wait` milliseconds
8
+ * of inactivity from the source.
9
+ *
10
+ * @example
11
+ * ```ts twoslash
12
+ * import { createStore, createDebounce } from 'seitu'
13
+ *
14
+ * const store = createStore('')
15
+ * const debounced = createDebounce(store, 300)
16
+ * debounced.subscribe(value => console.log('debounced:', value))
17
+ * ```
18
+ */
19
+ export declare function createDebounce<T>(source: Readable<T> & Subscribable<T>, wait: number): Debounced<T>;
@@ -1,4 +1,8 @@
1
1
  export * from './computed';
2
+ export * from './debounce';
3
+ export * from './debounce-fn';
2
4
  export * from './schema-store';
3
5
  export * from './store';
4
6
  export * from './subscription';
7
+ export * from './throttle';
8
+ export * from './throttle-fn';
@@ -0,0 +1,23 @@
1
+ import type { Readable, Subscribable } from './subscription';
2
+ export interface ThrottledFn<A extends unknown[], R> extends Readable<R | undefined>, Subscribable<R | undefined> {
3
+ (...args: A): void;
4
+ }
5
+ /**
6
+ * Wraps a plain function in a throttled callable that also acts as a subscribable.
7
+ *
8
+ * The first call fires immediately. Subsequent calls within the `wait` window are
9
+ * batched — only the last one fires when the interval elapses. The return value of
10
+ * the wrapped function becomes the current state, notifying subscribers.
11
+ *
12
+ * @example
13
+ * ```ts twoslash
14
+ * import { createThrottleFn } from 'seitu'
15
+ *
16
+ * const log = createThrottleFn((msg: string) => console.log(msg), 300)
17
+ * log.subscribe(result => console.log('result:', result))
18
+ * log('hello') // fires immediately
19
+ * log('world') // throttled — fires after 300ms
20
+ * log.get() // latest return value (undefined until first call)
21
+ * ```
22
+ */
23
+ export declare function createThrottleFn<A extends unknown[], R>(fn: (...args: A) => R, wait: number): ThrottledFn<A, R>;
@@ -0,0 +1,19 @@
1
+ import type { Readable, Subscribable } from './subscription';
2
+ export interface Throttled<T> extends Readable<T>, Subscribable<T> {
3
+ }
4
+ /**
5
+ * Creates a throttled readable that rate-limits updates from a source subscribable.
6
+ *
7
+ * Emits the latest value at most once every `wait` milliseconds. The first update
8
+ * fires immediately, then subsequent updates are batched until the interval elapses.
9
+ *
10
+ * @example
11
+ * ```ts twoslash
12
+ * import { createStore, createThrottle } from 'seitu'
13
+ *
14
+ * const store = createStore('')
15
+ * const throttled = createThrottle(store, 300)
16
+ * throttled.subscribe(value => console.log('throttled:', value))
17
+ * ```
18
+ */
19
+ export declare function createThrottle<T>(source: Readable<T> & Subscribable<T>, wait: number): Throttled<T>;
@@ -0,0 +1,187 @@
1
+ //#region src/core/subscription.ts
2
+ function e(e) {
3
+ let t = /* @__PURE__ */ new Set(), n = () => t.forEach((e) => e()), r;
4
+ return {
5
+ subscribe(n, i) {
6
+ return t.size === 0 && e?.onFirstSubscribe && (r = e.onFirstSubscribe() ?? void 0), i?.immediate && n(), t.add(n), () => {
7
+ t.delete(n), t.size === 0 && (r?.(), r = void 0);
8
+ };
9
+ },
10
+ notify: n
11
+ };
12
+ }
13
+ //#endregion
14
+ //#region src/core/computed.ts
15
+ function t(t, n) {
16
+ 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()));
17
+ for (let e of a) e.subscribe(() => i());
18
+ return {
19
+ get: s,
20
+ subscribe(e, t) {
21
+ return r(() => e(s()), t);
22
+ },
23
+ "~": {
24
+ output: null,
25
+ notify: i
26
+ }
27
+ };
28
+ }
29
+ //#endregion
30
+ //#region src/core/debounce.ts
31
+ function n(t, n) {
32
+ let r = t.get(), i, { subscribe: a, notify: o } = e({ onFirstSubscribe() {
33
+ let e = t.subscribe(() => {
34
+ clearTimeout(i), i = setTimeout(() => {
35
+ r = t.get(), o();
36
+ }, n);
37
+ });
38
+ return () => {
39
+ clearTimeout(i), e();
40
+ };
41
+ } }), s = () => r;
42
+ return {
43
+ get: s,
44
+ subscribe(e, t) {
45
+ return a(() => e(s()), t);
46
+ },
47
+ "~": {
48
+ output: null,
49
+ notify: o
50
+ }
51
+ };
52
+ }
53
+ //#endregion
54
+ //#region src/core/debounce-fn.ts
55
+ function r(t, n) {
56
+ let r, i, { subscribe: a, notify: o } = e(), s = () => r;
57
+ return Object.assign((...e) => {
58
+ clearTimeout(i), i = setTimeout(() => {
59
+ r = t(...e), o();
60
+ }, n);
61
+ }, {
62
+ get: s,
63
+ subscribe: (e, t) => a(() => e(s()), t),
64
+ "~": {
65
+ output: void 0,
66
+ notify: o
67
+ }
68
+ });
69
+ }
70
+ //#endregion
71
+ //#region src/utils.ts
72
+ function i(e) {
73
+ if (typeof e != "string") return e;
74
+ try {
75
+ return JSON.parse(e);
76
+ } catch {
77
+ return e;
78
+ }
79
+ }
80
+ //#endregion
81
+ //#region src/core/schema-store.ts
82
+ function a(t) {
83
+ let { subscribe: n, notify: r } = e(), a = { ...t.defaultValues }, s = t.provider ?? o(), c = () => {
84
+ let e = { ...a };
85
+ for (let [n, r] of Object.entries(t.schemas)) {
86
+ let t = s.get()[n], o = r["~standard"].validate(i(t));
87
+ if (o instanceof Promise) throw TypeError("[createStorage] Validation schema should not return a Promise.");
88
+ o.issues && console.warn(`[createSchemaStore] Returned value invalid for key ${String(n)}, returned default value instead`, JSON.stringify(o.issues, null, 2), { cause: o.issues }), e[n] = o.issues ? a[n] : o.value;
89
+ }
90
+ return e;
91
+ };
92
+ return {
93
+ get: c,
94
+ set: (e) => {
95
+ let t = typeof e == "function" ? e(c()) : e;
96
+ s.set(t), r();
97
+ },
98
+ getDefaultValue: (e) => a[e],
99
+ subscribe: (e, t) => n(() => e(c()), t),
100
+ "~": {
101
+ output: null,
102
+ notify: r
103
+ }
104
+ };
105
+ }
106
+ function o() {
107
+ let e = s({});
108
+ return {
109
+ get: () => e.get(),
110
+ set: (t) => {
111
+ e.set(t);
112
+ }
113
+ };
114
+ }
115
+ //#endregion
116
+ //#region src/core/store.ts
117
+ function s(t) {
118
+ let n = t, { subscribe: r, notify: i } = e(), a = () => n;
119
+ return {
120
+ get: a,
121
+ set: (e) => {
122
+ let t = typeof e == "function" ? e(n) : e;
123
+ t !== n && (n = t, i());
124
+ },
125
+ subscribe(e, t) {
126
+ return r(() => e(a()), t);
127
+ },
128
+ "~": {
129
+ output: null,
130
+ notify: i
131
+ }
132
+ };
133
+ }
134
+ //#endregion
135
+ //#region src/core/throttle.ts
136
+ function c(t, n) {
137
+ let r = t.get(), i, a = !1, { subscribe: o, notify: s } = e({ onFirstSubscribe() {
138
+ let e = t.subscribe(() => {
139
+ if (i) {
140
+ a = !0;
141
+ return;
142
+ }
143
+ r = t.get(), s(), i = setTimeout(() => {
144
+ i = void 0, a && (a = !1, r = t.get(), s());
145
+ }, n);
146
+ });
147
+ return () => {
148
+ clearTimeout(i), i = void 0, a = !1, e();
149
+ };
150
+ } }), c = () => r;
151
+ return {
152
+ get: c,
153
+ subscribe(e, t) {
154
+ return o(() => e(c()), t);
155
+ },
156
+ "~": {
157
+ output: null,
158
+ notify: s
159
+ }
160
+ };
161
+ }
162
+ //#endregion
163
+ //#region src/core/throttle-fn.ts
164
+ function l(t, n) {
165
+ let r, i, a, { subscribe: o, notify: s } = e(), c = () => r;
166
+ return Object.assign((...e) => {
167
+ if (i) {
168
+ a = e;
169
+ return;
170
+ }
171
+ r = t(...e), s(), i = setTimeout(() => {
172
+ if (i = void 0, a) {
173
+ let e = a;
174
+ a = void 0, r = t(...e), s();
175
+ }
176
+ }, n);
177
+ }, {
178
+ get: c,
179
+ subscribe: (e, t) => o(() => e(c()), t),
180
+ "~": {
181
+ output: void 0,
182
+ notify: s
183
+ }
184
+ });
185
+ }
186
+ //#endregion
187
+ export { o as a, n as c, a as i, t as l, c as n, i as o, s as r, r as s, l as t, e as u };
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-C93PiIy9.js";
2
- export { e as createComputed, t as createSchemaStore, r as createSchemaStoreMemoryProvider, i as createStore, n as createSubscription };
1
+ import { a as e, c as t, i as n, l as r, n as i, r as a, s as o, t as s, u as c } from "./core-vtgV_plC.js";
2
+ export { r as createComputed, t as createDebounce, o as createDebounceFn, n as createSchemaStore, e as createSchemaStoreMemoryProvider, a as createStore, c as createSubscription, i as createThrottle, s as createThrottleFn };
package/dist/web.js CHANGED
@@ -1,4 +1,4 @@
1
- import { i as e, o as t } from "./core-C93PiIy9.js";
1
+ import { o as e, u as t } from "./core-vtgV_plC.js";
2
2
  //#region src/web/is-online.ts
3
3
  function n() {
4
4
  let { subscribe: e, notify: n } = t({ onFirstSubscribe: () => (typeof window < "u" && (window.addEventListener("online", n), window.addEventListener("offline", n)), () => {
@@ -43,9 +43,9 @@ function r(n) {
43
43
  if (e !== void 0) {
44
44
  let t = n.schemas[i]["~standard"].validate(e);
45
45
  if (t instanceof Promise) throw TypeError("[createWebStorage] Validation schema should not return a Promise.");
46
- t.issues ? console.error("[createWebStorage] Returned value invalid, returned default value instead", JSON.stringify(t.issues, null, 2), { cause: t.issues }) : r[i] = t.value;
46
+ t.issues ? console.warn(`[createWebStorage] Returned value invalid for key ${i}, returned default value instead`, JSON.stringify(t.issues, null, 2), { cause: t.issues }) : r[i] = t.value;
47
47
  }
48
- } else console.warn("[createWebStorage] Returned value invalid, returned default value instead", JSON.stringify(s.issues, null, 2), { cause: s.issues }), r[i] = n.defaultValues[i];
48
+ } else console.warn(`[createWebStorage] Returned value invalid for key ${i}, returned default value instead`, JSON.stringify(s.issues, null, 2), { cause: s.issues }), r[i] = n.defaultValues[i];
49
49
  else r[i] = s.value;
50
50
  }
51
51
  return r;
@@ -113,10 +113,10 @@ function a(n) {
113
113
  if (t !== void 0) {
114
114
  let e = n.schema["~standard"].validate(t);
115
115
  if (e instanceof Promise) throw TypeError("Validation schema should not return a Promise.");
116
- if (e.issues) console.error("Returned value invalid, returned default value instead", JSON.stringify(e.issues, null, 2), { cause: e.issues });
116
+ if (e.issues) console.warn(`[${a}] Returned value invalid for key ${n.key}, returned default value instead`, JSON.stringify(e.issues, null, 2), { cause: e.issues });
117
117
  else return e.value;
118
118
  }
119
- } else console.warn(JSON.stringify(e.issues, null, 2), { cause: e.issues });
119
+ } else console.warn(`[${a}] Returned value invalid for key ${n.key}, returned default value instead`, JSON.stringify(e.issues, null, 2), { cause: e.issues });
120
120
  return o;
121
121
  }
122
122
  return e.value;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "seitu",
3
3
  "displayName": "Seitu",
4
4
  "type": "module",
5
- "version": "0.8.0",
5
+ "version": "0.9.0",
6
6
  "private": false,
7
7
  "author": "Valerii Strilets",
8
8
  "license": "MIT",
@@ -1,94 +0,0 @@
1
- //#region src/core/subscription.ts
2
- function e(e) {
3
- let t = /* @__PURE__ */ new Set(), n = () => t.forEach((e) => e()), r;
4
- return {
5
- subscribe(n, i) {
6
- return t.size === 0 && e?.onFirstSubscribe && (r = e.onFirstSubscribe() ?? void 0), i?.immediate && n(), t.add(n), () => {
7
- t.delete(n), t.size === 0 && (r?.(), r = void 0);
8
- };
9
- },
10
- notify: n
11
- };
12
- }
13
- //#endregion
14
- //#region src/core/computed.ts
15
- function t(t, n) {
16
- 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()));
17
- for (let e of a) e.subscribe(() => i());
18
- return {
19
- get: s,
20
- subscribe(e, t) {
21
- return r(() => e(s()), t);
22
- },
23
- "~": {
24
- output: null,
25
- notify: i
26
- }
27
- };
28
- }
29
- //#endregion
30
- //#region src/utils.ts
31
- function n(e) {
32
- if (typeof e != "string") return e;
33
- try {
34
- return JSON.parse(e);
35
- } catch {
36
- return e;
37
- }
38
- }
39
- //#endregion
40
- //#region src/core/schema-store.ts
41
- function r(t) {
42
- let { subscribe: r, notify: a } = e(), o = { ...t.defaultValues }, s = t.provider ?? i(), c = () => {
43
- let e = { ...o };
44
- for (let [r, i] of Object.entries(t.schemas)) {
45
- let t = s.get()[r], a = i["~standard"].validate(n(t));
46
- if (a instanceof Promise) throw TypeError("[createStorage] Validation schema should not return a Promise.");
47
- a.issues && console.warn(JSON.stringify(a.issues, null, 2), { cause: a.issues }), e[r] = a.issues ? o[r] : a.value;
48
- }
49
- return e;
50
- };
51
- return {
52
- get: c,
53
- set: (e) => {
54
- let t = typeof e == "function" ? e(c()) : e;
55
- s.set(t), a();
56
- },
57
- getDefaultValue: (e) => o[e],
58
- subscribe: (e, t) => r(() => e(c()), t),
59
- "~": {
60
- output: null,
61
- notify: a
62
- }
63
- };
64
- }
65
- function i() {
66
- let e = a({});
67
- return {
68
- get: () => e.get(),
69
- set: (t) => {
70
- e.set(t);
71
- }
72
- };
73
- }
74
- //#endregion
75
- //#region src/core/store.ts
76
- function a(t) {
77
- let n = t, { subscribe: r, notify: i } = e(), a = () => n;
78
- return {
79
- get: a,
80
- set: (e) => {
81
- let t = typeof e == "function" ? e(n) : e;
82
- t !== n && (n = t, i());
83
- },
84
- subscribe(e, t) {
85
- return r(() => e(a()), t);
86
- },
87
- "~": {
88
- output: null,
89
- notify: i
90
- }
91
- };
92
- }
93
- //#endregion
94
- export { t as a, n as i, r as n, e as o, i as r, a as t };