@but212/atom-effect-jquery 0.26.0 → 0.28.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.
package/dist/index.d.ts CHANGED
@@ -1,70 +1,28 @@
1
- import { atom } from '@but212/atom-effect';
2
- import { batch } from '@but212/atom-effect';
3
- import { computed } from '@but212/atom-effect';
4
1
  import { ComputedAtom } from '@but212/atom-effect';
5
2
  import { default as default_2 } from 'jquery';
6
- import { effect } from '@but212/atom-effect';
7
3
  import { EffectObject } from '@but212/atom-effect';
8
- import { isAtom } from '@but212/atom-effect';
9
- import { isComputed } from '@but212/atom-effect';
10
4
  import { ReadonlyAtom } from '@but212/atom-effect';
11
- import { untracked } from '@but212/atom-effect';
12
5
  import { WritableAtom } from '@but212/atom-effect';
13
6
 
14
7
  /**
15
- * Represents a value that can be a synchronous `ReactiveValue<T>`,
16
- * a `Promise<T>`, or an Atom yielding `T | Promise<T>`.
8
+ * An extension of ReactiveValue that also supports Promises and async functions.
9
+ * The binding system automatically handles the promise lifecycle, showing the
10
+ * latest resolved value and ignoring stale ones (race condition protection).
17
11
  */
18
12
  declare type AsyncReactiveValue<T> = T | ReadonlyAtom<T | Promise<T>> | Promise<T> | (() => T | Promise<T>);
19
13
 
20
- export { atom }
21
-
22
- /**
23
- * Creates a two-way "lens" for a specific property path on an object-based atom.
24
- * Optimized for performance using structural sharing and equality guards.
25
- *
26
- * This "fake" atom allows fine-grained binding to deep properties of a
27
- * monolithic state atom without extra memory or complex computed logic.
28
- *
29
- * @param atom The source atom containing the object.
30
- * @param path Dot-separated path to the property (e.g. 'user.profile.name').
31
- * @returns A WritableAtom that reads from and writes to the specified path.
32
- */
33
- export declare function atomLens<T extends object, P extends Paths<T>>(atom: WritableAtom<T>, path: P): WritableAtom<PathValue<T, P>>;
34
-
35
- export { batch }
36
-
37
- /**
38
- * Configuration options for `atomBind`.
39
- * @template T Type of the value for two-way binding (`val` field).
40
- */
41
14
  export declare interface BindingOptions<T = unknown> {
42
- /** Binds textContent to any reactive source. */
43
15
  text?: AsyncReactiveValue<unknown>;
44
- /** Binds innerHTML to a reactive string source (sanitized). */
45
16
  html?: AsyncReactiveValue<string>;
46
- /** Map of class names to reactive boolean conditions. */
47
17
  class?: Record<string, AsyncReactiveValue<boolean>>;
48
- /** Map of CSS properties to reactive values or [value, unit] tuples. */
49
18
  css?: CssBindings;
50
- /** Binds attributes with consistent primitive constraints. */
51
19
  attr?: Record<string, AsyncReactiveValue<PrimitiveValue>>;
52
- /** Binds DOM properties. */
53
20
  prop?: Record<string, AsyncReactiveValue<unknown>>;
54
- /** Direct visibility control (display: none). */
55
21
  show?: AsyncReactiveValue<boolean>;
56
- /** Inverse visibility control. */
57
22
  hide?: AsyncReactiveValue<boolean>;
58
- /**
59
- * Two-way binding for input values.
60
- * Pass an atom or a `[atom, options]` tuple.
61
- */
62
23
  val?: WritableAtom<T> | [atom: WritableAtom<T>, options: ValOptions<T>];
63
- /** Two-way binding for checkboxes and radio buttons. */
64
24
  checked?: WritableAtom<boolean>;
65
- /** Fully automated two-way form binding using name attributes. */
66
25
  form?: WritableAtom<T extends object ? T : unknown>;
67
- /** Event listeners with automatic batched execution and lifecycle-bound cleanup. */
68
26
  on?: Record<string, (e: JQuery.Event) => void>;
69
27
  }
70
28
 
