aberdeen 1.0.12 → 1.1.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/README.md +39 -5
- package/dist/aberdeen.d.ts +58 -100
- package/dist/aberdeen.js +244 -186
- package/dist/aberdeen.js.map +3 -3
- package/dist/dispatcher.d.ts +54 -0
- package/dist/dispatcher.js +65 -0
- package/dist/dispatcher.js.map +10 -0
- package/dist/route.d.ts +79 -30
- package/dist/route.js +162 -135
- package/dist/route.js.map +3 -3
- package/dist-min/aberdeen.js +5 -5
- package/dist-min/aberdeen.js.map +3 -3
- package/dist-min/dispatcher.js +4 -0
- package/dist-min/dispatcher.js.map +10 -0
- package/dist-min/route.js +2 -2
- package/dist-min/route.js.map +3 -3
- package/package.json +6 -1
- package/src/aberdeen.ts +348 -352
- package/src/dispatcher.ts +130 -0
- package/src/route.ts +272 -181
- package/html-to-aberdeen +0 -354
package/README.md
CHANGED
|
@@ -11,8 +11,8 @@ Aberdeen's approach is refreshingly simple:
|
|
|
11
11
|
- 🎩 **Simple:** Express UIs naturally in JavaScript/TypeScript, without build steps or JSX, and with a minimal amount of concepts you need to learn.
|
|
12
12
|
- ⏩ **Fast:** No virtual DOM. Aberdeen intelligently updates only the minimal, necessary parts of your UI when proxied data changes.
|
|
13
13
|
- 👥 **Awesome lists**: It's very easy and performant to reactively display data sorted by whatever you like.
|
|
14
|
-
- 🔬 **Tiny:** Around 6KB (minimized and gzipped)
|
|
15
|
-
- 🔋 **Batteries included**: Comes with
|
|
14
|
+
- 🔬 **Tiny:** Around 6KB (minimized and gzipped) for the core system. Zero runtime dependencies.
|
|
15
|
+
- 🔋 **Batteries included**: Comes with browser history management, routing, revertible patches for optimistic user-interface updates, component-local CSS, SVG support, helper functions for transforming reactive data (mapping, partitioning, filtering, etc) and hide/unhide transition effects. No bikeshedding required!
|
|
16
16
|
|
|
17
17
|
## Why *not* use Aberdeen?
|
|
18
18
|
|
|
@@ -57,7 +57,7 @@ Okay, next up is a somewhat more complex app - a todo-list with the following be
|
|
|
57
57
|
Pfew.. now let's look at the code:
|
|
58
58
|
|
|
59
59
|
```typescript
|
|
60
|
-
import {$, proxy, onEach, insertCss, peek,
|
|
60
|
+
import {$, proxy, onEach, insertCss, peek, unproxy, ref} from "aberdeen";
|
|
61
61
|
import {grow, shrink} from "aberdeen/transitions";
|
|
62
62
|
|
|
63
63
|
// We'll use a simple class to store our data.
|
|
@@ -172,6 +172,40 @@ Some further examples:
|
|
|
172
172
|
|
|
173
173
|
And you may want to study the examples above, of course!
|
|
174
174
|
|
|
175
|
-
##
|
|
175
|
+
## Changelog
|
|
176
176
|
|
|
177
|
-
|
|
177
|
+
### 1.1.0 (2024-09-12)
|
|
178
|
+
|
|
179
|
+
This major release aims to reduce surprises in our API, aligning more closely with regular JavaScript semantics (for better or worse).
|
|
180
|
+
|
|
181
|
+
**Breaking changes:**
|
|
182
|
+
|
|
183
|
+
- Functions that iterate objects (like `onEach` and `map`) will now only work on *own* properties of the object, ignoring those in the prototype chain. The new behavior should be more consistent and faster.
|
|
184
|
+
- These iteration function now properly distinguish between `undefined` and *empty*. Previously, object/array/map items with `undefined` values were considered non-existent. The new behavior (though arguably confusing) is more consistent with regular JavaScript semantics.
|
|
185
|
+
- The `copy` function no longer ..
|
|
186
|
+
- Supports `SHALLOW` and `MERGE` flags. The latter has been replaced by a dedicated `merge` function. The former turned out not to be particularly useful.
|
|
187
|
+
- Has weird special cases that would allow copying objects into maps and merging objects into arrays.
|
|
188
|
+
- Copies properties from the prototype chain of objects. Only *own* properties are copied now. As the prototype link itself *is* copied over, this should actually result in copies being *more* similar to the original.
|
|
189
|
+
- The `observe` function has been renamed to `derive` to better reflect its purpose and match terminology used in other reactive programming libraries.
|
|
190
|
+
- The `$({element: myElement})` syntax for inserting existing DOM elements has been removed. Use `$(myElement)` instead.
|
|
191
|
+
- The `route` API brings some significant changes. Modifying the `route` observable (which should now be accessed as `route.current`) will now always result in changing the current browser history item (URL and state, using `replaceState`), instead of using a heuristic to figure out what you probably want. Dedicated functions have been added for navigating to a new URL (`go`), back to a previous URL (`back`), and for going up in the route hierarchy (`up`).
|
|
192
|
+
- The concept of immediate observers (through the `immediateObserve` function) no longer exists. It caused unexpected behavior (for instance due to the fact that an array `pop()` in JavaScript is implemented as a delete followed by a length change, so happens in two steps that would each call immediate observers). The reason it existed was mostly to enable a pre-1.0 version of the `route` API. It turned out to be a mistake.
|
|
193
|
+
|
|
194
|
+
**Enhancements:**
|
|
195
|
+
|
|
196
|
+
- The `peek` function can no also accept an object and a key as argument (e.g. `peek(obj, 'myKey')`). It does the same as `peek(() => obj.myKey)`, but more concise and faster.
|
|
197
|
+
- The `copy` and `merge` functions now ..
|
|
198
|
+
- Accept an optional `dstKey` argument, allowing you to assign to a specific key with `copy` semantics, and without subscribing to the key.
|
|
199
|
+
- Return a boolean indicating whether any changes were made.
|
|
200
|
+
- Are faster.
|
|
201
|
+
- A new `dispatcher` module has been added. It provides a simple and type-safe way to match URL paths to handler functions, and extract parameters from the path. You can still use your own routing solution if you prefer, of course.
|
|
202
|
+
- The `route` module now also has tests, making the whole project now fully covered by tests.
|
|
203
|
+
|
|
204
|
+
**Fixes:**
|
|
205
|
+
|
|
206
|
+
- Browser-back behavior in the `route` module had some reliability issues after page reloads.
|
|
207
|
+
- The `copy` and `clone` function created Maps and Arrays with the wrong internal type. So `instanceof Array` would say yes, while `Array.isArray` would say no. JavaScript is weird.
|
|
208
|
+
|
|
209
|
+
### 1.0.0 (2025-05-07)
|
|
210
|
+
|
|
211
|
+
After five years of working on this library on and off, I'm finally happy with its API and the developer experience it offers. I'm calling it 1.0! To celebrate, I've created some pretty fancy (if I may say so myself) interactive documentation and a tutorial.
|
package/dist/aberdeen.d.ts
CHANGED
|
@@ -70,7 +70,7 @@ export declare function onEach<K extends string | number | symbol, T>(target: Re
|
|
|
70
70
|
* is deleted from an object), the scope that called `isEmpty` will be automatically
|
|
71
71
|
* scheduled for re-evaluation.
|
|
72
72
|
*
|
|
73
|
-
* @param proxied The observable array or object
|
|
73
|
+
* @param proxied The observable array or object to check.
|
|
74
74
|
* @returns `true` if the array has length 0 or the object has no own enumerable properties, `false` otherwise.
|
|
75
75
|
*
|
|
76
76
|
* @example
|
|
@@ -113,8 +113,8 @@ export interface ValueRef<T> {
|
|
|
113
113
|
* $('div', {text: cnt});
|
|
114
114
|
* // <div>2</div>
|
|
115
115
|
|
|
116
|
-
* // Or we can use it in an {@link
|
|
117
|
-
*
|
|
116
|
+
* // Or we can use it in an {@link derive} function:
|
|
117
|
+
* $(() => console.log("The count is now", cnt.value));
|
|
118
118
|
* // The count is now 2
|
|
119
119
|
*
|
|
120
120
|
* // Adding/removing items will update the count
|
|
@@ -178,12 +178,9 @@ export declare function unproxy<T>(target: T): T;
|
|
|
178
178
|
*
|
|
179
179
|
* @param dst - The destination object/array/Map (proxied or unproxied).
|
|
180
180
|
* @param src - The source object/array/Map (proxied or unproxied). It won't be modified.
|
|
181
|
-
* @
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
* - {@link SHALLOW}: Performs a shallow copy; when an array/object of the right type doesn't exist in `dst` yet, a reference to the array/object in `src` will be made, instead of creating a copy. If the array/object already exists, it won't be replaced (by a reference), but all items will be individually checked and copied like normal, keeping changes (and therefore UI updates) to a minimum.
|
|
185
|
-
* @template T - The type of the destination object.
|
|
186
|
-
* @throws Error if attempting to copy an array into a non-array or vice versa (unless {@link MERGE} is set, allowing for sparse array updates).
|
|
181
|
+
* @template T - The type of the objects being copied.
|
|
182
|
+
* @returns `true` if any changes were made to `dst`, or `false` if not.
|
|
183
|
+
* @throws Error if attempting to copy an array into a non-array or vice versa.
|
|
187
184
|
*
|
|
188
185
|
* @example Basic Copy
|
|
189
186
|
* ```typescript
|
|
@@ -191,67 +188,53 @@ export declare function unproxy<T>(target: T): T;
|
|
|
191
188
|
* const dest = proxy({ b: { d: 3 } });
|
|
192
189
|
* copy(dest, source);
|
|
193
190
|
* console.log(dest); // proxy({ a: 1, b: { c: 2 } })
|
|
191
|
+
* copy(dest, 'b', { e: 4 });
|
|
192
|
+
* console.log(dest); // proxy({ a: 1, b: { e: 4 } })
|
|
194
193
|
* ```
|
|
194
|
+
*/
|
|
195
|
+
export declare function copy<T extends object>(dst: T, src: T): boolean;
|
|
196
|
+
/**
|
|
197
|
+
* Like above, but copies `src` into `dst[dstKey]`. This is useful if you're unsure if dst[dstKey]
|
|
198
|
+
* already exists (as the right type of object) or if you don't want to subscribe to dst[dstKey].
|
|
195
199
|
*
|
|
196
|
-
* @
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
* copy
|
|
201
|
-
*
|
|
202
|
-
* ```
|
|
200
|
+
* @param dstKey - Optional key in `dst` to copy into.
|
|
201
|
+
*/
|
|
202
|
+
export declare function copy<T extends object>(dst: T, dstKey: keyof T, src: T[typeof dstKey]): boolean;
|
|
203
|
+
/**
|
|
204
|
+
* Like {@link copy}, but uses merge semantics. Properties in `dst` not present in `src` are kept.
|
|
205
|
+
* `null`/`undefined` in `src` delete properties in `dst`.
|
|
203
206
|
*
|
|
204
|
-
*
|
|
205
|
-
* ```typescript
|
|
206
|
-
* const source = { x: 3, y: 4 };
|
|
207
|
-
* const dest = proxy(new Map());
|
|
208
|
-
* copy(dest, source);
|
|
209
|
-
* console.log(dest); // proxy(Map([['x', 3], ['y', 4]]))
|
|
210
|
-
* ```
|
|
207
|
+
* When the destination is an object and the source is an array, its keys are used as (sparse) array indices.
|
|
211
208
|
*
|
|
212
|
-
* @example
|
|
209
|
+
* @example Basic merge
|
|
213
210
|
* ```typescript
|
|
214
211
|
* const source = { b: { c: 99 }, d: undefined }; // d: undefined will delete
|
|
215
212
|
* const dest = proxy({ a: 1, b: { x: 5 }, d: 4 });
|
|
216
|
-
*
|
|
217
|
-
*
|
|
213
|
+
* merge(dest, source);
|
|
214
|
+
* merge(dest, 'b', { y: 6 }); // merge into dest.b
|
|
215
|
+
* merge(dest, 'c', { z: 7 }); // merge.c doesn't exist yet, so it will just be assigned
|
|
216
|
+
* console.log(dest); // proxy({ a: 1, b: { c: 99, x: 5, y: 6 }, c: { z: 7 } })
|
|
218
217
|
* ```
|
|
219
218
|
*
|
|
220
|
-
* @example Partial Array
|
|
219
|
+
* @example Partial Array Merge
|
|
221
220
|
* ```typescript
|
|
222
221
|
* const messages = proxy(['msg1', 'msg2', 'msg3']);
|
|
223
222
|
* const update = { 1: 'updated msg2' }; // Update using object key as index
|
|
224
|
-
*
|
|
223
|
+
* merge(messages, update);
|
|
225
224
|
* console.log(messages); // proxy(['msg1', 'updated msg2', 'msg3'])
|
|
226
225
|
* ```
|
|
227
226
|
*
|
|
228
|
-
* @example SHALLOW
|
|
229
|
-
* ```typescript
|
|
230
|
-
* const source = { nested: [1, 2] };
|
|
231
|
-
* const dest = {};
|
|
232
|
-
* copy(dest, source, SHALLOW);
|
|
233
|
-
* dest.nested.push(3);
|
|
234
|
-
* console.log(source.nested); // [1, 2, 3] (source was modified)
|
|
235
|
-
* ```
|
|
236
227
|
*/
|
|
237
|
-
export declare function
|
|
238
|
-
export declare function
|
|
239
|
-
export declare function copy<T extends Record<string | number | symbol, any>>(dst: T, src: Map<keyof T, T[keyof T]>, flags?: number): void;
|
|
240
|
-
export declare function copy<T extends object>(dst: T, src: Partial<T>, flags?: number): void;
|
|
241
|
-
/** Flag to {@link copy} causing it to use merge semantics. See {@link copy} for details. */
|
|
242
|
-
export declare const MERGE = 1;
|
|
243
|
-
/** Flag to {@link copy} and {@link clone} causing them to create a shallow copy (instead of the deep copy done by default).*/
|
|
244
|
-
export declare const SHALLOW = 2;
|
|
228
|
+
export declare function merge<T extends object>(dst: T, value: Partial<T>): boolean;
|
|
229
|
+
export declare function merge<T extends object>(dst: T, dstKey: keyof T, value: Partial<T[typeof dstKey]>): boolean;
|
|
245
230
|
/**
|
|
246
231
|
* Clone an (optionally proxied) object or array.
|
|
247
232
|
*
|
|
248
233
|
* @param src The object or array to clone. If it is proxied, `clone` will subscribe to any changes to the (nested) data structure.
|
|
249
|
-
* @param flags
|
|
250
|
-
* - {@link SHALLOW}: Performs a shallow clone, meaning that only the top-level array or object will be copied, while object/array values will just be references to the original data in `src`.
|
|
251
234
|
* @template T - The type of the objects being copied.
|
|
252
|
-
* @returns A new unproxied array or object (of the same type as `src`), containing a deep
|
|
235
|
+
* @returns A new unproxied array or object (of the same type as `src`), containing a deep copy of `src`.
|
|
253
236
|
*/
|
|
254
|
-
export declare function clone<T extends object>(src: T
|
|
237
|
+
export declare function clone<T extends object>(src: T): T;
|
|
255
238
|
/**
|
|
256
239
|
* Creates a reactive reference (`{ value: T }`-like object) to a specific value
|
|
257
240
|
* within a proxied object or array.
|
|
@@ -301,7 +284,7 @@ export declare function ref<T extends TargetType, K extends keyof T>(target: T,
|
|
|
301
284
|
* - An optional HTML **tag**, something like `h1`. If present, a DOM element of that tag is created, and that element will be the *current* element for the rest of this `$` function execution.
|
|
302
285
|
* - Any number of CSS classes prefixed by `.` characters. These classes will be added to the *current* element.
|
|
303
286
|
* - Optional content **text** prefixed by a `:` character, ranging til the end of the string. This will be added as a TextNode to the *current* element.
|
|
304
|
-
* - `function`: When a function (without argument nor a return value) is passed in, it will be reactively executed in its own
|
|
287
|
+
* - `function`: When a function (without argument nor a return value) is passed in, it will be reactively executed in its own observer scope, preserving the *current element*. So any `$()` invocations within this function will create DOM elements with our *current* element as parent. If the function reads observable data, and that data is changed later on, the function we re-execute (after side effects, such as DOM modifications through `$`, have been cleaned - see also {@link clean}).
|
|
305
288
|
* - `object`: When an object is passed in, its key-value pairs are used to modify the *current* element in the following ways...
|
|
306
289
|
* - `{<attrName>: any}`: The common case is setting the value as an HTML attribute named key. So `{placeholder: "Your name"}` would add `placeholder="Your name"` to the current HTML element.
|
|
307
290
|
* - `{<propName>: boolean}` or `{value: any}` or `{selectedIndex: number}`: If the value is a boolean, or if the key is `value` or `selectedIndex`, it is set on the `current` element as a DOM property instead of an HTML attribute. For example `{checked: true}` would do `el.checked = true` for the *current* element.
|
|
@@ -312,7 +295,7 @@ export declare function ref<T extends TargetType, K extends keyof T>(target: T,
|
|
|
312
295
|
* - `{destroy: string}`: When the *current* element is a top-level element to be removed (due to reactivity cleanup), actual removal from the DOM is delayed by 2 seconds, and in the mean time the value string is added as a CSS class to the element, allowing for a deletion transition. The string may also contain multiple dot-separated CSS classes, such as `.fade.shrink`.
|
|
313
296
|
* - `{create: function}` and `{destroy: function}`: The function is invoked when the *current* element is the top-level element being created/destroyed. It can be used for more involved creation/deletion animations. In case of `destroy`, the function is responsible for actually removing the element from the DOM (eventually). See `transitions.ts` in the Aberdeen source code for some examples.
|
|
314
297
|
* - `{bind: <obsValue>}`: Create a two-way binding element between the `value` property of the given observable (proxy) variable, and the *current* input element (`<input>`, `<select>` or `<textarea>`). This is often used together with {@link ref}, in order to use properties other than `.value`.
|
|
315
|
-
* - `{<any>: <obsvalue>}`: Create a new
|
|
298
|
+
* - `{<any>: <obsvalue>}`: Create a new observer scope and read the `value` property of the given observable (proxy) variable from within it, and apply the contained value using any of the other rules in this list. Example:
|
|
316
299
|
* ```typescript
|
|
317
300
|
* const myColor = proxy('red');
|
|
318
301
|
* $('p:Test', {$color: myColor, click: () => myColor.value = 'yellow'})
|
|
@@ -435,7 +418,7 @@ export declare function insertCss(style: object, global?: boolean): string;
|
|
|
435
418
|
/**
|
|
436
419
|
* Sets a custom error handler function for errors that occur asynchronously
|
|
437
420
|
* within reactive scopes (e.g., during updates triggered by proxy changes in
|
|
438
|
-
* {@link
|
|
421
|
+
* {@link derive} or {@link $} render functions).
|
|
439
422
|
*
|
|
440
423
|
* The default handler logs the error to `console.error` and adds a simple
|
|
441
424
|
* 'Error' message div to the DOM at the location where the error occurred (if possible).
|
|
@@ -515,7 +498,7 @@ export declare function getParentElement(): Element;
|
|
|
515
498
|
* This is useful for releasing resources, removing manual event listeners, or cleaning up
|
|
516
499
|
* side effects associated with the scope. Cleaners are run in reverse order of registration.
|
|
517
500
|
*
|
|
518
|
-
* Scopes are created by functions like {@link
|
|
501
|
+
* Scopes are created by functions like {@link derive}, {@link mount}, {@link $} (when given a render function),
|
|
519
502
|
* and internally by constructs like {@link onEach}.
|
|
520
503
|
*
|
|
521
504
|
* @param cleaner - The function to execute during cleanup.
|
|
@@ -533,7 +516,7 @@ export declare function getParentElement(): Element;
|
|
|
533
516
|
* peek(() => sum.value += item);
|
|
534
517
|
* // Clean gets called before each rerun for a certain item index
|
|
535
518
|
* // No need for peek here, as the clean code doesn't run in an
|
|
536
|
-
* //
|
|
519
|
+
* // observer scope.
|
|
537
520
|
* clean(() => sum.value -= item);
|
|
538
521
|
* })
|
|
539
522
|
*
|
|
@@ -557,7 +540,7 @@ export declare function clean(cleaner: () => void): void;
|
|
|
557
540
|
*
|
|
558
541
|
* @param func - The function to execute reactively. Any DOM manipulations should typically
|
|
559
542
|
* be done using {@link $} within this function. Its return value will be made available as an
|
|
560
|
-
* observable returned by the `
|
|
543
|
+
* observable returned by the `derive()` function.
|
|
561
544
|
* @returns An observable object, with its `value` property containing whatever the last run of `func` returned.
|
|
562
545
|
*
|
|
563
546
|
* @example Observation creating a UI components
|
|
@@ -568,7 +551,7 @@ export declare function clean(cleaner: () => void): void;
|
|
|
568
551
|
* console.log('Welcome');
|
|
569
552
|
* $('h3:Welcome, ' + data.user); // Reactive text
|
|
570
553
|
*
|
|
571
|
-
*
|
|
554
|
+
* derive(() => {
|
|
572
555
|
* // When data.notifications changes, only this inner scope reruns,
|
|
573
556
|
* // leaving the `<p>Welcome, ..</p>` untouched.
|
|
574
557
|
* console.log('Notifications');
|
|
@@ -578,13 +561,13 @@ export declare function clean(cleaner: () => void): void;
|
|
|
578
561
|
* });
|
|
579
562
|
* ```
|
|
580
563
|
*
|
|
581
|
-
* ***Note*** that the above could just as easily be done using `$(func)` instead of `
|
|
564
|
+
* ***Note*** that the above could just as easily be done using `$(func)` instead of `derive(func)`.
|
|
582
565
|
*
|
|
583
566
|
* @example Observation with return value
|
|
584
567
|
* ```typescript
|
|
585
568
|
* const counter = proxy(0);
|
|
586
569
|
* setInterval(() => counter.value++, 1000);
|
|
587
|
-
* const double =
|
|
570
|
+
* const double = derive(() => counter.value * 2);
|
|
588
571
|
*
|
|
589
572
|
* $('h3', () => {
|
|
590
573
|
* $(`:counter=${counter.value} double=${double.value}`);
|
|
@@ -594,36 +577,7 @@ export declare function clean(cleaner: () => void): void;
|
|
|
594
577
|
* @overload
|
|
595
578
|
* @param func Func without a return value.
|
|
596
579
|
*/
|
|
597
|
-
export declare function
|
|
598
|
-
/**
|
|
599
|
-
* Similar to {@link observe}, creates a reactive scope that re-executes the function
|
|
600
|
-
* when its proxied dependencies change.
|
|
601
|
-
*
|
|
602
|
-
* **Difference:** Updates run **synchronously and immediately** after the proxy modification
|
|
603
|
-
* that triggered the update occurs.
|
|
604
|
-
*
|
|
605
|
-
* **Caution:** Use sparingly. Immediate execution bypasses Aberdeen's usual batching and
|
|
606
|
-
* ordering optimizations, which can lead to performance issues or observing inconsistent
|
|
607
|
-
* intermediate states if multiple related updates are applied sequentially.
|
|
608
|
-
* Prefer {@link observe} or {@link $} for most use cases.
|
|
609
|
-
*
|
|
610
|
-
* @param func - The function to execute reactively and synchronously.
|
|
611
|
-
*
|
|
612
|
-
* @example
|
|
613
|
-
* ```javascript
|
|
614
|
-
* const state = proxy({ single: 'A' });
|
|
615
|
-
*
|
|
616
|
-
* immediateObserve(() => {
|
|
617
|
-
* state.double = state.single + state.single
|
|
618
|
-
* });
|
|
619
|
-
* console.log(state.double); // 'AA'
|
|
620
|
-
*
|
|
621
|
-
* state.single = 'B';
|
|
622
|
-
* // Synchronously:
|
|
623
|
-
* console.log(state.double); // 'BB'
|
|
624
|
-
* ```
|
|
625
|
-
*/
|
|
626
|
-
export declare function immediateObserve(func: () => void): void;
|
|
580
|
+
export declare function derive<T>(func: () => T): ValueRef<T>;
|
|
627
581
|
/**
|
|
628
582
|
* Attaches a reactive Aberdeen UI fragment to an existing DOM element. Without the use of
|
|
629
583
|
* this function, {@link $} will assume `document.body` as its root.
|
|
@@ -633,11 +587,11 @@ export declare function immediateObserve(func: () => void): void;
|
|
|
633
587
|
* will cause it to re-execute when the data changes, updating the DOM elements created within it.
|
|
634
588
|
*
|
|
635
589
|
* Calls to {@link $} inside `func` will append nodes to `parentElement`.
|
|
636
|
-
* You can nest {@link
|
|
590
|
+
* You can nest {@link derive} or other {@link $} scopes within `func`.
|
|
637
591
|
* Use {@link unmountAll} to clean up all mounted scopes and their DOM nodes.
|
|
638
592
|
*
|
|
639
593
|
* Mounting scopes happens reactively, meaning that if this function is called from within another
|
|
640
|
-
* ({@link
|
|
594
|
+
* ({@link derive} or {@link $} or {@link mount}) scope that gets cleaned up, so will the mount.
|
|
641
595
|
*
|
|
642
596
|
* @param parentElement - The native DOM `Element` to which the UI fragment will be appended.
|
|
643
597
|
* @param func - The function that defines the UI fragment, typically containing calls to {@link $}.
|
|
@@ -669,26 +623,27 @@ export declare function immediateObserve(func: () => void): void;
|
|
|
669
623
|
export declare function mount(parentElement: Element, func: () => void): void;
|
|
670
624
|
/**
|
|
671
625
|
* Removes all Aberdeen-managed DOM nodes and stops all active reactive scopes
|
|
672
|
-
* (created by {@link mount}, {@link
|
|
626
|
+
* (created by {@link mount}, {@link derive}, {@link $} with functions, etc.).
|
|
673
627
|
*
|
|
674
628
|
* This effectively cleans up the entire Aberdeen application state.
|
|
675
629
|
*/
|
|
676
630
|
export declare function unmountAll(): void;
|
|
677
631
|
/**
|
|
678
|
-
* Executes a function *without* creating subscriptions in the current reactive scope, and returns its result.
|
|
632
|
+
* Executes a function or retrieves a value *without* creating subscriptions in the current reactive scope, and returns its result.
|
|
679
633
|
*
|
|
680
|
-
* This is useful when you need to access reactive data inside a reactive scope (like {@link
|
|
634
|
+
* This is useful when you need to access reactive data inside a reactive scope (like {@link $})
|
|
681
635
|
* but do not want changes to that specific data to trigger a re-execute of the scope.
|
|
682
636
|
*
|
|
683
|
-
* @
|
|
637
|
+
* Note: You may also use {@link unproxy} to get to the raw underlying data structure, which can be used to similar effect.
|
|
684
638
|
*
|
|
685
|
-
* @param
|
|
686
|
-
* @
|
|
639
|
+
* @param target - Either a function to execute, or an object (which may also be an Array or a Map) to index.
|
|
640
|
+
* @param key - Optional key/index to use when `target` is an object.
|
|
641
|
+
* @returns The result of the function call, or the value at `target[key]` when `target` is an object or `target.get(key)` when it's a Map.
|
|
687
642
|
*
|
|
688
|
-
* @example Peeking within
|
|
643
|
+
* @example Peeking within observer
|
|
689
644
|
* ```typescript
|
|
690
645
|
* const data = proxy({ a: 1, b: 2 });
|
|
691
|
-
*
|
|
646
|
+
* $(() => {
|
|
692
647
|
* // re-executes only when data.a changes, because data.b is peeked.
|
|
693
648
|
* const b = peek(() => data.b);
|
|
694
649
|
* console.log(`A is ${data.a}, B was ${b} when A changed.`);
|
|
@@ -698,13 +653,16 @@ export declare function unmountAll(): void;
|
|
|
698
653
|
* ```
|
|
699
654
|
*
|
|
700
655
|
*/
|
|
701
|
-
export declare function peek<T>(
|
|
656
|
+
export declare function peek<T extends object>(target: T, key: keyof T): T[typeof key];
|
|
657
|
+
export declare function peek<K, V>(target: Map<K, V>, key: K): V | undefined;
|
|
658
|
+
export declare function peek<T>(target: T[], key: number): T | undefined;
|
|
659
|
+
export declare function peek<T>(target: () => T): T;
|
|
702
660
|
/** When using a Map as `source`. */
|
|
703
661
|
export declare function map<K, IN, OUT>(source: Map<K, IN>, func: (value: IN, key: K) => undefined | OUT): Map<K, OUT>;
|
|
704
|
-
/** When using an object as `source`. */
|
|
705
|
-
export declare function map<IN, const IN_KEY extends string | number | symbol, OUT>(source: Record<IN_KEY, IN>, func: (value: IN, index: KeyToString<IN_KEY>) => undefined | OUT): Record<string | symbol, OUT>;
|
|
706
662
|
/** When using an array as `source`. */
|
|
707
663
|
export declare function map<IN, OUT>(source: Array<IN>, func: (value: IN, index: number) => undefined | OUT): Array<OUT>;
|
|
664
|
+
/** When using an object as `source`. */
|
|
665
|
+
export declare function map<IN, const IN_KEY extends string | number | symbol, OUT>(source: Record<IN_KEY, IN>, func: (value: IN, index: KeyToString<IN_KEY>) => undefined | OUT): Record<string | symbol, OUT>;
|
|
708
666
|
/** When using an array as `source`. */
|
|
709
667
|
export declare function multiMap<IN, OUT extends {
|
|
710
668
|
[key: string | symbol]: any;
|