UNPKG

2.92 kBTypeScriptView Raw
1// tslint:disable:variable-name Describing an API that's defined elsewhere.
2// tslint:disable:no-any describes the API as best we are able today
3
4import {calculateSplices} from './array-splice.js';
5
6import {microTask} from './async.js';
7
8export {FlattenedNodesObserver};
9
10/**
11 * Class that listens for changes (additions or removals) to
12 * "flattened nodes" on a given `node`. The list of flattened nodes consists
13 * of a node's children and, for any children that are `<slot>` elements,
14 * the expanded flattened list of `assignedNodes`.
15 * For example, if the observed node has children `<a></a><slot></slot><b></b>`
16 * and the `<slot>` has one `<div>` assigned to it, then the flattened
17 * nodes list is `<a></a><div></div><b></b>`. If the `<slot>` has other
18 * `<slot>` elements assigned to it, these are flattened as well.
19 *
20 * The provided `callback` is called whenever any change to this list
21 * of flattened nodes occurs, where an addition or removal of a node is
22 * considered a change. The `callback` is called with one argument, an object
23 * containing an array of any `addedNodes` and `removedNodes`.
24 *
25 * Note: the callback is called asynchronous to any changes
26 * at a microtask checkpoint. This is because observation is performed using
27 * `MutationObserver` and the `<slot>` element's `slotchange` event which
28 * are asynchronous.
29 *
30 * An example:
31 * ```js
32 * class TestSelfObserve extends PolymerElement {
33 * static get is() { return 'test-self-observe';}
34 * connectedCallback() {
35 * super.connectedCallback();
36 * this._observer = new FlattenedNodesObserver(this, (info) => {
37 * this.info = info;
38 * });
39 * }
40 * disconnectedCallback() {
41 * super.disconnectedCallback();
42 * this._observer.disconnect();
43 * }
44 * }
45 * customElements.define(TestSelfObserve.is, TestSelfObserve);
46 * ```
47 */
48declare class FlattenedNodesObserver {
49
50 /**
51 * eslint-disable-next-line
52 */
53 constructor(target: any, callback: any);
54
55 /**
56 * eslint-disable-next-line
57 */
58 static getFlattenedNodes(node: any): any;
59
60 /**
61 * Activates an observer. This method is automatically called when
62 * a `FlattenedNodesObserver` is created. It should only be called to
63 * re-activate an observer that has been deactivated via the `disconnect` method.
64 */
65 connect(): void;
66
67 /**
68 * Deactivates the flattened nodes observer. After calling this method
69 * the observer callback will not be called when changes to flattened nodes
70 * occur. The `connect` method may be subsequently called to reactivate
71 * the observer.
72 */
73 disconnect(): void;
74
75 /**
76 * Flushes the observer causing any pending changes to be immediately
77 * delivered the observer callback. By default these changes are delivered
78 * asynchronously at the next microtask checkpoint.
79 *
80 * @returns Returns true if any pending changes caused the observer
81 * callback to run.
82 */
83 flush(): boolean;
84}