-
-
Notifications
You must be signed in to change notification settings - Fork 835
Expand file tree
/
Copy pathreactive-controller.ts
More file actions
84 lines (71 loc) · 2.32 KB
/
reactive-controller.ts
File metadata and controls
84 lines (71 loc) · 2.32 KB
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
import type { ComponentInterface } from '../declarations/stencil-public-runtime';
import { forceUpdate } from './update-component';
export interface ReactiveController {
hostConnected?(): void;
hostDisconnected?(): void;
hostWillLoad?(): Promise<void> | void;
hostDidLoad?(): void;
hostWillRender?(): Promise<void> | void;
hostDidRender?(): void;
hostWillUpdate?(): Promise<void> | void;
hostDidUpdate?(): void;
}
/**
* Base class for components that want to use reactive controllers.
*
* Components extending this class can use the composition pattern to share
* stateful logic via reactive controllers.
*
* Known Limitation: Components extending ReactiveControllerHost cannot use
* `<Host>` as their root element in the render method. This is because
* ReactiveControllerHost does not extend HTMLElement. Instead, return a
* regular element (like `<div>`) as the root.
*
* @example
* ```tsx
* @Component({ tag: 'my-component' })
* export class MyComponent extends ReactiveControllerHost {
* private myController = new MyController(this);
*
* render() {
* return <div>...</div>; // Use <div>, not <Host>
* }
* }
* ```
*/
export class ReactiveControllerHost implements ComponentInterface {
controllers = new Set<ReactiveController>();
addController(controller: ReactiveController) {
this.controllers.add(controller);
}
removeController(controller: ReactiveController) {
this.controllers.delete(controller);
}
requestUpdate() {
forceUpdate(this);
}
connectedCallback() {
this.controllers.forEach((controller) => controller.hostConnected?.());
}
disconnectedCallback() {
this.controllers.forEach((controller) => controller.hostDisconnected?.());
}
componentWillLoad() {
this.controllers.forEach((controller) => controller.hostWillLoad?.());
}
componentDidLoad() {
this.controllers.forEach((controller) => controller.hostDidLoad?.());
}
componentWillRender() {
this.controllers.forEach((controller) => controller.hostWillRender?.());
}
componentDidRender() {
this.controllers.forEach((controller) => controller.hostDidRender?.());
}
componentWillUpdate() {
this.controllers.forEach((controller) => controller.hostWillUpdate?.());
}
componentDidUpdate() {
this.controllers.forEach((controller) => controller.hostDidUpdate?.());
}
}