-
Notifications
You must be signed in to change notification settings - Fork 0
/
be-dispatching.ts
93 lines (86 loc) · 2.89 KB
/
be-dispatching.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 {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, DispatchRule} from './types';
import {register} from 'be-hive/register.js';
export class BeDispatching extends BE<AP, Actions> implements Actions{
#abortControllers: Array<AbortController> = [];
detach(): void {
for(const ac of this.#abortControllers){
ac.abort();
}
}
static override get beConfig(){
return {
parse: true,
parseAndCamelize: true,
isParsedProp: 'isParsed'
} as BEConfig;
}
async onCamelized(self: this) {
const {of, Of} = self;
let dispatchRules: Array<DispatchRule> = [];
if((of || Of) !== undefined){
const {prsOf} = await import('./prsOf.js');
dispatchRules = prsOf(self);
}
return {
dispatchRules
};
}
async hydrate(self: this){
const {enhancedElement, dispatchRules} = self;
for(const rule of dispatchRules!){
const abortController: AbortController = new AbortController();
this.#abortControllers.push(abortController);
let {dispatchOn} = rule;
let eventTar = enhancedElement as EventTarget;
if(dispatchOn === undefined){
const {getDefaultSignalInfo} = await import('be-linked/getDefaultSignalInfo.js');
const signalInfo = getDefaultSignalInfo(enhancedElement);
dispatchOn = signalInfo.type;
eventTar = signalInfo.eventTarget;
}
eventTar.addEventListener(dispatchOn!, e => {
const {replace, bubbles, composed, cancelable, dispatch} = rule;
if(replace){
e.stopImmediatePropagation();
e.stopPropagation();
}
enhancedElement.dispatchEvent(new CustomEvent(dispatch!, {
bubbles, composed, cancelable
}))
}, {signal: abortController.signal});
}
const {nudge} = await import('trans-render/lib/nudge.js');
nudge(enhancedElement);
return {
resolved: true,
}
}
}
export interface BeDispatching extends AllProps{}
const tagName = 'be-dispatching';
const ifWantsToBe = 'dispatching';
const upgrade = '*';
const xe = new XE<AP, Actions>({
config:{
tagName,
isEnh: true,
propDefaults:{
...propDefaults,
},
propInfo:{
...propInfo
},
actions:{
onCamelized:{
ifAllOf: ['isParsed'],
ifAtLeastOneOf: ['of', 'Of',],
},
hydrate: 'dispatchRules'
}
},
superclass: BeDispatching,
});
register(ifWantsToBe, upgrade, tagName);