sigpro 2.0.3 → 2.0.6

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 CHANGED
@@ -1,13 +1,8 @@
1
1
  /**
2
2
  * SigPro — Type Definitions
3
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
4
+ * Minimal reactive library: alien-signals-class push-pull core + direct-to-DOM
5
+ * rendering. No VDOM, no diffing, no build step.
11
6
  */
12
7
 
13
8
  // ============================================================================
@@ -17,65 +12,40 @@
17
12
  /**
18
13
  * A reactive signal. Call with no args to read, with a value or an updater
19
14
  * 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
15
  */
27
16
  export interface Signal<T> {
28
17
  (): T;
29
18
  (value: T | ((prev: T) => T)): void;
30
19
  }
31
20
 
32
- /**
33
- * Creates a reactive signal with an initial value.
34
- */
35
21
  export function signal<T>(initialValue: T): Signal<T>;
36
22
 
37
- /**
38
- * A read-only derived value. Call with no args to read.
39
- */
23
+ /** A read-only derived value. Call with no args to read. */
40
24
  export type ReadonlySignal<T> = () => T;
41
25
 
42
- /**
43
- * Creates a derived value computed lazily from dependencies.
44
- * Recomputed on read only when dependencies have actually changed.
45
- */
46
26
  export function computed<T>(getter: (oldValue?: T) => T): ReadonlySignal<T>;
47
27
 
48
28
  /**
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.
29
+ * Runs `fn` reactively. Re-executes when tracked deps change. `fn` may
30
+ * return a cleanup function, run before the next re-execution and on stop.
54
31
  */
55
32
  export function effect(fn: () => void | (() => void)): () => void;
56
33
 
57
34
  /**
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.
35
+ * Groups child effects in a scope. Does not track deps and does not
36
+ * re-execute. Returns a disposer for all effects created inside.
61
37
  */
62
38
  export function effectScope(fn: () => void): () => void;
63
39
 
64
- /**
65
- * Runs `fn` without tracking signal reads as dependencies.
66
- */
40
+ /** Runs `fn` without tracking reads as dependencies. */
67
41
  export function untrack<T>(fn: () => T): T;
68
42
 
69
- /**
70
- * Batches multiple signal writes into a single flush.
71
- */
43
+ /** Batches multiple signal writes into a single flush. */
72
44
  export function batch<T>(fn: () => T): T;
73
45
 
74
46
  /**
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.
47
+ * Watches a signal/getter and calls `cb` when the value changes. Skips the
48
+ * initial run by default; pass `{ immediate: true }` to fire once on setup.
79
49
  */
80
50
  export function watch<T>(
81
51
  source: Signal<T> | ReadonlySignal<T>,
@@ -84,59 +54,42 @@ export function watch<T>(
84
54
  ): () => void;
85
55
 
86
56
  /**
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.
57
+ * Signal persisted to `localStorage` under `key` (JSON-serialized).
58
+ * Falls back to `initial` if unavailable or unparseable.
90
59
  */
91
60
  export function local<T>(key: string, initial: T): Signal<T>;
92
61
 
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
62
  // ============================================================================
110
63
  // DOM Creation
111
64
  // ============================================================================
112
65
 
113
- /**
114
- * A prop value may be static or a reactive getter.
115
- */
66
+ /** Static value or reactive getter. */
116
67
  export type PropValue<T = any> = T | (() => T);
117
68
 
118
69
  /**
119
- * Props accepted by `h` and all tag helpers.
70
+ * Props accepted by `h` and tag helpers.
120
71
  *
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)
72
+ * Special keys:
73
+ * - `ref`: function or `{ current }` object
74
+ * - `html`: sets `innerHTML` reactively (children ignored when present)
75
+ * - `on*`: DOM event listeners (case-sensitive after `on`: `onclick`, `onInput`)
76
+ * - `class` / `className`: element class
77
+ * - `style`: CSS string or object of properties
78
+ * - anything else: assigned as property if present on the element,
79
+ * otherwise set as attribute
126
80
  */
127
81
  export type Props = {
128
82
  ref?: ((el: Element) => void) | { current: Element | null };
129
83
  html?: PropValue<string | null | undefined>;
130
84
  class?: PropValue<string | null | undefined>;
131
85
  className?: PropValue<string | null | undefined>;
132
- style?: PropValue<string | null | undefined>;
86
+ style?: PropValue<
87
+ string | Record<string, string | number | null | false> | null | undefined
88
+ >;
133
89
  [key: string]: PropValue<any>;
134
90
  };
135
91
 
136
- /**
137
- * Any value accepted as a child: nodes, strings, numbers, arrays, or
138
- * reactive getter functions returning any of these.
139
- */
92
+ /** Any valid child: node, primitive, array, or a reactive getter. */
140
93
  export type Child =
141
94
  | Node
142
95
  | string
@@ -148,18 +101,15 @@ export type Child =
148
101
  | Child[];
149
102
 
150
103
  /**
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.
104
+ * Creates an HTML/SVG element (tag: string) or renders a component
105
+ * (tag: function). The first arg after `tag` may be children when it is not
106
+ * a plain object.
157
107
  */
158
108
  export function h(
159
109
  tag: string,
160
110
  props?: Props | Child | null,
161
111
  ...children: Child[]
162
- ): Element | SVGElement;
112
+ ): Element;
163
113
 
