@but212/atom-effect-jquery 0.26.0 → 0.27.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
@@ -12,8 +12,9 @@ import { untracked } from '@but212/atom-effect';
12
12
  import { WritableAtom } from '@but212/atom-effect';
13
13
 
14
14
  /**
15
- * Represents a value that can be a synchronous `ReactiveValue<T>`,
16
- * a `Promise<T>`, or an Atom yielding `T | Promise<T>`.
15
+ * An extension of ReactiveValue that also supports Promises and async functions.
16
+ * The binding system automatically handles the promise lifecycle, showing the
17
+ * latest resolved value and ignoring stale ones (race condition protection).
17
18
  */
18
19
  declare type AsyncReactiveValue<T> = T | ReadonlyAtom<T | Promise<T>> | Promise<T> | (() => T | Promise<T>);
19
20
 
@@ -21,50 +22,23 @@ export { atom }
21
22
 
22
23
  /**
23
24
  * 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
25
  */
33
26
  export declare function atomLens<T extends object, P extends Paths<T>>(atom: WritableAtom<T>, path: P): WritableAtom<PathValue<T, P>>;
34
27
 
35
28
  export { batch }
36
29
 
37
- /**
38
- * Configuration options for `atomBind`.
39
- * @template T Type of the value for two-way binding (`val` field).
40
- */
41
30
  export declare interface BindingOptions<T = unknown> {
42
- /** Binds textContent to any reactive source. */
43
31
  text?: AsyncReactiveValue<unknown>;
44
- /** Binds innerHTML to a reactive string source (sanitized). */
45
32
  html?: AsyncReactiveValue<string>;
46
- /** Map of class names to reactive boolean conditions. */
47
33
  class?: Record<string, AsyncReactiveValue<boolean>>;
48
- /** Map of CSS properties to reactive values or [value, unit] tuples. */
49
34
  css?: CssBindings;
50
- /** Binds attributes with consistent primitive constraints. */
51
35
  attr?: Record<string, AsyncReactiveValue<PrimitiveValue>>;
52
- /** Binds DOM properties. */
53
36
  prop?: Record<string, AsyncReactiveValue<unknown>>;
54
- /** Direct visibility control (display: none). */
55
37
  show?: AsyncReactiveValue<boolean>;
56
- /** Inverse visibility control. */
57
38
  hide?: AsyncReactiveValue<boolean>;
58
- /**
59
- * Two-way binding for input values.
60
- * Pass an atom or a `[atom, options]` tuple.
61
- */
62
39
  val?: WritableAtom<T> | [atom: WritableAtom<T>, options: ValOptions<T>];
63
- /** Two-way binding for checkboxes and radio buttons. */
64
40
  checked?: WritableAtom<boolean>;
65
- /** Fully automated two-way form binding using name attributes. */
66
41
  form?: WritableAtom<T extends object ? T : unknown>;
67
- /** Event listeners with automatic batched execution and lifecycle-bound cleanup. */
68
42
  on?: Record<string, (e: JQuery.Event) => void>;
69
43
  }
70
44
 
