-
Notifications
You must be signed in to change notification settings - Fork 0
/
be-promising.ts
102 lines (89 loc) · 3.33 KB
/
be-promising.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
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, Instruction} from './types';
import {register} from 'be-hive/register.js';
import {camelToLisp} from 'trans-render/lib/camelToLisp.js';
import { lispToCamel } from 'trans-render/lib/lispToCamel.js';
export class BePromising extends BE<AP, Actions> implements Actions{
static override get beConfig(){
return {
parse: true,
primaryProp: 'be'
} as BEConfig;
}
async onBe(self: this) {
const {be, enhancedElement} = self;
for(const instruction of be){
await this.doInstruction(enhancedElement, instruction);
}
return {
resolved: true,
}
}
async doInstruction(enhancedElement: Element, instruction: Instruction){
switch(typeof instruction){
case 'string':
await (<any>enhancedElement).beEnhanced.whenResolved('be-' + instruction);
return;
case 'object':
if(instruction instanceof Array){
for(const childInstruction of instruction){
await this.doInstruction(enhancedElement, childInstruction);
}
}else{
for(const key in instruction){
const enh = 'be-' + key;
const enhancement = lispToCamel(enh);
const val = instruction[key];
switch(typeof val){
case 'string':
(<any>enhancedElement).beEnhanced.by[enhancement] = val;
break;
case 'object':
if(Array.isArray(val)) throw 'NI';
Object.assign((<any>enhancedElement).beEnhanced.by[enhancement], val);
break;
default:
throw 'NI';
}
await (<any>enhancedElement).beEnhanced.whenResolved(enh);
}
}
}
}
async onResolved(self: this){
const {enhancedElement, be} = self;
if(enhancedElement.hasAttribute('be-promising')){
const guid = 'a' + counter++;
enhancedElement.setAttribute('data-bpguid', guid);
const rn = enhancedElement.getRootNode() as any;
if(rn[enhancementGuid] === undefined) rn[enhancementGuid] = {};
rn[enhancementGuid][guid] = be;
}
await (<any>enhancedElement).beEnhanced.whenDetached('be-promising');
}
}
export const enhancementGuid = 'QA7Ey8RiUuHVDZ3lw';
let counter = 0;
export interface BePromising extends AllProps{}
const tagName = 'be-promising';
const ifWantsToBe = 'promising';
const upgrade = '*';
const xe = new XE<AP, Actions>({
config:{
tagName,
propDefaults:{
...propDefaults,
},
propInfo:{
...propInfo,
},
actions: {
onBe: 'be',
onResolved: 'resolved'
}
},
superclass: BePromising
});
register(ifWantsToBe, upgrade, tagName);