164
114
  export function h<P extends object>(
165
115
  tag: (props: P, children: Child[]) => Node | Node[] | null,
@@ -168,10 +118,8 @@ export function h<P extends object>(
168
118
  ): Node | Node[] | null;
169
119
 
170
120
  /**
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`.
121
+ * Registers all standard HTML tag names as globals on `target` (default
122
+ * `window`). Call once at startup.
175
123
  */
176
124
  export function exposeTags(target?: any): void;
177
125
 
@@ -180,22 +128,17 @@ export function exposeTags(target?: any): void;
180
128
  // ============================================================================
181
129
 
182
130
  /**
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.
131
+ * Mounts a component or node into a target. Returns a disposer that stops
132
+ * the app scope and clears the target.
188
133
  */
189
134
  export function mount(
190
- component: (props?: any) => Node | Node[] | null,
135
+ component: ((props?: any) => Node | Node[] | null) | Node | string,
191
136
  target: string | Element
192
137
  ): () => void;
193
138
 
194
139
  /**
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()`.
140
+ * Recursively disposes scopes, removes tracked event listeners, and detaches
141
+ * the node. Only needed for nodes created outside `h()`.
199
142
  */
200
143
  export function unmount(node: Node): void;
201
144
 
@@ -204,10 +147,11 @@ export function unmount(node: Node): void;
204
147
  // ============================================================================
205
148
 
206
149
  export interface RouteDefinition {
207
- /** Path pattern: `/`, `/user/:id`, `/blog/*`, or `*` for fallback. */
150
+ /** `/`, `/user/:id`, `/blog/*`, or `*` for fallback. */
208
151
  path: string;
209
- /** Component function receiving route params, or a static node. */
210
- component: ((params: Record<string, string>) => Node | Node[] | null) | Node;
152
+ component:
153
+ | ((params: Record<string, string>) => Node | Node[] | null)
154
+ | Node;
211
155
  }
212
156
 
213
157
  export interface RouterFn {
@@ -216,14 +160,11 @@ export interface RouterFn {
216
160
  to(path: string): void;
217
161
  /** Go back in browser history. */
218
162
  back(): void;
219
- /** Returns the current path (without leading `#`). */
163
+ /** Current path (without leading `#`). */
220
164
  path(): string;
221
165
  }
222
166
 
223
- /**
224
- * Creates a hash-based router. Returns a component that renders the
225
- * matched route and re-renders on navigation.
226
- */
167
+ /** Creates a hash-based router component. */
227
168
  export function router(routes: RouteDefinition[]): RouterFn;
228
169
 
229
170
  /** Current path as a reactive signal (without leading `#`). */
@@ -232,6 +173,9 @@ export const currentPath: Signal<string>;
232
173
  /** Current route params as a reactive signal. */
233
174
  export const routerParams: Signal<Record<string, string>>;
234
175
 
176
+ /** Increments on HMR page registration; read to force route refresh. */
177
+ export const hmrTick: Signal<number>;
178
+
235
179
  // ============================================================================
236
180
  // i18n
237
181
  // ============================================================================
@@ -245,7 +189,7 @@ export const currentLocale: Signal<string>;
245
189
  * @example
246
190
  * addLang({ en: { hello: "Hello" }, es: { hello: "Hola" } });
247
191
  */
248
- export function addLang(translations: Record<string, Record<string, string>>): void;
192
+ export function addLang(obj: Record<string, Record<string, string>>): void;
249
193
 
250
194
  /** Sets the active locale if it has been registered. */
251
195
  export function setLocale(locale: string): void;
@@ -253,34 +197,48 @@ export function setLocale(locale: string): void;
253
197
  /** Reactive translation getter for a key. */
254
198
  export function t(key: string): () => string;
255
199
 
256
- /** Imperative translation read for a key. */
257
- export function tt(key: string): string;
258
-
259
200
  // ============================================================================
260
201
  // HTTP Helper
261
202
  // ============================================================================
262
203
 
263
204
  /**
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.
205
+ * JSON fetch helper. POSTs when `data` is provided, GETs otherwise. Calls
206
+ * `loading(true/false)` around the request. Pass `abortSignal` to cancel.
267
207
  */
268
208
  export function db<T = any>(
269
209
  url: string,
270
210
  data?: any | null,
271
211
  loading?: ((loading: boolean) => void) | null,
272
- signal?: AbortSignal | null
212
+ abortSignal?: AbortSignal | null
273
213
  ): Promise<T>;
274
214
 
275
215
  // ============================================================================
276
- // Tag Helpers (registered by `exposeTags`)
216
+ // Universal factory
277
217
  // ============================================================================
278
218
 
279
219
  /**
280
- * A tag helper: `div(props?, ...children)`.
281
- * Available on `window` after calling `exposeTags()`.
220
+ * Universal reactive factory:
221
+ * - called with a getter → wrapped in `computed()`
222
+ * - called with a value + key → `local(key, value)`
223
+ * - called with a value → `signal(value)`
224
+ *
225
+ * @example
226
+ * const count = $(0); // signal
227
+ * const theme = $('dark', 'app.theme'); // localStorage signal
228
+ * const double = $(() => count() * 2); // computed
282
229
  */
283
- export type TagHelper = (props?: Props | Child | null, ...children: Child[]) => Element;
230
+ export function $<T>(getter: (oldValue?: T) => T): ReadonlySignal<T>;
231
+ export function $<T>(value: T, key: string): Signal<T>;
232
+ export function $<T>(value: T): Signal<T>;
233
+
234
+ // ============================================================================
235
+ // Tag Helpers (registered by `exposeTags`)
236
+ // ============================================================================
237
+
238
+ export type TagHelper = (
239
+ props?: Props | Child | null,
240
+ ...children: Child[]
241
+ ) => Element;
284
242
 
285
243
  export const a: TagHelper;
286
244
  export const abbr: TagHelper;
@@ -381,8 +339,6 @@ declare const SigPro: {
381
339
  batch: typeof batch;
382
340
  watch: typeof watch;
383
341
  local: typeof local;
384
- provide: typeof provide;
385
- inject: typeof inject;
386
342
  h: typeof h;
387
343
  mount: typeof mount;
388
344
  unmount: typeof unmount;
@@ -390,18 +346,19 @@ declare const SigPro: {
390
346
  router: typeof router;
391
347
  currentPath: typeof currentPath;
392
348
  routerParams: typeof routerParams;
349
+ hmrTick: typeof hmrTick;
393
350
  currentLocale: typeof currentLocale;
394
351
  addLang: typeof addLang;
395
352
  setLocale: typeof setLocale;
396
353
  t: typeof t;
397
- tt: typeof tt;
398
354
  db: typeof db;
355
+ $: typeof $;
399
356
  };
400
357
 
401
358
  export default SigPro;
402
359
 
403
360
  // ============================================================================
404
- // Global augmentation for browser environments
361
+ // Global augmentation
405
362
  // ============================================================================
406
363
 
407
364
  declare global {
@@ -416,8 +373,6 @@ declare global {
416
373
  batch: typeof batch;
417
374
  watch: typeof watch;
418
375
  local: typeof local;
419
- provide: typeof provide;
420
- inject: typeof inject;
421
376
  h: typeof h;
422
377
  mount: typeof mount;
423
378
  unmount: typeof unmount;
@@ -425,98 +380,34 @@ declare global {
425
380
  router: typeof router;
426
381
  currentPath: typeof currentPath;
427
382
  routerParams: typeof routerParams;
383
+ hmrTick: typeof hmrTick;
428
384
  currentLocale: typeof currentLocale;
429
385
  addLang: typeof addLang;
430
386
  setLocale: typeof setLocale;
431
387
  t: typeof t;
432
- tt: typeof tt;
433
388
  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;
389
+ $: typeof $;
390
+
391
+ a: TagHelper; abbr: TagHelper; article: TagHelper; aside: TagHelper;
392
+ audio: TagHelper; b: TagHelper; blockquote: TagHelper; br: TagHelper;
393
+ button: TagHelper; canvas: TagHelper; caption: TagHelper; cite: TagHelper;
394
+ code: TagHelper; col: TagHelper; colgroup: TagHelper; datalist: TagHelper;
395
+ dd: TagHelper; del: TagHelper; details: TagHelper; dfn: TagHelper;
396
+ dialog: TagHelper; div: TagHelper; dl: TagHelper; dt: TagHelper;
397
+ em: TagHelper; embed: TagHelper; fieldset: TagHelper; figcaption: TagHelper;
398
+ figure: TagHelper; footer: TagHelper; form: TagHelper; h1: TagHelper;
399
+ h2: TagHelper; h3: TagHelper; h4: TagHelper; h5: TagHelper; h6: TagHelper;
400
+ header: TagHelper; hr: TagHelper; i: TagHelper; iframe: TagHelper;
401
+ img: TagHelper; input: TagHelper; ins: TagHelper; kbd: TagHelper;
402
+ label: TagHelper; legend: TagHelper; li: TagHelper; main: TagHelper;
403
+ mark: TagHelper; meter: TagHelper; nav: TagHelper; object: TagHelper;
404
+ ol: TagHelper; optgroup: TagHelper; option: TagHelper; output: TagHelper;
405
+ p: TagHelper; picture: TagHelper; pre: TagHelper; progress: TagHelper;
406
+ section: TagHelper; select: TagHelper; slot: TagHelper; small: TagHelper;
407
+ source: TagHelper; span: TagHelper; strong: TagHelper; sub: TagHelper;
408
+ summary: TagHelper; sup: TagHelper; svg: TagHelper; table: TagHelper;
409
+ tbody: TagHelper; td: TagHelper; template: TagHelper; textarea: TagHelper;
410
+ tfoot: TagHelper; th: TagHelper; thead: TagHelper; time: TagHelper;
411
+ tr: TagHelper; u: TagHelper; ul: TagHelper; video: TagHelper;
521
412
  }
522
413
  }