@@ -85,8 +59,26 @@ declare class BindingRegistry {
85
59
  markIgnored(node: Node): void;
86
60
  isIgnored(node: Node): boolean;
87
61
  private getOrCreateRecord;
62
+ /**
63
+ * Registers a reactive effect with an element's record.
64
+ * Effects are automatically disposed when the element is removed from the DOM.
65
+ *
66
+ * @param el - The DOM element to bind the effect to.
67
+ * @param fx - The reactive effect instance.
68
+ */
88
69
  trackEffect(el: Element, fx: EffectObject): void;
70
+ /**
71
+ * Registers an arbitrary cleanup function with an element's record.
72
+ * Cleanups are executed when the element is removed from the DOM.
73
+ *
74
+ * @param el - The DOM element to bind the cleanup to.
75
+ * @param fn - The cleanup function (e.g., event unbinding, timer clear).
76
+ */
89
77
  trackCleanup(el: Element, fn: () => void): void;
78
+ /**
79
+ * Assigns a component-level cleanup function (e.g., from atomMount).
80
+ * Unlike generic cleanups, there can only be one component cleanup per element.
81
+ */
90
82
  setComponentCleanup(el: Element, fn: (() => void) | undefined): void;
91
83
  hasBind(el: Element): boolean;
92
84
  cleanup(el: Element | Node): void;
@@ -94,34 +86,19 @@ declare class BindingRegistry {
94
86
  cleanupTree(el: Element | Node): void;
95
87
  }
96
88
 
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
89
  export declare type ComponentFn<P = Record<string, unknown>> = ($el: JQuery, props: P) => EffectResult;
103
90
 
104
91
  /**
105
92
  * 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
93
  */
111
- export declare function composeLens<T extends object, P extends Paths<T>>(lens: WritableAtom<T>, path: P): WritableAtom<PathValue<T, P>>;
94
+ export declare const composeLens: <T extends object, P extends Paths<T>>(lens: WritableAtom<T>, path: P) => WritableAtom<PathValue<T, P>>;
112
95
 
113
96
  export { computed }
114
97
 
115
98
  export { ComputedAtom }
116
99
 
117
- /**
118
- * CSS bindings map property names to CSS values.
119
- */
120
100
  export declare type CssBindings = Record<string, CssValue>;
121
101
 
122
- /**
123
- * CSS value: either a direct reactive value or a numeric tuple of [source, unit].
124
- */
125
102
  export declare type CssValue = AsyncReactiveValue<string | number> | [source: AsyncReactiveValue<number>, unit: string];
126
103
 
127
104
  export default default_2;
@@ -139,15 +116,8 @@ export declare function disablejQueryOverrides(): void;
139
116
 
140
117
  export { effect }
141
118
 
142
- /**
143
- * Cleanup function returned by effects or components.
144
- */
145
119
  export declare type EffectCleanup = () => void;
146
120
 
147
- /**
148
- * Result of a reactive factory or component mount.
149
- * Returns `void` (no cleanup) or an `EffectCleanup` function.
150
- */
151
121
  export declare type EffectResult = undefined | EffectCleanup;
152
122
 
153
123
  /**
@@ -162,41 +132,17 @@ export declare type EffectResult = undefined | EffectCleanup;
162
132
  */
163
133
  export declare function enableAutoCleanup(root: Element): void;
164
134
 
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
135
  export declare function enablejQueryOverrides(): void;
176
136
 
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
137
  export declare type EqualFn<T> = (a: T, b: T) => boolean;
182
138
 
183
- /**
184
- * Configuration options for `atomFetch`.
185
- */
186
139
  export declare interface FetchOptions<T> {
187
- /** Initial value before the first fetch resolves. */
188
140
  defaultValue: T;
189
- /** HTTP method (default: 'GET'). */
190
141
  method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS' | (string & {});
191
- /** HTTP headers. */
192
142
  headers?: Record<string, string>;
193
- /** Transforms the raw response into T. */
194
143
  transform?: (raw: unknown) => T;
195
- /** Additional `$.ajax` settings. Can be a getter function for reactive data tracking. */
196
144
  ajaxOptions?: JQuery.AjaxSettings | (() => JQuery.AjaxSettings);
197
- /** Error callback. */
198
145
  onError?: (err: unknown) => void;
199
- /** Whether to fetch immediately (default: true). */
200
146
  eager?: boolean;
201
147
  }
202
148
 
@@ -204,120 +150,71 @@ export { isAtom }
204
150
 
205
151
  export { isComputed }
206
152
 
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>;
153
+ /** Checks if a given value is a reactive node (Atom or Computed). */
154
+ export declare const isReactive: (v: unknown) => v is ReadonlyAtom<unknown>;
215
155
 
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
156
  declare type KeysOfType<T, V> = {
221
157
  [K in keyof T]: T[K] extends V ? K : never;
222
158
  }[keyof T];
223
159
 
224
160
  /**
225
161
  * 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
162
  */
232
- export declare function lensFor<T extends object>(atom: WritableAtom<T>): <P extends Paths<T>>(path: P) => WritableAtom<PathValue<T, P>>;
163
+ export declare const lensFor: <T extends object>(atom: WritableAtom<T>) => <P extends Paths<T>>(path: P) => WritableAtom<PathValue<T, P>>;
233
164
 
234
- /** Key type for Map/Set inside list.ts */
235
165
  declare type ListKey = string | number;
236
166
 
237
- /** Key extractor function signature. */
238
167
  declare type ListKeyFn<T> = (item: T, index: number) => ListKey;
239
168
 
240
- /**
241
- * Configuration options for `atomList`.
242
- */
243
169
  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
170
  key: KeysOfType<T, ListKey> | ListKeyFn<T>;
249
- /** Render function for each item. */
250
171
  render: (item: T, index: number) => ListRenderResult;
251
- /** Optional post-render binding logic. */
252
172
  bind?: ($el: JQuery, item: T, index: number) => void;
253
- /** Optional update logic when item data changes but DOM is reused. */
254
173
  update?: ($el: JQuery, item: T, index: number) => void;
255
- /** Lifecycle hook: called when an element is added to the list. */
256
174
  onAdd?: ($el: JQuery) => void;
257
- /** Lifecycle hook: called when an element is about to be removed. */
258
175
  onRemove?: ($el: JQuery) => Promise<void> | void;
259
- /** Content to show when the list is empty. */
260
176
  empty?: ListRenderResult;
261
- /** Delegated event handlers attached to the container. */
262
177
  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
178
  isEqual?: (a: T, b: T) => boolean;
268
179
  }
