sigpro 1.1.20 → 2.0.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.
package/sigpro.d.ts ADDED
@@ -0,0 +1,522 @@
1
+ /**
2
+ * SigPro — Type Definitions
3
+ *
4
+ * A minimal reactive library built on an alien-signals-class push-pull core
5
+ * with a thin, direct-to-DOM rendering layer.
6
+ *
7
+ * - Explicit API: `signal()`, `computed()`, `effect()`
8
+ * - Lazy computeds with checkDirty verification
9
+ * - Atomic updates: no virtual DOM, no diffing
10
+ * - Built-in: hash router, i18n, localStorage signals, DI, fetch helper
11
+ */
12
+
13
+ // ============================================================================
14
+ // Core Reactivity
15
+ // ============================================================================
16
+
17
+ /**
18
+ * A reactive signal. Call with no args to read, with a value or an updater
19
+ * function to write.
20
+ *
21
+ * @example
22
+ * const count = signal(0);
23
+ * count(); // read → 0
24
+ * count(1); // write
25
+ * count(c => c + 1);// updater
26
+ */
27
+ export interface Signal<T> {
28
+ (): T;
29
+ (value: T | ((prev: T) => T)): void;
30
+ }
31
+
32
+ /**
33
+ * Creates a reactive signal with an initial value.
34
+ */
35
+ export function signal<T>(initialValue: T): Signal<T>;
36
+
37
+ /**
38
+ * A read-only derived value. Call with no args to read.
39
+ */
40
+ export type ReadonlySignal<T> = () => T;
41
+
42
+ /**
43
+ * Creates a derived value computed lazily from dependencies.
44
+ * Recomputed on read only when dependencies have actually changed.
45
+ */
46
+ export function computed<T>(getter: (oldValue?: T) => T): ReadonlySignal<T>;
47
+
48
+ /**
49
+ * Runs `fn` reactively. Re-executes when its tracked dependencies change.
50
+ * `fn` may return a cleanup function, run before the next re-execution and
51
+ * on stop.
52
+ *
53
+ * @returns A function that stops the effect.
54
+ */
55
+ export function effect(fn: () => void | (() => void)): () => void;
56
+
57
+ /**
58
+ * Creates a scope that groups child effects. Does not track dependencies
59
+ * and does not re-execute. Returns a function that disposes all effects
60
+ * created inside.
61
+ */
62
+ export function effectScope(fn: () => void): () => void;
63
+
64
+ /**
65
+ * Runs `fn` without tracking signal reads as dependencies.
66
+ */
67
+ export function untrack<T>(fn: () => T): T;
68
+
69
+ /**
70
+ * Batches multiple signal writes into a single flush.
71
+ */
72
+ export function batch<T>(fn: () => T): T;
73
+
74
+ /**
75
+ * Watches a signal or getter and calls `cb` when the value changes.
76
+ * Skips the initial run by default. Set `immediate` to call once on setup.
77
+ *
78
+ * @returns A function that stops the watcher.
79
+ */
80
+ export function watch<T>(
81
+ source: Signal<T> | ReadonlySignal<T>,
82
+ cb: (value: T, oldValue: T | undefined) => void,
83
+ options?: { immediate?: boolean }
84
+ ): () => void;
85
+
86
+ /**
87
+ * Creates a signal persisted to `localStorage` under `key`.
88
+ * The value is JSON-serialized. Falls back to `initial` if unavailable
89
+ * or if the stored value cannot be parsed.
90
+ */
91
+ export function local<T>(key: string, initial: T): Signal<T>;
92
+
93
+ // ============================================================================
94
+ // Dependency Injection
95
+ // ============================================================================
96
+
97
+ /**
98
+ * Provides a value in the current effect/scope context.
99
+ * Only visible to descendants.
100
+ */
101
+ export function provide<T = any>(key: string | symbol, value: T): void;
102
+
103
+ /**
104
+ * Injects the nearest value for `key` from an ancestor context.
105
+ * Returns `fallback` if not found.
106
+ */
107
+ export function inject<T = any>(key: string | symbol, fallback?: T): T | undefined;
108
+
109
+ // ============================================================================
110
+ // DOM Creation
111
+ // ============================================================================
112
+
113
+ /**
114
+ * A prop value may be static or a reactive getter.
115
+ */
116
+ export type PropValue<T = any> = T | (() => T);
117
+
118
+ /**
119
+ * Props accepted by `h` and all tag helpers.
120
+ *
121
+ * Special props:
122
+ * - `ref`: function `(el) => void` or object with `.current`
123
+ * - `html`: sets `innerHTML` reactively (skips `children` if present)
124
+ * - `on*`: DOM event listeners (case-insensitive: `onclick`, `onInput`)
125
+ * - any other: assigned to the element (property if it exists, otherwise attribute)
126
+ */
127
+ export type Props = {
128
+ ref?: ((el: Element) => void) | { current: Element | null };
129
+ html?: PropValue<string | null | undefined>;
130
+ class?: PropValue<string | null | undefined>;
131
+ className?: PropValue<string | null | undefined>;
132
+ style?: PropValue<string | null | undefined>;
133
+ [key: string]: PropValue<any>;
134
+ };
135
+
136
+ /**
137
+ * Any value accepted as a child: nodes, strings, numbers, arrays, or
138
+ * reactive getter functions returning any of these.
139
+ */
140
+ export type Child =
141
+ | Node
142
+ | string
143
+ | number
144
+ | boolean
145
+ | null
146
+ | undefined
147
+ | (() => Child | Child[])
148
+ | Child[];
149
+
150
+ /**
151
+ * Creates a DOM element from a tag name, or renders a component.
152
+ *
153
+ * - `tag` as string → creates HTML or SVG element (auto-detected).
154
+ * - `tag` as function → treated as a component: `(props, children[]) => Node | Node[]`.
155
+ * - First argument may be children if it is not a plain object.
156
+ * - Multiple children may be passed variadically.
157
+ */
158
+ export function h(
159
+ tag: string,
160
+ props?: Props | Child | null,
161
+ ...children: Child[]
162
+ ): Element | SVGElement;
163
+
164
+ export function h<P extends object>(
165
+ tag: (props: P, children: Child[]) => Node | Node[] | null,
166
+ props?: P | null,
167
+ ...children: Child[]
168
+ ): Node | Node[] | null;
169
+
170
+ /**
171
+ * Registers all standard HTML tag names as globals (`div`, `span`, `button`,
172
+ * `input`, `textarea`, `svg`, etc.).
173
+ *
174
+ * Must be called once at startup. Defaults to `window`.
175
+ */
176
+ export function exposeTags(target?: any): void;
177
+
178
+ // ============================================================================
179
+ // Mount / Unmount
180
+ // ============================================================================
181
+
182
+ /**
183
+ * Mounts a component into a DOM target.
184
+ *
185
+ * @param component - Component function or node.
186
+ * @param target - CSS selector or DOM element.
187
+ * @returns A function that stops the app scope and clears the target.
188
+ */
189
+ export function mount(
190
+ component: (props?: any) => Node | Node[] | null,
191
+ target: string | Element
192
+ ): () => void;
193
+
194
+ /**
195
+ * Recursively disposes scopes and removes tracked event listeners on a node
196
+ * and its descendants, then detaches it from the DOM.
197
+ *
198
+ * Only needed when working with nodes created outside `h()`.
199
+ */
200
+ export function unmount(node: Node): void;
201
+
202
+ // ============================================================================
203
+ // Router
204
+ // ============================================================================
205
+
206
+ export interface RouteDefinition {
207
+ /** Path pattern: `/`, `/user/:id`, `/blog/*`, or `*` for fallback. */
208
+ path: string;
209
+ /** Component function receiving route params, or a static node. */
210
+ component: ((params: Record<string, string>) => Node | Node[] | null) | Node;
211
+ }
212
+
213
+ export interface RouterFn {
214
+ (): HTMLElement;
215
+ /** Navigate to a path (updates `window.location.hash`). */
216
+ to(path: string): void;
217
+ /** Go back in browser history. */
218
+ back(): void;
219
+ /** Returns the current path (without leading `#`). */
220
+ path(): string;
221
+ }
222
+
223
+ /**
224
+ * Creates a hash-based router. Returns a component that renders the
225
+ * matched route and re-renders on navigation.
226
+ */
227
+ export function router(routes: RouteDefinition[]): RouterFn;
228
+
229
+ /** Current path as a reactive signal (without leading `#`). */
230
+ export const currentPath: Signal<string>;
231
+
232
+ /** Current route params as a reactive signal. */
233
+ export const routerParams: Signal<Record<string, string>>;
234
+
235
+ // ============================================================================
236
+ // i18n
237
+ // ============================================================================
238
+
239
+ /** Active locale. Reactive. */
240
+ export const currentLocale: Signal<string>;
241
+
242
+ /**
243
+ * Registers translations for one or more locales.
244
+ *
245
+ * @example
246
+ * addLang({ en: { hello: "Hello" }, es: { hello: "Hola" } });
247
+ */
248
+ export function addLang(translations: Record<string, Record<string, string>>): void;
249
+
250
+ /** Sets the active locale if it has been registered. */
251
+ export function setLocale(locale: string): void;
252
+
253
+ /** Reactive translation getter for a key. */
254
+ export function t(key: string): () => string;
255
+
256
+ /** Imperative translation read for a key. */
257
+ export function tt(key: string): string;
258
+
259
+ // ============================================================================
260
+ // HTTP Helper
261
+ // ============================================================================
262
+
263
+ /**
264
+ * JSON fetch helper. POSTs when `data` is provided, GETs otherwise.
265
+ * Calls `loading(true/false)` around the request if provided.
266
+ * Pass an `AbortSignal` to cancel.
267
+ */
268
+ export function db<T = any>(
269
+ url: string,
270
+ data?: any | null,
271
+ loading?: ((loading: boolean) => void) | null,
272
+ signal?: AbortSignal | null
273
+ ): Promise<T>;
274
+
275
+ // ============================================================================
276
+ // Tag Helpers (registered by `exposeTags`)
277
+ // ============================================================================
278
+
279
+ /**
280
+ * A tag helper: `div(props?, ...children)`.
281
+ * Available on `window` after calling `exposeTags()`.
282
+ */
283
+ export type TagHelper = (props?: Props | Child | null, ...children: Child[]) => Element;
284
+
285
+ export const a: TagHelper;
286
+ export const abbr: TagHelper;
287
+ export const article: TagHelper;
288
+ export const aside: TagHelper;
289
+ export const audio: TagHelper;
290
+ export const b: TagHelper;
291
+ export const blockquote: TagHelper;
292
+ export const br: TagHelper;
293
+ export const button: TagHelper;
294
+ export const canvas: TagHelper;
295
+ export const caption: TagHelper;
296
+ export const cite: TagHelper;
297
+ export const code: TagHelper;
298
+ export const col: TagHelper;
299
+ export const colgroup: TagHelper;
300
+ export const datalist: TagHelper;
301
+ export const dd: TagHelper;
302
+ export const del: TagHelper;
303
+ export const details: TagHelper;
304
+ export const dfn: TagHelper;
305
+ export const dialog: TagHelper;
306
+ export const div: TagHelper;
307
+ export const dl: TagHelper;
308
+ export const dt: TagHelper;
309
+ export const em: TagHelper;
310
+ export const embed: TagHelper;
311
+ export const fieldset: TagHelper;
312
+ export const figcaption: TagHelper;
313
+ export const figure: TagHelper;
314
+ export const footer: TagHelper;
315
+ export const form: TagHelper;
316
+ export const h1: TagHelper;
317
+ export const h2: TagHelper;
318
+ export const h3: TagHelper;
319
+ export const h4: TagHelper;
320
+ export const h5: TagHelper;
321
+ export const h6: TagHelper;
322
+ export const header: TagHelper;
323
+ export const hr: TagHelper;
324
+ export const i: TagHelper;
325
+ export const iframe: TagHelper;
326
+ export const img: TagHelper;
327
+ export const input: TagHelper;
328
+ export const ins: TagHelper;
329
+ export const kbd: TagHelper;
330
+ export const label: TagHelper;
331
+ export const legend: TagHelper;
332
+ export const li: TagHelper;
333
+ export const main: TagHelper;
334
+ export const mark: TagHelper;
335
+ export const meter: TagHelper;
336
+ export const nav: TagHelper;
337
+ export const object: TagHelper;
338
+ export const ol: TagHelper;
339
+ export const optgroup: TagHelper;
340
+ export const option: TagHelper;
341
+ export const output: TagHelper;
342
+ export const p: TagHelper;
343
+ export const picture: TagHelper;
344
+ export const pre: TagHelper;
345
+ export const progress: TagHelper;
346
+ export const section: TagHelper;
347
+ export const select: TagHelper;
348
+ export const slot: TagHelper;
349
+ export const small: TagHelper;
350
+ export const source: TagHelper;
351
+ export const span: TagHelper;
352
+ export const strong: TagHelper;
353
+ export const sub: TagHelper;
354
+ export const summary: TagHelper;
355
+ export const sup: TagHelper;
356
+ export const svg: TagHelper;
357
+ export const table: TagHelper;
358
+ export const tbody: TagHelper;
359
+ export const td: TagHelper;
360
+ export const template: TagHelper;
361
+ export const textarea: TagHelper;
362
+ export const tfoot: TagHelper;
363
+ export const th: TagHelper;
364
+ export const thead: TagHelper;
365
+ export const time: TagHelper;
366
+ export const tr: TagHelper;
367
+ export const u: TagHelper;
368
+ export const ul: TagHelper;
369
+ export const video: TagHelper;
370
+
371
+ // ============================================================================
372
+ // Aggregate Export
373
+ // ============================================================================
374
+
375
+ declare const SigPro: {
376
+ signal: typeof signal;
377
+ computed: typeof computed;
378
+ effect: typeof effect;
379
+ effectScope: typeof effectScope;
380
+ untrack: typeof untrack;
381
+ batch: typeof batch;
382
+ watch: typeof watch;
383
+ local: typeof local;
384
+ provide: typeof provide;
385
+ inject: typeof inject;
386
+ h: typeof h;
387
+ mount: typeof mount;
388
+ unmount: typeof unmount;
389
+ exposeTags: typeof exposeTags;
390
+ router: typeof router;
391
+ currentPath: typeof currentPath;
392
+ routerParams: typeof routerParams;
393
+ currentLocale: typeof currentLocale;
394
+ addLang: typeof addLang;
395
+ setLocale: typeof setLocale;
396
+ t: typeof t;
397
+ tt: typeof tt;
398
+ db: typeof db;
399
+ };
400
+
401
+ export default SigPro;
402
+
403
+ // ============================================================================
404
+ // Global augmentation for browser environments
405
+ // ============================================================================
406
+
407
+ declare global {
408
+ interface Window {
409
+ SigPro: typeof SigPro;
410
+
411
+ signal: typeof signal;
412
+ computed: typeof computed;
413
+ effect: typeof effect;
414
+ effectScope: typeof effectScope;
415
+ untrack: typeof untrack;
416
+ batch: typeof batch;
417
+ watch: typeof watch;
418
+ local: typeof local;
419
+ provide: typeof provide;
420
+ inject: typeof inject;
421
+ h: typeof h;
422
+ mount: typeof mount;
423
+ unmount: typeof unmount;
424
+ exposeTags: typeof exposeTags;
425
+ router: typeof router;
426
+ currentPath: typeof currentPath;
427
+ routerParams: typeof routerParams;
428
+ currentLocale: typeof currentLocale;
429
+ addLang: typeof addLang;
430
+ setLocale: typeof setLocale;
431
+ t: typeof t;
432
+ tt: typeof tt;
433
+ db: typeof db;
434
+
435
+ // Tag helpers (lowercase)
436
+ a: TagHelper;
437
+ abbr: TagHelper;
438
+ article: TagHelper;
439
+ aside: TagHelper;
440
+ audio: TagHelper;
441
+ b: TagHelper;
442
+ blockquote: TagHelper;
443
+ br: TagHelper;
444
+ button: TagHelper;
445
+ canvas: TagHelper;
446
+ caption: TagHelper;
447
+ cite: TagHelper;
448
+ code: TagHelper;
449
+ col: TagHelper;
450
+ colgroup: TagHelper;
451
+ datalist: TagHelper;
452
+ dd: TagHelper;
453
+ del: TagHelper;
454
+ details: TagHelper;
455
+ dfn: TagHelper;
456
+ dialog: TagHelper;
457
+ div: TagHelper;
458
+ dl: TagHelper;
459
+ dt: TagHelper;
460
+ em: TagHelper;
461
+ embed: TagHelper;
462
+ fieldset: TagHelper;
463
+ figcaption: TagHelper;
464
+ figure: TagHelper;
465
+ footer: TagHelper;
466
+ form: TagHelper;
467
+ h1: TagHelper;
468
+ h2: TagHelper;
469
+ h3: TagHelper;
470
+ h4: TagHelper;
471
+ h5: TagHelper;
472
+ h6: TagHelper;
473
+ header: TagHelper;
474
+ hr: TagHelper;
475
+ i: TagHelper;
476
+ iframe: TagHelper;
477
+ img: TagHelper;
478
+ input: TagHelper;
479
+ ins: TagHelper;
480
+ kbd: TagHelper;
481
+ label: TagHelper;
482
+ legend: TagHelper;
483
+ li: TagHelper;
484
+ main: TagHelper;
485
+ mark: TagHelper;
486
+ meter: TagHelper;
487
+ nav: TagHelper;
488
+ object: TagHelper;
489
+ ol: TagHelper;
490
+ optgroup: TagHelper;
491
+ option: TagHelper;
492
+ output: TagHelper;
493
+ p: TagHelper;
494
+ picture: TagHelper;
495
+ pre: TagHelper;
496
+ progress: TagHelper;
497
+ section: TagHelper;
498
+ select: TagHelper;
499
+ slot: TagHelper;
500
+ small: TagHelper;
501
+ source: TagHelper;
502
+ span: TagHelper;
503
+ strong: TagHelper;
504
+ sub: TagHelper;
505
+ summary: TagHelper;
506
+ sup: TagHelper;
507
+ svg: TagHelper;
508
+ table: TagHelper;
509
+ tbody: TagHelper;
510
+ td: TagHelper;
511
+ template: TagHelper;
512
+ textarea: TagHelper;
513
+ tfoot: TagHelper;
514
+ th: TagHelper;
515
+ thead: TagHelper;
516
+ time: TagHelper;
517
+ tr: TagHelper;
518
+ u: TagHelper;
519
+ ul: TagHelper;
520
+ video: TagHelper;
521
+ }
522
+ }