@@ -85,8 +43,26 @@ declare class BindingRegistry {
85
43
  markIgnored(node: Node): void;
86
44
  isIgnored(node: Node): boolean;
87
45
  private getOrCreateRecord;
46
+ /**
47
+ * Registers a reactive effect with an element's record.
48
+ * Effects are automatically disposed when the element is removed from the DOM.
49
+ *
50
+ * @param el - The DOM element to bind the effect to.
51
+ * @param fx - The reactive effect instance.
52
+ */
88
53
  trackEffect(el: Element, fx: EffectObject): void;
54
+ /**
55
+ * Registers an arbitrary cleanup function with an element's record.
56
+ * Cleanups are executed when the element is removed from the DOM.
57
+ *
58
+ * @param el - The DOM element to bind the cleanup to.
59
+ * @param fn - The cleanup function (e.g., event unbinding, timer clear).
60
+ */
89
61
  trackCleanup(el: Element, fn: () => void): void;
62
+ /**
63
+ * Assigns a component-level cleanup function (e.g., from atomMount).
64
+ * Unlike generic cleanups, there can only be one component cleanup per element.
65
+ */
90
66
  setComponentCleanup(el: Element, fn: (() => void) | undefined): void;
91
67
  hasBind(el: Element): boolean;
92
68
  cleanup(el: Element | Node): void;
@@ -94,34 +70,12 @@ declare class BindingRegistry {
94
70
  cleanupTree(el: Element | Node): void;
95
71
  }
96
72
 
97
- /**
98
- * A function that initializes logic on a jQuery element and returns an optional cleanup function.
99
- * `P` defaults to `Record<string, unknown>` for convenience. Use `P = Record<string, never>`
100
- * for strictly no-props components.
101
- */
102
73
  export declare type ComponentFn<P = Record<string, unknown>> = ($el: JQuery, props: P) => EffectResult;
103
74
 
104
- /**
105
- * Composes an existing lens with a sub-path to create a deeper lens.
106
- *
107
- * @param lens The parent lens.
108
- * @param path Sub-path relative to the parent lens.
109
- * @returns A new lens pointing to the deeper path.
110
- */
111
- export declare function composeLens<T extends object, P extends Paths<T>>(lens: WritableAtom<T>, path: P): WritableAtom<PathValue<T, P>>;
112
-
113
- export { computed }
114
-
115
75
  export { ComputedAtom }
116
76
 
117
- /**
118
- * CSS bindings map property names to CSS values.
119
- */
120
77
  export declare type CssBindings = Record<string, CssValue>;
121
78
 
122
- /**
123
- * CSS value: either a direct reactive value or a numeric tuple of [source, unit].
124
- */
125
79
  export declare type CssValue = AsyncReactiveValue<string | number> | [source: AsyncReactiveValue<number>, unit: string];
126
80
 
127
81
  export default default_2;
@@ -137,17 +91,8 @@ export declare function disableAutoCleanup(): void;
137
91
  */
138
92
  export declare function disablejQueryOverrides(): void;
139
93
 
140
- export { effect }
141
-
142
- /**
143
- * Cleanup function returned by effects or components.
144
- */
145
94
  export declare type EffectCleanup = () => void;
146
95
 
147
- /**
148
- * Result of a reactive factory or component mount.
149
- * Returns `void` (no cleanup) or an `EffectCleanup` function.
150
- */
151
96
  export declare type EffectResult = undefined | EffectCleanup;
152
97
 
153
98
  /**
@@ -162,162 +107,55 @@ export declare type EffectResult = undefined | EffectCleanup;
162
107
  */
163
108
  export declare function enableAutoCleanup(root: Element): void;
164
109
 
165
- /**
166
- * Patches jQuery's `.on()`, `.off()`, `.remove()`, `.empty()`, and `.detach()`
167
- * to integrate with the reactive system:
168
- * - Event handlers are wrapped in `batch()` for efficient atom flushing.
169
- * - DOM removal triggers reactive binding cleanup.
170
- * - `.detach()` preserves bindings for re-attachment.
171
- *
172
- * Idempotent — calling more than once has no effect.
173
- * Call `disablejQueryOverrides()` to restore original methods.
174
- */
175
110
  export declare function enablejQueryOverrides(): void;
176
111
 
177
- /**
178
- * Generic equality predicate shared by `ValOptions` and any future consumer.
179
- * Extracted as a named type to avoid duplicating the inline function signature.
180
- */
181
112
  export declare type EqualFn<T> = (a: T, b: T) => boolean;
182
113
 
183
- /**
184
- * Configuration options for `atomFetch`.
185
- */
186
114
  export declare interface FetchOptions<T> {
187
- /** Initial value before the first fetch resolves. */
188
115
  defaultValue: T;
189
- /** HTTP method (default: 'GET'). */
190
116
  method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS' | (string & {});
191
- /** HTTP headers. */
192
117
  headers?: Record<string, string>;
193
- /** Transforms the raw response into T. */
194
118
  transform?: (raw: unknown) => T;
195
- /** Additional `$.ajax` settings. Can be a getter function for reactive data tracking. */
196
119
  ajaxOptions?: JQuery.AjaxSettings | (() => JQuery.AjaxSettings);
197
- /** Error callback. */
198
120
  onError?: (err: unknown) => void;
199
- /** Whether to fetch immediately (default: true). */
200
121
  eager?: boolean;
201
122
  }
202
123
 
203
- export { isAtom }
204
-
205
- export { isComputed }
206
-
207
- /**
208
- * Checks if a given value is a reactive node (Atom or Computed).
209
- *
210
- * `isAtom` returns `true` for both plain atoms and computed atoms because
211
- * `ComputedAtomImpl` carries `ATOM_BRAND` in addition to `COMPUTED_BRAND`.
212
- * A separate `isComputed` check would therefore be redundant.
213
- */
214
- export declare function isReactive(value: unknown): value is ReadonlyAtom<unknown>;
124
+ /** Checks if a given value is a reactive node (Atom or Computed). */
125
+ export declare const isReactive: (v: unknown) => v is ReadonlyAtom<unknown>;
215
126
 
216
- /**
217
- * Helper to extract keys of T whose values extend V.
218
- * Used to ensure `key` property refers to valid ID-like values.
219
- */
220
127
  declare type KeysOfType<T, V> = {
221
128
  [K in keyof T]: T[K] extends V ? K : never;
222
129
  }[keyof T];
223
130
 
224
- /**
225
- * Creates a lens factory bound to a specific atom.
226
- * Eliminates the need to pass the atom reference on every call.
227
- *
228
- * @example
229
- * const lens = lensFor(userAtom);
230
- * const email = lens('settings.notifications.email'); // WritableAtom<boolean>
231
- */
232
- export declare function lensFor<T extends object>(atom: WritableAtom<T>): <P extends Paths<T>>(path: P) => WritableAtom<PathValue<T, P>>;
233
-
234
- /** Key type for Map/Set inside list.ts */
235
131
  declare type ListKey = string | number;
236
132
 
237
- /** Key extractor function signature. */
238
133
  declare type ListKeyFn<T> = (item: T, index: number) => ListKey;
239
134
 
240
- /**
241
- * Configuration options for `atomList`.
242
- */
243
135
  export declare interface ListOptions<T> {
244
- /**
245
- * Key to track items. Must be a property name whose value is a string|number,
246
- * or a key extractor function.
247
- */
248
136
  key: KeysOfType<T, ListKey> | ListKeyFn<T>;
249
- /** Render function for each item. */
250
137
  render: (item: T, index: number) => ListRenderResult;
251
- /** Optional post-render binding logic. */
252
138
  bind?: ($el: JQuery, item: T, index: number) => void;
253
- /** Optional update logic when item data changes but DOM is reused. */
254
139
  update?: ($el: JQuery, item: T, index: number) => void;
255
- /** Lifecycle hook: called when an element is added to the list. */
256
140
  onAdd?: ($el: JQuery) => void;
257
- /** Lifecycle hook: called when an element is about to be removed. */
258
141
  onRemove?: ($el: JQuery) => Promise<void> | void;
259
- /** Content to show when the list is empty. */
260
142
  empty?: ListRenderResult;
261
- /** Delegated event handlers attached to the container. */
262
143
  events?: Record<string, (item: T, index: number, e: JQuery.TriggeredEvent) => void>;
263
- /**
264
- * Custom equality checker to determine if an item has changed.
265
- * Defaults to `shallowEqual`. If it returns false, the item is re-rendered (unless `update` is provided).
266
- */
267
144
  isEqual?: (a: T, b: T) => boolean;
268
145
  }
269
146
 
270
- /** Possible return types for render() / empty */
271
147
  declare type ListRenderResult = string | Element | DocumentFragment | JQuery;
272
148
 
273
- /**
274
- * Maximum recursion depth for path generation.
275
- * Prevents TypeScript compiler from hitting recursion limits on deeply nested types.
276
- */
277
- declare type MaxDepth = 8;
149
+ /** Resolves after microtask effects flush. Fast Promise-based scheduling. */
150
+ export declare const nextTick: () => Promise<void>;
278
151
 
279
- /**
280
- * Resolves after all pending microtask-scheduled reactive effects have flushed.
281
- *
282
- * Implementation uses `setTimeout(0)` (a macrotask) which always runs after
283
- * the current microtask queue is drained. This is intentional: core's
284
- * scheduler enqueues effects as microtasks, so by the time the macrotask
285
- * fires, all pending reactive propagation for the current turn is complete.
286
- *
287
- * Note: browsers may enforce a minimum 4 ms delay for nested `setTimeout`
288
- * calls. For unit tests this is typically not an issue. If sub-millisecond
289
- * resolution is needed, use `Promise.resolve()` directly to wait for a single
290
- * microtask tick instead.
291
- *
292
- * **Caveats**: A single `await nextTick()` covers one reactive propagation
293
- * wave. Chains of computed → effect → atom → effect may require multiple
294
- * awaits — one per propagation step.
295
- */
296
- export declare function nextTick(): Promise<void>;
297
-
298
- /**
299
- * Generates a union of all valid dot-separated paths for type T.
300
- */
301
- export declare type Paths<T, D extends unknown[] = []> = D['length'] extends MaxDepth ? never : T extends object ? {
302
- [K in keyof T & (string | number)]-?: `${K}` | (T[K] extends object ? `${K}.${Paths<T[K], [...D, 1]>}` : never);
303
- }[keyof T & (string | number)] : never;
304
-
305
- /**
306
- * Extracts the value type at path P within type T.
307
- */
308
- export declare type PathValue<T, P extends string> = P extends `${infer K}.${infer Rest}` ? StringKeyToNumber<K> extends keyof T ? PathValue<T[StringKeyToNumber<K> & keyof T], Rest> : never : StringKeyToNumber<P> extends keyof T ? T[StringKeyToNumber<P> & keyof T] : never;
309
-
310
- /**
311
- * Values allowed for DOM properties and attributes.
312
- */
313
152
  export declare type PrimitiveValue = string | number | boolean | null | undefined;
314
153
 
315
154
  /**
316
- * Represents a value that can be either a reactive node (Atom or Computed)
317
- * or a plain static value of the same type.
318
- *
319
- * `ComputedAtom<T>` is a structural sub-type of `ReadonlyAtom<T>`, so it is
320
- * already covered by `ReadonlyAtom<T>`.
155
+ * Represents a value that can be tracked by the reactive system.
156
+ * - T: Static value (one-time bind)
157
+ * - ReadonlyAtom<T>: Reactive value (updates DOM when atom changes)
158
+ * - () => T: Reactive function (updates DOM when any atom read inside changes)
321
159
  */
322
160
  export declare type ReactiveValue<T> = T | ReadonlyAtom<T> | (() => T);
323
161
 
@@ -345,7 +183,6 @@ export declare interface RouteConfig {
345
183
 
346
184
  export declare type RouteDefinition = TemplateRoute | RenderRoute;
347
185
 
348
- /** Shared route lifecycle hooks. */
349
186
  export declare interface RouteLifecycle {
350
187
  onEnter?: (params: Record<string, string>, router: Router) => Record<string, string> | undefined;
351
188
  onLeave?: (router: Router) => boolean | undefined;
@@ -358,33 +195,25 @@ export declare interface Router {
358
195
  destroy: () => void;
359
196
  }
360
197
 
361
- /**
362
- * Helper to convert a numeric string to a number type, otherwise returns the string.
363
- * Used for array indexing in paths.
364
- */
365
- declare type StringKeyToNumber<S extends string> = S extends `${infer N extends number}` ? N : S;
366
-
367
198
  export declare interface TemplateRoute extends RouteLifecycle {
368
199
  template: string;
369
200
  render?: never;
370
201
  onMount?: ($content: JQuery, onUnmount: (cleanupFn: () => void) => void, router: Router) => void;
371
202
  }
372
203
 
373
- export { untracked }
374
-
375
204
  /**
376
- * Configuration options for `atomVal`.
205
+ * Options for `atomVal`, `atomChecked`, and `atomForm` bindings.
377
206
  */
378
207
  export declare interface ValOptions<T> {
379
- /** Delay in milliseconds before syncing DOM input to Atom. */
208
+ /** Debounce duration in milliseconds for DOM -> Atom sync. Defaults to 0. */
380
209
  debounce?: number;
381
- /** DOM event to trigger sync (default: 'input'). */
210
+ /** jQuery event name(s) to listen to. Defaults to "input". */
382
211
  event?: string;
383
- /** Parser to convert string input to Atom type T. */
212
+ /** Custom function to parse DOM string to atom type T. */
384
213
  parse?: (v: string) => T;
385
- /** Formatter to convert Atom type T to string for DOM display. */
214
+ /** Custom function to format atom type T to DOM string. */
386
215
  format?: (v: T) => string;
387
- /** Custom equality check for comparing parsed values. Defaults to Object.is. */
216
+ /** Custom equality check to prevent redundant atom updates. */
388
217
  equal?: EqualFn<T>;
389
218
  }
390
219