269
180
 
270
- /** Possible return types for render() / empty */
271
181
  declare type ListRenderResult = string | Element | DocumentFragment | JQuery;
272
182
 
273
- /**
274
- * Maximum recursion depth for path generation.
275
- * Prevents TypeScript compiler from hitting recursion limits on deeply nested types.
276
- */
183
+ /** Max recursion depth for dot-paths. */
277
184
  declare type MaxDepth = 8;
278
185
 
186
+ /** Resolves after microtask effects flush. Fast Promise-based scheduling. */
187
+ export declare const nextTick: () => Promise<void>;
188
+
279
189
  /**
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.
190
+ * Generates a union of all possible dot-separated paths for a given type T.
286
191
  *
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.
192
+ * Used for `atomLens` to provide IDE autocomplete and type safety when
193
+ * zooming into deeply nested reactive objects.
291
194
  *
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.
195
+ * @example
196
+ * type User = { profile: { name: string } };
197
+ * type P = Paths<User>; // "profile" | "profile.name"
300
198
  */
301
199
  export declare type Paths<T, D extends unknown[] = []> = D['length'] extends MaxDepth ? never : T extends object ? {
302
200
  [K in keyof T & (string | number)]-?: `${K}` | (T[K] extends object ? `${K}.${Paths<T[K], [...D, 1]>}` : never);
303
201
  }[keyof T & (string | number)] : never;
304
202
 
305
203
  /**
306
- * Extracts the value type at path P within type T.
204
+ * Resolves the type of a value at a specific dot-path P within type T.
205
+ *
206
+ * Works in tandem with `Paths<T>` to ensure that lensed atoms have
207
+ * the correct inferred type for the member they point to.
307
208
  */
308
209
  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
210
 
310
- /**
311
- * Values allowed for DOM properties and attributes.
312
- */
313
211
  export declare type PrimitiveValue = string | number | boolean | null | undefined;
314
212
 
315
213
  /**
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>`.
214
+ * Represents a value that can be tracked by the reactive system.
215
+ * - T: Static value (one-time bind)
216
+ * - ReadonlyAtom<T>: Reactive value (updates DOM when atom changes)
217
+ * - () => T: Reactive function (updates DOM when any atom read inside changes)
321
218
  */
322
219
  export declare type ReactiveValue<T> = T | ReadonlyAtom<T> | (() => T);
323
220
 
@@ -345,7 +242,6 @@ export declare interface RouteConfig {
345
242
 
346
243
  export declare type RouteDefinition = TemplateRoute | RenderRoute;
347
244
 
348
- /** Shared route lifecycle hooks. */
349
245
  export declare interface RouteLifecycle {
350
246
  onEnter?: (params: Record<string, string>, router: Router) => Record<string, string> | undefined;
351
247
  onLeave?: (router: Router) => boolean | undefined;
@@ -358,10 +254,7 @@ export declare interface Router {
358
254
  destroy: () => void;
359
255
  }
360
256
 
361
- /**
362
- * Helper to convert a numeric string to a number type, otherwise returns the string.
363
- * Used for array indexing in paths.
364
- */
257
+ /** Helper to convert numeric string to number for array indexing. */
365
258
  declare type StringKeyToNumber<S extends string> = S extends `${infer N extends number}` ? N : S;
366
259
 
367
260
  export declare interface TemplateRoute extends RouteLifecycle {
@@ -373,18 +266,18 @@ export declare interface TemplateRoute extends RouteLifecycle {
373
266
  export { untracked }
374
267
 
375
268
  /**
376
- * Configuration options for `atomVal`.
269
+ * Options for `atomVal`, `atomChecked`, and `atomForm` bindings.
377
270
  */
378
271
  export declare interface ValOptions<T> {
379
- /** Delay in milliseconds before syncing DOM input to Atom. */
272
+ /** Debounce duration in milliseconds for DOM -> Atom sync. Defaults to 0. */
380
273
  debounce?: number;
381
- /** DOM event to trigger sync (default: 'input'). */
274
+ /** jQuery event name(s) to listen to. Defaults to "input". */
382
275
  event?: string;
383
- /** Parser to convert string input to Atom type T. */
276
+ /** Custom function to parse DOM string to atom type T. */
384
277
  parse?: (v: string) => T;
385
- /** Formatter to convert Atom type T to string for DOM display. */
278
+ /** Custom function to format atom type T to DOM string. */
386
279
  format?: (v: T) => string;
387
- /** Custom equality check for comparing parsed values. Defaults to Object.is. */
280
+ /** Custom equality check to prevent redundant atom updates. */
388
281
  equal?: EqualFn<T>;
389
282
  }
390
283