-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
custom.ts
319 lines (271 loc) · 9.36 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import { ENV } from '@ember/-internals/environment';
import { Factory } from '@ember/-internals/owner';
import { assert } from '@ember/debug';
import {
Arguments,
Bounds,
ComponentCapabilities,
ComponentDefinition,
Destroyable,
Dict,
Option,
VMArguments,
WithStaticLayout,
} from '@glimmer/interfaces';
import { createConstRef, Reference } from '@glimmer/reference';
import { registerDestructor } from '@glimmer/runtime';
import { unwrapTemplate } from '@glimmer/util';
import { EmberVMEnvironment } from '../environment';
import RuntimeResolver from '../resolver';
import { OwnedTemplate } from '../template';
import { argsProxyFor } from '../utils/args-proxy';
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,
wrapped: false,
willDestroy: false,
};
export interface OptionalCapabilities {
'3.4': {
asyncLifecycleCallbacks?: boolean;
destructor?: boolean;
};
'3.13': {
asyncLifecycleCallbacks?: boolean;
destructor?: boolean;
updateHook?: boolean;
};
}
export function capabilities<Version extends keyof OptionalCapabilities>(
managerAPI: Version,
options: OptionalCapabilities[Version] = {}
): Capabilities {
assert(
'Invalid component manager compatibility specified',
managerAPI === '3.4' || managerAPI === '3.13'
);
let updateHook = true;
if (managerAPI === '3.13') {
updateHook = Boolean((options as OptionalCapabilities['3.13']).updateHook);
}
return {
asyncLifeCycleCallbacks: Boolean(options.asyncLifecycleCallbacks),
destructor: Boolean(options.destructor),
updateHook,
};
}
export interface DefinitionState<ComponentInstance> {
name: string;
ComponentClass: Factory<ComponentInstance>;
template: OwnedTemplate;
}
export interface Capabilities {
asyncLifeCycleCallbacks: boolean;
destructor: boolean;
updateHook: boolean;
}
// TODO: export ICapturedArgumentsValue from glimmer and replace this
export interface Args {
named: Dict<unknown>;
positional: unknown[];
}
export interface ManagerDelegate<ComponentInstance> {
capabilities: Capabilities;
createComponent(factory: unknown, args: Args): ComponentInstance;
getContext(instance: ComponentInstance): unknown;
}
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;
}
export function hasUpdateHook<ComponentInstance>(
delegate: ManagerDelegate<ComponentInstance>
): delegate is ManagerDelegateWithUpdateHook<ComponentInstance> {
return delegate.capabilities.updateHook;
}
export interface ManagerDelegateWithUpdateHook<ComponentInstance>
extends ManagerDelegate<ComponentInstance> {
updateComponent(instance: ComponentInstance, args: Args): void;
}
export function hasAsyncUpdateHook<ComponentInstance>(
delegate: ManagerDelegate<ComponentInstance>
): delegate is ManagerDelegateWithAsyncUpdateHook<ComponentInstance> {
return hasAsyncLifeCycleCallbacks(delegate) && hasUpdateHook(delegate);
}
export interface ManagerDelegateWithAsyncUpdateHook<ComponentInstance>
extends ManagerDelegateWithAsyncLifeCycleCallbacks<ComponentInstance>,
ManagerDelegateWithUpdateHook<ComponentInstance> {
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: unknown[];
named: Dict<unknown>;
}
/**
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>,
CustomComponentDefinitionState<ComponentInstance>
>
implements
WithStaticLayout<
CustomComponentState<ComponentInstance>,
CustomComponentDefinitionState<ComponentInstance>,
RuntimeResolver
> {
create(
env: EmberVMEnvironment,
definition: CustomComponentDefinitionState<ComponentInstance>,
vmArgs: VMArguments
): CustomComponentState<ComponentInstance> {
let { delegate } = definition;
let args = argsProxyFor(vmArgs.capture(), 'component');
let component = delegate.createComponent(definition.ComponentClass.class, args);
let bucket = new CustomComponentState(delegate, component, args, env);
if (ENV._DEBUG_RENDER_TREE) {
env.extra.debugRenderTree.create(bucket, {
type: 'component',
name: definition.name,
args: vmArgs.capture(),
instance: component,
template: definition.template,
});
registerDestructor(bucket, () => {
env.extra.debugRenderTree.willDestroy(bucket);
});
}
return bucket;
}
getDebugName({ name }: CustomComponentDefinitionState<ComponentInstance>) {
return name;
}
update(bucket: CustomComponentState<ComponentInstance>) {
if (ENV._DEBUG_RENDER_TREE) {
bucket.env.extra.debugRenderTree.update(bucket);
}
if (hasUpdateHook(bucket.delegate)) {
let { delegate, component, args } = bucket;
delegate.updateComponent(component, args);
}
}
didCreate({ delegate, component }: CustomComponentState<ComponentInstance>) {
if (hasAsyncLifeCycleCallbacks(delegate)) {
delegate.didCreateComponent(component);
}
}
didUpdate({ delegate, component }: CustomComponentState<ComponentInstance>) {
if (hasAsyncUpdateHook(delegate)) {
delegate.didUpdateComponent(component);
}
}
getContext({ delegate, component }: CustomComponentState<ComponentInstance>) {
delegate.getContext(component);
}
getSelf({ delegate, component }: CustomComponentState<ComponentInstance>): Reference {
return createConstRef(delegate.getContext(component), 'this');
}
getDestroyable(bucket: CustomComponentState<ComponentInstance>): Option<Destroyable> {
return bucket;
}
getCapabilities({
delegate,
}: CustomComponentDefinitionState<ComponentInstance>): ComponentCapabilities {
return Object.assign({}, CAPABILITIES, {
updateHook: ENV._DEBUG_RENDER_TREE || delegate.capabilities.updateHook,
});
}
didRenderLayout(bucket: CustomComponentState<ComponentInstance>, bounds: Bounds) {
if (ENV._DEBUG_RENDER_TREE) {
bucket.env.extra.debugRenderTree.didRender(bucket, bounds);
}
}
didUpdateLayout(bucket: CustomComponentState<ComponentInstance>, bounds: Bounds) {
if (ENV._DEBUG_RENDER_TREE) {
bucket.env.extra.debugRenderTree.didRender(bucket, bounds);
}
}
getStaticLayout(state: DefinitionState<ComponentInstance>) {
return unwrapTemplate(state.template).asLayout();
}
}
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: Arguments,
public env: EmberVMEnvironment
) {
if (hasDestructors(delegate)) {
registerDestructor(this, () => delegate.destroyComponent(component));
}
}
}
export interface CustomComponentDefinitionState<ComponentInstance>
extends DefinitionState<ComponentInstance> {
delegate: ManagerDelegate<ComponentInstance>;
}
export class CustomManagerDefinition<ComponentInstance> implements ComponentDefinition {
public state: CustomComponentDefinitionState<ComponentInstance>;
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
) {
this.state = {
name,
ComponentClass,
template,
delegate,
};
}
}