-
Notifications
You must be signed in to change notification settings - Fork 0
/
be-functional.ts
72 lines (66 loc) · 2.23 KB
/
be-functional.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
import {BE, propDefaults, propInfo} from 'be-enhanced/BE.js';
import {BEConfig} from 'be-enhanced/types';
import {XE} from 'xtal-element/XE.js';
import {Actions, AllProps, AP, PAP, ProPAP, POA} from './types';
import {register} from 'be-hive/register.js';
export class BeFunctional extends BE<AP, Actions> implements Actions{
static override get beConfig(){
return {
parse: true,
primaryProp: 'fnParams',
primaryPropReq: true,
} as BEConfig
}
#exportsLookup = new Map<string, any>();
onFnParams(self: this): Partial<AllProps> {
const {fnParams, enhancedElement} = self;
import('be-exportable/be-exportable.js');
for(const key in fnParams){
const param = fnParams[key];
enhancedElement.addEventListener(key, async (e: Event) => {
const {scriptRef, fn} = param;
if(this.#exportsLookup.has(scriptRef)){
const exports = this.#exportsLookup.get(scriptRef);
const fun = exports[fn];
fun.bind(self)(e);
return;
}
const rn = enhancedElement.getRootNode() as DocumentFragment;
const scriptEl = rn.getElementById(scriptRef);
if(scriptEl === null) throw '404';
const base = await (<any>scriptEl).beEnhanced.whenResolved('be-exportable');
const exports = base.exports;
this.#exportsLookup.set(scriptRef, exports);
const fun = exports[fn];
fun.bind(self)(e);
});
}
return{
resolved: true,
} as PAP;
}
}
export interface BeFunctional extends AllProps{}
const tagName = 'be-functional';
const ifWantsToBe = 'functional';
const upgrade = '*';
const xe = new XE<AP, Actions>({
config: {
tagName,
propDefaults: {
...propDefaults,
},
propInfo: {
...propInfo,
fnParams: {
type: 'Object',
parse: false,
}
},
actions:{
onFnParams: 'fnParams'
}
},
superclass: BeFunctional
});
register(ifWantsToBe, upgrade, tagName);