-
Notifications
You must be signed in to change notification settings - Fork 0
/
be-exportable.js
99 lines (98 loc) · 2.97 KB
/
be-exportable.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
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
// @ts-check
import { resolved, rejected, propInfo } from 'be-enhanced/cc.js';
import { BE } from 'be-enhanced/BE.js';
/** @import {Actions} from './ts-refs/be-exportable/types' */
//TODO: store in truly global place based on guid (symbol.for)
const sharedTags = new Map();
/**
* @implements {Actions}
*/
class BeExportable extends BE {
static config = {
propInfo: {
...propInfo,
attached: {
def: true,
ro: true,
}
},
actions: {
hydrate: {
ifAllOf: ['attached']
}
},
positractions: [
resolved, rejected
]
};
#emc;
async attach(el, enhancementInfo) {
const { mountCnfg } = enhancementInfo;
this.#emc = mountCnfg;
await super.attach(el, enhancementInfo);
}
async hydrate(self) {
const { enhancedElement, preferAttrForBareImports } = self;
if (enhancedElement.hasAttribute('blow-dry') || enhancedElement.dataset.blowDry) {
const { blowDry } = await import('./blowDry.js');
return await blowDry(self, this.#emc);
}
delete enhancedElement.dataset.loaded;
let { id } = enhancedElement;
if (!id) {
id = 'shared-' + crypto.randomUUID();
enhancedElement.id = id;
}
if (id.startsWith('shared-')) {
if (sharedTags.has(id)) {
const sharedElement = sharedTags.get(id);
await sharedElement.whenResolved();
self.exports = sharedElement.exports;
self.dispatchEvent(new Event('load'));
enhancedElement.dataset.loaded = 'true';
sharedElement.innerHTML = '';
return {
resolved: true
};
}
else {
sharedTags.set(id, self);
}
}
self.exports = {};
let innerText;
let src;
if (preferAttrForBareImports) {
const attr = enhancedElement.getAttribute('src');
if (attr !== null && !attr.startsWith('.') && !attr.startsWith('/')) {
src = attr;
}
else {
src = enhancedElement.src;
}
}
else {
src = enhancedElement.src;
}
if (src) {
const module = await import(src); //.then(module => {
self.exports = module;
self.dispatchEvent(new Event('load'));
enhancedElement.dataset.loaded = 'true';
return {
resolved: true
};
}
else {
const { doInline } = await import('./doInline.js');
await doInline(enhancedElement);
//the script itself does the resolving
//self.resolved = true;
}
return {
//resolved: true
};
}
}
await BeExportable.bootUp();
export { BeExportable };