forked from glimmerjs/glimmer-vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodifiers.ts
93 lines (78 loc) · 2.46 KB
/
modifiers.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
import type {
CapturedArguments,
Destroyable,
Dict,
InternalModifierManager,
Nullable,
Owner,
SimpleElement,
} from '@glimmer/interfaces';
import type { UpdatableTag } from '@glimmer/validator';
import { registerDestructor } from '@glimmer/destroyable';
import { reifyNamed, reifyPositional } from '@glimmer/runtime';
import { createUpdatableTag } from '@glimmer/validator';
export interface TestModifierConstructor {
new (): TestModifierInstance;
}
export interface TestModifierInstance {
element?: SimpleElement;
didInsertElement?(_params: unknown[], _hash: Dict<unknown>): void;
didUpdate?(_params: unknown[], _hash: Dict<unknown>): void;
willDestroyElement?(): void;
}
export class TestModifierDefinitionState {
constructor(public Klass?: TestModifierConstructor) {}
}
export class TestModifierManager
implements InternalModifierManager<TestModifier, TestModifierDefinitionState>
{
create(
_owner: Owner,
element: SimpleElement,
state: TestModifierDefinitionState,
args: CapturedArguments
) {
let instance = state.Klass ? new state.Klass() : undefined;
return new TestModifier(element, instance, args);
}
getTag({ tag }: TestModifier): UpdatableTag {
return tag;
}
getDebugName({ Klass }: TestModifierDefinitionState) {
return Klass?.name || '<unknown>';
}
getDebugInstance({ instance }: TestModifier) {
return instance;
}
install({ element, args, instance }: TestModifier) {
// Do this eagerly to ensure they are tracked
let positional = reifyPositional(args.positional);
let named = reifyNamed(args.named);
if (instance && instance.didInsertElement) {
instance.element = element;
instance.didInsertElement(positional, named);
}
if (instance && instance.willDestroyElement) {
registerDestructor(instance, () => instance.willDestroyElement!(), true);
}
}
update({ args, instance }: TestModifier) {
// Do this eagerly to ensure they are tracked
let positional = reifyPositional(args.positional);
let named = reifyNamed(args.named);
if (instance && instance.didUpdate) {
instance.didUpdate(positional, named);
}
}
getDestroyable(modifier: TestModifier): Nullable<Destroyable> {
return modifier.instance || null;
}
}
export class TestModifier {
public tag = createUpdatableTag();
constructor(
public element: SimpleElement,
public instance: TestModifierInstance | undefined,
public args: CapturedArguments
) {}
}