-
Notifications
You must be signed in to change notification settings - Fork 438
/
worker-script.ts
76 lines (65 loc) · 2.4 KB
/
worker-script.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
import { definePrototypePropertyDescriptor } from '../utils';
import { getInstanceStateValue, setInstanceStateValue } from './worker-state';
import { getter, setter } from './worker-proxy';
import { HTMLSrcElementDescriptorMap } from './worker-src-element';
import { resolveUrl } from './worker-exec';
import { StateProp, WebWorkerEnvironment, WorkerNode } from '../types';
import { webWorkerCtx } from './worker-constants';
export const patchHTMLScriptElement = (WorkerHTMLScriptElement: any, env: WebWorkerEnvironment) => {
const HTMLScriptDescriptorMap: PropertyDescriptorMap & ThisType<WorkerNode> = {
innerHTML: innerHTMLDescriptor,
innerText: innerHTMLDescriptor,
src: {
get() {
return getInstanceStateValue<string>(this, StateProp.url) || '';
},
set(url: string) {
const orgUrl = resolveUrl(env, url, null);
const config = webWorkerCtx.$config$;
url = resolveUrl(env, url, 'script');
setInstanceStateValue(this, StateProp.url, url);
setter(this, ['src'], url);
if (orgUrl !== url) {
setter(this, ['dataset', 'ptsrc'], orgUrl);
}
if (this.type && config.loadScriptsOnMainThread) {
const shouldExecuteScriptViaMainThread = config.loadScriptsOnMainThread.some(
(scriptUrl) => new RegExp(scriptUrl).test(url)
);
if (shouldExecuteScriptViaMainThread) {
setter(this, ['type'], 'text/javascript');
}
}
},
},
textContent: innerHTMLDescriptor,
type: {
get() {
return getter(this, ['type']);
},
set(type: string) {
if (!isScriptJsType(type)) {
setInstanceStateValue(this, StateProp.type, type);
setter(this, ['type'], type);
}
},
},
...HTMLSrcElementDescriptorMap,
};
definePrototypePropertyDescriptor(WorkerHTMLScriptElement, HTMLScriptDescriptorMap);
};
const innerHTMLDescriptor: PropertyDescriptor & ThisType<WorkerNode> = {
get() {
const type = getter(this, ['type']);
if (isScriptJsType(type)) {
return getInstanceStateValue<string>(this, StateProp.innerHTML) || '';
} else {
return getter(this, ['innerHTML']);
}
},
set(scriptContent: string) {
setInstanceStateValue(this, StateProp.innerHTML, scriptContent);
},
};
export const isScriptJsType = (scriptType: string) =>
!scriptType || scriptType === 'text/javascript';