/
custom.ts
259 lines (220 loc) · 7.5 KB
/
custom.ts
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import { Factory } from '@ember/-internals/owner';
import { OwnedTemplateMeta } from '@ember/-internals/views';
import { assert } from '@ember/debug';
import {
ComponentCapabilities,
Dict,
Opaque,
Option,
ProgramSymbolTable,
} from '@glimmer/interfaces';
import { PathReference, Tag } from '@glimmer/reference';
import {
Arguments,
CapturedArguments,
ComponentDefinition,
Invocation,
PrimitiveReference,
WithStaticLayout,
} from '@glimmer/runtime';
import { Destroyable } from '@glimmer/util';
import Environment from '../environment';
import RuntimeResolver from '../resolver';
import { OwnedTemplate } from '../template';
import { ManagerArgs, valueForCapturedArgs } from '../utils/managers';
import { RootReference } from '../utils/references';
import AbstractComponentManager from './abstract';
const CAPABILITIES = {
dynamicLayout: false,
dynamicTag: false,
prepareArgs: false,
createArgs: true,
attributeHook: false,
elementHook: false,
createCaller: false,
dynamicScope: true,
updateHook: true,
createInstance: true,
};
export interface OptionalCapabilities {
asyncLifecycleCallbacks?: boolean;
destructor?: boolean;
}
export function capabilities(managerAPI: '3.4', options: OptionalCapabilities = {}): Capabilities {
assert('Invalid component manager compatibility specified', managerAPI === '3.4');
return {
asyncLifeCycleCallbacks: !!options.asyncLifecycleCallbacks,
destructor: !!options.destructor,
};
}
export interface DefinitionState<ComponentInstance> {
name: string;
ComponentClass: Factory<ComponentInstance>;
symbolTable: ProgramSymbolTable;
template?: any;
}
export interface Capabilities {
asyncLifeCycleCallbacks: boolean;
destructor: boolean;
}
export interface ManagerDelegate<ComponentInstance> {
capabilities: Capabilities;
createComponent(factory: Opaque, args: ManagerArgs): ComponentInstance;
updateComponent(instance: ComponentInstance, args: ManagerArgs): void;
getContext(instance: ComponentInstance): Opaque;
}
export function hasAsyncLifeCycleCallbacks<ComponentInstance>(
delegate: ManagerDelegate<ComponentInstance>
): delegate is ManagerDelegateWithAsyncLifeCycleCallbacks<ComponentInstance> {
return delegate.capabilities.asyncLifeCycleCallbacks;
}
export interface ManagerDelegateWithAsyncLifeCycleCallbacks<ComponentInstance>
extends ManagerDelegate<ComponentInstance> {
didCreateComponent(instance: ComponentInstance): void;
didUpdateComponent(instance: ComponentInstance): void;
}
export function hasDestructors<ComponentInstance>(
delegate: ManagerDelegate<ComponentInstance>
): delegate is ManagerDelegateWithDestructors<ComponentInstance> {
return delegate.capabilities.destructor;
}
export interface ManagerDelegateWithDestructors<ComponentInstance>
extends ManagerDelegate<ComponentInstance> {
destroyComponent(instance: ComponentInstance): void;
}
export interface ComponentArguments {
positional: Opaque[];
named: Dict<Opaque>;
}
/**
The CustomComponentManager allows addons to provide custom component
implementations that integrate seamlessly into Ember. This is accomplished
through a delegate, registered with the custom component manager, which
implements a set of hooks that determine component behavior.
To create a custom component manager, instantiate a new CustomComponentManager
class and pass the delegate as the first argument:
```js
let manager = new CustomComponentManager({
// ...delegate implementation...
});
```
## Delegate Hooks
Throughout the lifecycle of a component, the component manager will invoke
delegate hooks that are responsible for surfacing those lifecycle changes to
the end developer.
* `create()` - invoked when a new instance of a component should be created
* `update()` - invoked when the arguments passed to a component change
* `getContext()` - returns the object that should be
*/
export default class CustomComponentManager<ComponentInstance>
extends AbstractComponentManager<
CustomComponentState<ComponentInstance>,
DefinitionState<ComponentInstance>
>
implements
WithStaticLayout<
CustomComponentState<ComponentInstance>,
DefinitionState<ComponentInstance>,
OwnedTemplateMeta,
RuntimeResolver
> {
create(
_env: Environment,
definition: CustomComponentDefinitionState<ComponentInstance>,
args: Arguments
): CustomComponentState<ComponentInstance> {
const { delegate } = definition;
const capturedArgs = args.capture();
let invocationArgs = valueForCapturedArgs(capturedArgs);
const component = delegate.createComponent(definition.ComponentClass.class, invocationArgs);
return new CustomComponentState(delegate, component, capturedArgs);
}
update({ delegate, component, args }: CustomComponentState<ComponentInstance>) {
delegate.updateComponent(component, valueForCapturedArgs(args));
}
didCreate({ delegate, component }: CustomComponentState<ComponentInstance>) {
if (hasAsyncLifeCycleCallbacks(delegate)) {
delegate.didCreateComponent(component);
}
}
didUpdate({ delegate, component }: CustomComponentState<ComponentInstance>) {
if (hasAsyncLifeCycleCallbacks(delegate)) {
delegate.didUpdateComponent(component);
}
}
getContext({ delegate, component }: CustomComponentState<ComponentInstance>) {
delegate.getContext(component);
}
getSelf({
delegate,
component,
}: CustomComponentState<ComponentInstance>): PrimitiveReference<null> | PathReference<Opaque> {
const context = delegate.getContext(component);
return new RootReference(context);
}
getDestructor(state: CustomComponentState<ComponentInstance>): Option<Destroyable> {
if (hasDestructors(state.delegate)) {
return state;
} else {
return null;
}
}
getCapabilities(): ComponentCapabilities {
return CAPABILITIES;
}
getTag({ args }: CustomComponentState<ComponentInstance>): Tag {
return args.tag;
}
didRenderLayout() {}
getLayout(state: DefinitionState<ComponentInstance>): Invocation {
return {
handle: state.template.asLayout().compile(),
symbolTable: state.symbolTable!,
};
}
}
const CUSTOM_COMPONENT_MANAGER = new CustomComponentManager();
/**
* Stores internal state about a component instance after it's been created.
*/
export class CustomComponentState<ComponentInstance> {
constructor(
public delegate: ManagerDelegate<ComponentInstance>,
public component: ComponentInstance,
public args: CapturedArguments
) {}
destroy() {
const { delegate, component } = this;
if (hasDestructors(delegate)) {
delegate.destroyComponent(component);
}
}
}
export interface CustomComponentDefinitionState<ComponentInstance>
extends DefinitionState<ComponentInstance> {
delegate: ManagerDelegate<ComponentInstance>;
}
export class CustomManagerDefinition<ComponentInstance> implements ComponentDefinition {
public state: CustomComponentDefinitionState<ComponentInstance>;
public symbolTable: ProgramSymbolTable;
public manager: CustomComponentManager<
ComponentInstance
> = CUSTOM_COMPONENT_MANAGER as CustomComponentManager<ComponentInstance>;
constructor(
public name: string,
public ComponentClass: Factory<ComponentInstance>,
public delegate: ManagerDelegate<ComponentInstance>,
public template: OwnedTemplate
) {
const layout = template.asLayout();
const symbolTable = layout.symbolTable;
this.symbolTable = symbolTable;
this.state = {
name,
ComponentClass,
template,
symbolTable,
delegate,
};
}
}