forked from glimmerjs/glimmer-vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodifier.ts
177 lines (144 loc) · 5.54 KB
/
modifier.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
import type {
Arguments,
CapturedArguments,
InternalModifierManager,
ModifierCapabilities,
ModifierCapabilitiesVersions,
ModifierManager,
Owner,
SimpleElement,
UpdatableTag,
} from '@glimmer/interfaces';
import { registerDestructor } from '@glimmer/destroyable';
import { valueForRef } from '@glimmer/reference';
import { castToBrowser, dict } from '@glimmer/util';
import { createUpdatableTag, untrack } from '@glimmer/validator';
import type { ManagerFactory } from '.';
import { argsProxyFor } from '../util/args-proxy';
import { buildCapabilities, FROM_CAPABILITIES } from '../util/capabilities';
export function modifierCapabilities<Version extends keyof ModifierCapabilitiesVersions>(
managerAPI: Version,
optionalFeatures: ModifierCapabilitiesVersions[Version] = {}
): ModifierCapabilities {
if (import.meta.env.DEV && managerAPI !== '3.22') {
throw new Error('Invalid modifier manager compatibility specified');
}
return buildCapabilities({
disableAutoTracking: Boolean(optionalFeatures.disableAutoTracking),
});
}
export interface CustomModifierState<ModifierInstance> {
tag: UpdatableTag;
element: SimpleElement;
modifier: ModifierInstance;
delegate: ModifierManager<ModifierInstance>;
args: Arguments;
debugName?: string;
}
/**
The CustomModifierManager allows addons to provide custom modifier
implementations that integrate seamlessly into Ember. This is accomplished
through a delegate, registered with the custom modifier manager, which
implements a set of hooks that determine modifier behavior.
To create a custom modifier manager, instantiate a new CustomModifierManager
class and pass the delegate as the first argument:
```js
let manager = new CustomModifierManager({
// ...delegate implementation...
});
```
## Delegate Hooks
Throughout the lifecycle of a modifier, the modifier manager will invoke
delegate hooks that are responsible for surfacing those lifecycle changes to
the end developer.
* `createModifier()` - invoked when a new instance of a modifier should be created
* `installModifier()` - invoked when the modifier is installed on the element
* `updateModifier()` - invoked when the arguments passed to a modifier change
* `destroyModifier()` - invoked when the modifier is about to be destroyed
*/
export class CustomModifierManager<O extends Owner, ModifierInstance>
implements InternalModifierManager<CustomModifierState<ModifierInstance>>
{
private componentManagerDelegates = new WeakMap<O, ModifierManager<ModifierInstance>>();
constructor(private factory: ManagerFactory<O, ModifierManager<ModifierInstance>>) {}
private getDelegateFor(owner: O) {
let { componentManagerDelegates } = this;
let delegate = componentManagerDelegates.get(owner);
if (delegate === undefined) {
let { factory } = this;
delegate = factory(owner);
if (import.meta.env.DEV && !FROM_CAPABILITIES!.has(delegate.capabilities)) {
// TODO: This error message should make sense in both Ember and Glimmer https://github.com/glimmerjs/glimmer-vm/issues/1200
throw new Error(
`Custom modifier managers must have a \`capabilities\` property that is the result of calling the \`capabilities('3.22')\` (imported via \`import { capabilities } from '@ember/modifier';\`). Received: \`${JSON.stringify(
delegate.capabilities
)}\` for: \`${delegate}\``
);
}
componentManagerDelegates.set(owner, delegate);
}
return delegate;
}
create(owner: O, element: SimpleElement, definition: object, capturedArgs: CapturedArguments) {
let delegate = this.getDelegateFor(owner);
let args = argsProxyFor(capturedArgs, 'modifier');
let instance: ModifierInstance = delegate.createModifier(definition, args);
let tag = createUpdatableTag();
let state: CustomModifierState<ModifierInstance>;
state = {
tag,
element,
delegate,
args,
modifier: instance,
};
registerDestructor(state, () => delegate.destroyModifier(instance, args));
return state;
}
getDebugName(definition: object) {
if (typeof definition === 'function') {
return definition.name || definition.toString();
} else {
return '<unknown>';
}
}
getDebugInstance({ modifier }: CustomModifierState<ModifierInstance>) {
return modifier;
}
getTag({ tag }: CustomModifierState<ModifierInstance>) {
return tag;
}
install({ element, args, modifier, delegate }: CustomModifierState<ModifierInstance>) {
let { capabilities } = delegate;
if (capabilities.disableAutoTracking === true) {
untrack(() => delegate.installModifier(modifier, castToBrowser(element, 'ELEMENT'), args));
} else {
delegate.installModifier(modifier, castToBrowser(element, 'ELEMENT'), args);
}
}
update({ args, modifier, delegate }: CustomModifierState<ModifierInstance>) {
let { capabilities } = delegate;
if (capabilities.disableAutoTracking === true) {
untrack(() => delegate.updateModifier(modifier, args));
} else {
delegate.updateModifier(modifier, args);
}
}
getDestroyable(state: CustomModifierState<ModifierInstance>) {
return state;
}
}
export function reifyArgs({ named, positional }: CapturedArguments): {
named: Record<string, unknown>;
positional: unknown[];
} {
let reifiedNamed = dict();
for (const [key, value] of Object.entries(named)) {
reifiedNamed[key] = valueForRef(value);
}
let reifiedPositional = positional.map(valueForRef);
return {
named: reifiedNamed,
positional: reifiedPositional,
};
}