-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
85 lines (63 loc) · 2.16 KB
/
index.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
import Ember from 'ember';
import ApplicationInstance from '@ember/application/instance';
import { setProperties } from '@ember/object';
import { assign } from '@ember/polyfills';
interface setComponentManager<T> {
(managerFactory: (owner: ApplicationInstance) => unknown, baseClass: T): T
}
export interface ComponentManagerArgs {
named: object;
positional: any[];
}
const setComponentManager = (Ember as any)._setComponentManager as setComponentManager<FunctionalComponent>;
interface CreateComponentResult {
fn: FunctionalComponent;
args: ComponentManagerArgs;
templateContext: unknown;
}
interface FunctionalComponent {
(props: object): object;
}
const MANAGERS = new WeakMap<ApplicationInstance, FunctionalComponentManager>();
class FunctionalComponentManager {
//private owner: ApplicationInstance;
public capabilities: any;
static forOwner(owner: ApplicationInstance) {
let instance = MANAGERS.get(owner);
if (instance !== undefined) {
return instance;
}
instance = new this(owner);
MANAGERS.set(owner, instance);
return instance;
}
constructor(_owner: ApplicationInstance) {
//this.owner = owner;
this.capabilities = (Ember as any)._componentManagerCapabilities('3.4', {
destructor: true,
asyncLifecycleCallbacks: true,
});
}
createComponent(fn: FunctionalComponent, args: ComponentManagerArgs): CreateComponentResult {
let templateContext = assign({}, fn(args.named));
return {
fn,
args,
templateContext,
}
}
updateComponent(bucket: CreateComponentResult, args: ComponentManagerArgs) {
let updatedTemplateContext = bucket.fn(args.named);
setProperties(bucket.templateContext, updatedTemplateContext);
}
destroyComponent(_bucket: CreateComponentResult) {}
getContext(bucket: CreateComponentResult) {
return bucket.templateContext;
}
didCreateComponent(_bucket: CreateComponentResult) {}
didUpdateComponent(_bucket: CreateComponentResult) {}
}
export function createComponent(callback: FunctionalComponent) {
setComponentManager((owner: ApplicationInstance) => FunctionalComponentManager.forOwner(owner), callback);
return callback;
}