Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | /**
* Interface for a component for the {@link ComponentRegistry}.
*/
export interface Component {
el: HTMLElement;
dispose?: () => void;
}
/**
* Component registry. The registry monitors the DOM for changes and
* automatically mounts and unmounts any components as elements enter and
* leave the DOM.
*
* Within the DOM, components are marked by the `data-component="ComponentName"`
* attribute. An element entering the DOM with that attribute will be
* initialized by the registered component, along with any args passed to the
* [register]{@link ComponentRegistry#register} method.
*
* Usage:
*
* ```
* // main.ts
* class FooComponent {
* el: HTMLElement;
* options: any;
* constructor(el: HTMLElement, options: any) {
* this.el = el;
* this.options = options;
* }
* }
*
* const registry = new ComponentRegistry();
* registry.register('FooComponent', FooComponent, {myOption: 'value'});
* registry.start();
* ```
*
* ```
* <!-- foo.html -->
* <div class="foo" data-component="FooComponent"></div>
* ```
*
* In the example above, when the "foo" element enters the DOM, the following
* would be called automatically by the registry:
*
* ```
* new FooComponent(el, {myOption: 'value'})
* ```
*/
export class ComponentRegistry {
observer: MutationObserver = new MutationObserver(this.onMutate.bind(this));
managers: ComponentLifecycleManager[] = [];
/** Disposes the registry. */
dispose() {
this.managers.forEach((h) => h.dispose());
this.managers = [];
this.stop();
}
/** Registers a component to the registry. */
register(
name: string,
cls: new (el: HTMLElement, ...args: any[]) => Component,
...args: unknown[]
): ComponentRegistry {
this.managers.push(new ComponentLifecycleManager(name, cls, ...args));
return this;
}
/** Starts the component registry. */
start() {
this.observer.observe(document.body, {
attributes: true,
attributeFilter: ['data-component'],
subtree: true,
childList: true,
});
this.update();
}
/** Stops the component registry observer. */
stop() {
this.observer.disconnect();
}
/** Updates all component handlers registered to the registry. */
update() {
this.managers.forEach((m) => m.update());
}
private onMutate(mutationList: MutationRecord[]) {
// At the moment, any observed mutation will trigger all lifecycle managers
// to update its components. Need to check the mutation list to only
// trigger the ones that need to be updated.
// TODO(stevenle): optimize this.
this.update();
}
}
/**
* Lifecycle manager for a registered component. Handles the mounting and
* unmounting of components as they enter or leave the DOM.
*/
class ComponentLifecycleManager {
/** The name of the component. */
name: string;
/** Constructor to create a component. */
cls: new (
el: HTMLElement,
...args: unknown[]
) => Component;
/** Any args to pass to the component's constructor. */
args: unknown[];
/** A map of elements and their mounted components. */
mounted: Map<HTMLElement, Component> = new Map();
constructor(
name: string,
cls: new (el: HTMLElement, ...args: unknown[]) => Component,
...args: unknown[]
) {
this.name = name;
this.cls = cls;
this.args = args;
}
/** Checks if the element is mounted by a component handled by the manager. */
isMounted(el: HTMLElement) {
return this.mounted.has(el);
}
/** Mounts or unmounts all components handled by the manager. */
update() {
const active: Map<HTMLElement, Component> = new Map();
const nodeList = document.querySelectorAll(
`[data-component="${this.name}"]`
);
// Mount any new components that have entered the DOM.
for (let i = 0; i < nodeList.length; i++) {
const el = nodeList[i] as HTMLElement;
let component = this.mounted.get(el);
if (!component) {
component = new this.cls(el, ...this.args);
el.setAttribute('data-component-mounted', 'true');
}
active.set(el, component);
}
// Dispose any components that have left the DOM.
const diff = Array.from(this.mounted.keys()).filter(
(el) => !active.has(el)
);
diff.forEach((el) => {
const component = this.mounted.get(el)!;
component.el.removeAttribute('data-component-mounted');
if (component.dispose) {
component.dispose();
}
});
this.mounted = active;
}
/** Disposes any component mounted by the manager. */
dispose() {
this.mounted.forEach((component) => {
component.el.removeAttribute('data-component-mounted');
if (component.dispose) {
component.dispose();
}
});
this.mounted.clear();
}
}
export default ComponentRegistry;
|