-
Notifications
You must be signed in to change notification settings - Fork 25
/
provider-mixin.js
53 lines (49 loc) · 1.27 KB
/
provider-mixin.js
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
export class ProviderDelegate {
constructor(delegate, noCache) {
this._noCache = noCache;
this._delegate = delegate;
}
getValue() {
if (this._noCache || this._value === undefined) {
this._value = this._delegate();
}
return this._value;
}
}
export function provideInstance(node, key, obj) {
if (!node._providerInstances) {
node._providerInstances = new Map();
node.addEventListener('d2l-request-instance', e => {
if (node._providerInstances.has(e.detail.key)) {
const instance = node._providerInstances.get(e.detail.key);
if (instance instanceof ProviderDelegate) {
e.detail.instance = instance.getValue();
} else {
e.detail.instance = instance;
}
e.stopPropagation();
}
});
}
node._providerInstances.set(key, obj);
}
export const ProviderMixin = superclass => class extends superclass {
provideInstance(key, obj) {
provideInstance(this, key, obj);
}
};
export function requestInstance(node, key) {
const event = new CustomEvent('d2l-request-instance', {
detail: { key },
bubbles: true,
composed: true,
cancelable: true
});
node.dispatchEvent(event);
return event.detail.instance;
}
export const RequesterMixin = superclass => class extends superclass {
requestInstance(key) {
return requestInstance(this, key);
}
};