-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.ts
61 lines (57 loc) · 1.32 KB
/
context.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
import { Config, SideEffect, SideEffects } from "./";
type TAddEffectFn<T = SideEffect> = (
effect: SideEffect | Promise<T>
) => Promise<T>;
export default class Context {
add_effect: TAddEffectFn;
config: Config.Config;
language: string;
constructor(
add_effect: TAddEffectFn,
config: Config.Config,
language?: string
) {
this.add_effect = add_effect;
this.config = config;
this.language = language || "en";
}
async addExternalScript(src: string) {
const script_file = await SideEffects.File.fromURL(src);
await Promise.all([
this.add_effect(script_file),
script_file.getUrlPlaceholder().then((placeholder) => {
const html = /* HTML */ `<script
async
src=${placeholder}
></script>`;
return this.add_effect(
new SideEffects.HtmlChunk(
html,
"head",
`external-script-${src}`
)
);
}),
]);
}
async addExternalStylesheet(href: string) {
const sheet_file = await SideEffects.File.fromURL(href);
await Promise.all([
this.add_effect(sheet_file),
sheet_file.getUrlPlaceholder().then((placeholder) => {
const html = /* HTML */ `<link
rel="stylesheet"
async
href=${placeholder}
/>`;
return this.add_effect(
new SideEffects.HtmlChunk(
html,
"head",
`external-sheet-${href}`
)
);
}),
]);
}
}