-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
internal.ts
148 lines (124 loc) · 4.01 KB
/
internal.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
import { ENV } from '@ember/-internals/environment';
import { Owner } from '@ember/-internals/owner';
import { assert } from '@ember/debug';
import {
Bounds,
ComponentCapabilities,
ComponentDefinition,
Destroyable,
DynamicScope,
VMArguments,
WithStaticLayout,
} from '@glimmer/interfaces';
import { createConstRef, isConstRef, Reference, valueForRef } from '@glimmer/reference';
import { registerDestructor } from '@glimmer/runtime';
import { _WeakSet, unwrapTemplate } from '@glimmer/util';
import InternalComponent from '../components/internal';
import { EmberVMEnvironment } from '../environment';
import RuntimeResolver from '../resolver';
import { OwnedTemplate } from '../template';
import AbstractComponentManager from './abstract';
const CAPABILITIES: ComponentCapabilities = {
dynamicLayout: false,
dynamicTag: false,
prepareArgs: false,
createArgs: true,
attributeHook: false,
elementHook: false,
createCaller: true,
dynamicScope: false,
updateHook: true,
createInstance: true,
wrapped: false,
willDestroy: false,
};
export interface InternalDefinitionState {
ComponentClass: typeof InternalComponent;
layout: OwnedTemplate;
}
export interface InternalComponentState {
env: EmberVMEnvironment;
instance: Destroyable;
}
export class InternalComponentDefinition
implements ComponentDefinition<InternalDefinitionState, InternalComponentState, InternalManager> {
public state: InternalDefinitionState;
constructor(
public manager: InternalManager,
ComponentClass: typeof InternalComponent,
layout: OwnedTemplate
) {
this.state = { ComponentClass, layout };
}
}
const INTERNAL_MANAGERS = new _WeakSet();
export function isInternalManager(manager: object): manager is InternalManager {
return INTERNAL_MANAGERS.has(manager);
}
export default class InternalManager
extends AbstractComponentManager<InternalComponentState, InternalDefinitionState>
implements WithStaticLayout<InternalComponentState, InternalDefinitionState, RuntimeResolver> {
static for(name: string): (owner: Owner) => InternalManager {
return (owner: Owner) => new InternalManager(owner, name);
}
constructor(private owner: Owner, private name: string) {
super();
INTERNAL_MANAGERS.add(this);
}
getCapabilities(): ComponentCapabilities {
return CAPABILITIES;
}
create(
env: EmberVMEnvironment,
{ ComponentClass, layout }: InternalDefinitionState,
args: VMArguments,
_dynamicScope: DynamicScope,
caller: Reference
): InternalComponentState {
assert('caller must be const', isConstRef(caller));
assert(
`The ${this.name} component does not take any positional arguments`,
args.positional.length === 0
);
let instance = new ComponentClass(this.owner, args.named.capture(), valueForRef(caller));
let state = { env, instance };
if (ENV._DEBUG_RENDER_TREE) {
env.extra.debugRenderTree.create(state, {
type: 'component',
name: this.getDebugName(),
args: args.capture(),
instance,
template: layout,
});
registerDestructor(instance, () => env.extra.debugRenderTree.willDestroy(state));
}
return state;
}
getDebugName(): string {
return this.name;
}
getSelf({ instance }: InternalComponentState): Reference {
return createConstRef(instance, 'this');
}
didRenderLayout(state: InternalComponentState, bounds: Bounds): void {
if (ENV._DEBUG_RENDER_TREE) {
state.env.extra.debugRenderTree.didRender(state, bounds);
}
}
update(state: InternalComponentState): void {
if (ENV._DEBUG_RENDER_TREE) {
state.env.extra.debugRenderTree.update(state);
}
}
didUpdateLayout(state: InternalComponentState, bounds: Bounds): void {
if (ENV._DEBUG_RENDER_TREE) {
state.env.extra.debugRenderTree.didRender(state, bounds);
}
}
getDestroyable(state: InternalComponentState): Destroyable {
return state.instance;
}
getStaticLayout({ layout: template }: InternalDefinitionState) {
return unwrapTemplate(template).asLayout();
}
}