template-replacement 3.3.2 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +8 -0
- package/.oxfmtrc.jsonc +7 -0
- package/.oxlintrc.json +3 -0
- package/README.md +39 -9
- package/core/base.ts +54 -55
- package/core/general.ts +37 -9
- package/core/sign.ts +43 -8
- package/dispatcher/general.ts +2 -1
- package/dispatcher/sign.ts +2 -1
- package/dispatcher/workerGeneral.ts +1 -1
- package/dispatcher/workerSign.ts +1 -1
- package/dist/base-DQz39fXI.js +249 -0
- package/dist/index-oILo_kXG.js +46 -0
- package/dist/main/general.js +1373 -1401
- package/dist/main/sign.js +1559 -1573
- package/dist/replace/general.js +281 -306
- package/dist/replace/sign.js +304 -315
- package/download/index.ts +13 -13
- package/download/stream.ts +29 -28
- package/eslint.config.ts +28 -0
- package/fileSystem/db/index.ts +25 -25
- package/fileSystem/db/indexedDBCache.ts +142 -124
- package/fileSystem/index.ts +5 -8
- package/fileSystem/interface.ts +4 -5
- package/fileSystem/opfs/index.ts +40 -36
- package/helper/index.ts +144 -116
- package/index.ts +9 -17
- package/office/zip.ts +106 -97
- package/package.json +17 -11
- package/replace/base.ts +203 -214
- package/replace/general.ts +44 -19
- package/replace/image.ts +88 -86
- package/replace/interface.ts +29 -24
- package/replace/paramsData.ts +108 -96
- package/replace/sign.ts +79 -43
- package/task/urlDownloadTask.ts +54 -63
- package/temp/index.ts +139 -131
- package/temp/interface.ts +8 -8
- package/tsconfig.json +1 -1
- package/vite.config.ts +11 -14
- package/worker/child/agency.ts +49 -41
- package/worker/child/base.ts +125 -89
- package/worker/child/general.ts +2 -3
- package/worker/child/sign.ts +4 -4
- package/worker/index.ts +52 -51
- package/worker/interface.ts +9 -6
- package/worker/main/general.ts +5 -5
- package/worker/main/index.ts +191 -66
- package/worker/main/sign.ts +5 -5
- package/worker/type.ts +16 -15
- package/dist/assets/template_replacement_core_wasm_bg.wasm +0 -0
- package/dist/assets/template_replacement_sign_core_wasm_bg.wasm +0 -0
- package/dist/base-BuKBOMgk.js +0 -269
- package/dist/general.d.ts +0 -1
- package/dist/index-tFDVIkZX.js +0 -46
- package/dist/sign.d.ts +0 -1
package/worker/child/agency.ts
CHANGED
|
@@ -1,51 +1,56 @@
|
|
|
1
|
-
import image from
|
|
2
|
-
import ReplaceInterface, { media } from
|
|
3
|
-
import paramsData, { mediaData, textData } from
|
|
4
|
-
import Temp, { transmitFileInfo, transmitFileInfoToTemp } from
|
|
1
|
+
import image from '../../replace/image'
|
|
2
|
+
import ReplaceInterface, { media } from '../../replace/interface'
|
|
3
|
+
import paramsData, { mediaData, textData } from '../../replace/paramsData'
|
|
4
|
+
import Temp, { transmitFileInfo, transmitFileInfoToTemp } from '../../temp'
|
|
5
5
|
|
|
6
6
|
export default class implements ReplaceInterface {
|
|
7
|
-
replace: ReplaceInterface
|
|
7
|
+
#replace: ReplaceInterface
|
|
8
8
|
|
|
9
9
|
constructor(replace: ReplaceInterface) {
|
|
10
|
-
this
|
|
10
|
+
this.#replace = replace
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
addTempFile(tempFile:
|
|
14
|
-
tempFile = transmitFileInfoToTemp(tempFile)
|
|
15
|
-
return this
|
|
13
|
+
addTempFile(tempFile: transmitFileInfo | Temp): void {
|
|
14
|
+
tempFile = transmitFileInfoToTemp(tempFile as transmitFileInfo)
|
|
15
|
+
return this.#replace.addTempFile(tempFile)
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
clear() {
|
|
19
|
-
return this
|
|
19
|
+
return this.#replace.clear()
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
//提取变量
|
|
23
|
-
extractVariables(
|
|
23
|
+
extractVariables(
|
|
24
|
+
files: Temp[] | transmitFileInfo[] | undefined,
|
|
25
|
+
): Promise<Record<string, string[]>> {
|
|
24
26
|
if (files) {
|
|
25
27
|
for (const i in files) {
|
|
26
28
|
files[i] = transmitFileInfoToTemp(files[i] as transmitFileInfo)
|
|
27
29
|
}
|
|
28
30
|
}
|
|
29
|
-
return this
|
|
31
|
+
return this.#replace.extractVariables(files as Temp[])
|
|
30
32
|
}
|
|
31
33
|
|
|
32
34
|
//提取媒体
|
|
33
|
-
extractMedias(files: Temp[] | undefined): Promise<Record<string, media[]>> {
|
|
35
|
+
extractMedias(files: Temp[] | transmitFileInfo[] | undefined): Promise<Record<string, media[]>> {
|
|
34
36
|
if (files) {
|
|
35
37
|
for (const i in files) {
|
|
36
38
|
files[i] = transmitFileInfoToTemp(files[i] as transmitFileInfo)
|
|
37
39
|
}
|
|
38
40
|
}
|
|
39
|
-
return this
|
|
41
|
+
return this.#replace.extractMedias(files as Temp[])
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
//签名方法
|
|
43
|
-
sign(data:
|
|
44
|
-
return this
|
|
45
|
+
sign(data: unknown): Promise<string> {
|
|
46
|
+
return this.#replace.sign(data)
|
|
45
47
|
}
|
|
46
48
|
|
|
47
49
|
//执行替换任务
|
|
48
|
-
execute(
|
|
50
|
+
execute(
|
|
51
|
+
params: paramsData,
|
|
52
|
+
files: Temp[] | transmitFileInfo[] | undefined,
|
|
53
|
+
): Promise<Record<string, Uint8Array>> {
|
|
49
54
|
if (files) {
|
|
50
55
|
for (const i in files) {
|
|
51
56
|
files[i] = transmitFileInfoToTemp(files[i] as transmitFileInfo)
|
|
@@ -53,70 +58,73 @@ export default class implements ReplaceInterface {
|
|
|
53
58
|
}
|
|
54
59
|
const text: textData = {}
|
|
55
60
|
for (const key in params.textData) {
|
|
56
|
-
const value = params.textData[key]
|
|
57
|
-
if (value && value.file instanceof Blob) {
|
|
58
|
-
text[key] = new image(value.file)
|
|
59
|
-
delete value.file
|
|
60
|
-
text[key].
|
|
61
|
-
}else {
|
|
61
|
+
const value = params.textData[key]
|
|
62
|
+
if (value && (value as image).file instanceof Blob) {
|
|
63
|
+
text[key] = new image((value as image).file)
|
|
64
|
+
delete (value as Record<keyof image, unknown>).file
|
|
65
|
+
text[key].setProperties((value as Record<keyof image, unknown>))
|
|
66
|
+
} else {
|
|
62
67
|
text[key] = value
|
|
63
68
|
}
|
|
64
69
|
}
|
|
65
70
|
|
|
66
71
|
const media: mediaData = {}
|
|
67
72
|
for (const key in params.mediaData) {
|
|
68
|
-
const value = params.mediaData[key] as Record<keyof image,
|
|
73
|
+
const value = params.mediaData[key] as Record<keyof image, unknown>
|
|
69
74
|
if (value && value.file instanceof Blob) {
|
|
70
75
|
media[key] = new image(value.file)
|
|
71
76
|
delete value.file
|
|
72
|
-
media[key].
|
|
77
|
+
media[key].setProperties(value)
|
|
73
78
|
}
|
|
74
79
|
}
|
|
75
|
-
return this
|
|
80
|
+
return this.#replace.execute(new paramsData(text, media), files as Temp[])
|
|
76
81
|
}
|
|
77
82
|
|
|
78
83
|
//执行替换任务(多套参数)
|
|
79
|
-
executeMultipleParams(
|
|
84
|
+
executeMultipleParams(
|
|
85
|
+
paramsMultiple: paramsData[],
|
|
86
|
+
files: Temp[] | undefined,
|
|
87
|
+
): Promise<Record<string, Uint8Array>[]> {
|
|
80
88
|
if (files) {
|
|
81
89
|
for (const i in files) {
|
|
82
90
|
files[i] = transmitFileInfoToTemp(files[i] as transmitFileInfo)
|
|
83
91
|
}
|
|
84
92
|
}
|
|
85
|
-
const paramsList= []
|
|
93
|
+
const paramsList = []
|
|
86
94
|
for (const params of paramsMultiple) {
|
|
87
95
|
const text: textData = {}
|
|
88
96
|
for (const key in params.textData) {
|
|
89
|
-
const value = params.textData[key]
|
|
90
|
-
if (value && value.file instanceof Blob) {
|
|
91
|
-
text[key] = new image(value.file)
|
|
92
|
-
delete value.file
|
|
93
|
-
text[key].
|
|
94
|
-
}else {
|
|
97
|
+
const value = params.textData[key]
|
|
98
|
+
if (value && (value as image).file instanceof Blob) {
|
|
99
|
+
text[key] = new image((value as image).file)
|
|
100
|
+
delete (value as Record<keyof image, unknown>).file
|
|
101
|
+
text[key].setProperties((value as Record<keyof image, unknown>))
|
|
102
|
+
} else {
|
|
95
103
|
text[key] = value
|
|
96
104
|
}
|
|
97
105
|
}
|
|
98
106
|
|
|
99
107
|
const media: mediaData = {}
|
|
100
108
|
for (const key in params.mediaData) {
|
|
101
|
-
const value = params.mediaData[key] as Record<keyof image,
|
|
109
|
+
const value = params.mediaData[key] as Record<keyof image, unknown>
|
|
102
110
|
if (value && value.file instanceof Blob) {
|
|
103
111
|
media[key] = new image(value.file)
|
|
104
112
|
delete value.file
|
|
105
|
-
media[key].
|
|
113
|
+
media[key].setProperties(value)
|
|
106
114
|
}
|
|
107
115
|
}
|
|
108
116
|
paramsList.push(new paramsData(text, media))
|
|
109
117
|
}
|
|
110
|
-
return this
|
|
118
|
+
return this.#replace.executeMultipleParams(paramsList, files)
|
|
111
119
|
}
|
|
112
120
|
|
|
113
121
|
//文件加密
|
|
114
122
|
fileEncrypt(file: Uint8Array): Promise<Uint8Array> {
|
|
115
|
-
return this
|
|
123
|
+
return this.#replace.fileEncrypt(file)
|
|
116
124
|
}
|
|
117
125
|
|
|
118
126
|
//文件批量加密
|
|
119
|
-
filesEncrypt(files:
|
|
120
|
-
return this
|
|
127
|
+
filesEncrypt(files: Uint8Array[]): Promise<Uint8Array[]> {
|
|
128
|
+
return this.#replace.filesEncrypt(files)
|
|
121
129
|
}
|
|
122
|
-
}
|
|
130
|
+
}
|
package/worker/child/base.ts
CHANGED
|
@@ -1,114 +1,150 @@
|
|
|
1
1
|
import agency from './agency'
|
|
2
|
-
import { messageData, messageTypes, methodCall } from '../type'
|
|
3
|
-
import ReplaceInterface, { media } from '../../replace/interface'
|
|
4
|
-
import { generateId } from '../../helper'
|
|
2
|
+
import { messageData, messageTypes, methodCall, methodCallReply } from '../type'
|
|
3
|
+
import ReplaceInterface, { media } from '../../replace/interface'
|
|
4
|
+
import { generateId } from '../../helper'
|
|
5
5
|
|
|
6
6
|
type methodKeys<T> = {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
8
|
+
[K in keyof T]: T[K] extends Function ? K : never
|
|
9
|
+
}[keyof T]
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
extractVariables: true,
|
|
13
|
-
extractMedias: true,
|
|
14
|
-
execute: true,
|
|
15
|
-
filesEncrypt: true,
|
|
16
|
-
fileEncrypt: true,
|
|
17
|
-
executeMultipleParams: true,
|
|
18
|
-
}
|
|
11
|
+
//允许调用的方法
|
|
12
|
+
const allowCallMethodNames: Set<methodKeys<ReplaceInterface>> = new Set(["addTempFile", "extractVariables", "extractMedias", "execute", "filesEncrypt", "fileEncrypt", "executeMultipleParams"])
|
|
19
13
|
|
|
20
|
-
|
|
14
|
+
//处理方法调用的返回值
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
|
+
const resultHandles = new Map<methodKeys<ReplaceInterface>, (result: any, transfer: Transferable[]) => Transferable[]>([
|
|
17
|
+
['execute', (result: Record<string, Uint8Array>, transfer: Transferable[] = []) => {
|
|
18
|
+
for (const key in result) {
|
|
19
|
+
const value = result[key]
|
|
20
|
+
if (value?.length) {
|
|
21
|
+
transfer.push(value.buffer)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return transfer
|
|
25
|
+
}],
|
|
26
|
+
['executeMultipleParams', (result: Record<string, Uint8Array>[], transfer: Transferable[] = []) => {
|
|
27
|
+
for (const map of result) {
|
|
28
|
+
for (const key in map) {
|
|
29
|
+
const value = map[key]
|
|
30
|
+
if (value?.length) {
|
|
31
|
+
transfer.push(value.buffer)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return transfer
|
|
36
|
+
}],
|
|
37
|
+
['extractMedias', (result: Record<string, media[]>, transfer: Transferable[] = []) => {
|
|
38
|
+
for (const key in result) {
|
|
39
|
+
const medias = result[key]
|
|
40
|
+
for (const media of medias) {
|
|
41
|
+
if (media.data?.length) {
|
|
42
|
+
transfer.push(media.data.buffer)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return transfer
|
|
47
|
+
}],
|
|
48
|
+
['fileEncrypt', (result: Uint8Array, transfer: Transferable[] = []) => {
|
|
49
|
+
if (result?.length) {
|
|
50
|
+
transfer.push(result.buffer)
|
|
51
|
+
}
|
|
52
|
+
return transfer
|
|
53
|
+
}],
|
|
54
|
+
['filesEncrypt', (result: Uint8Array[], transfer: Transferable[] = []) => {
|
|
55
|
+
for (const item of result) {
|
|
56
|
+
if (item?.length) {
|
|
57
|
+
transfer.push(item.buffer)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return transfer
|
|
61
|
+
}],
|
|
62
|
+
])
|
|
63
|
+
|
|
64
|
+
const tasks = new Map<string, (value: unknown) => void>()
|
|
21
65
|
|
|
22
66
|
let dispatch: ReplaceInterface
|
|
23
67
|
|
|
24
68
|
export default function _init(replace: ReplaceInterface) {
|
|
25
|
-
|
|
69
|
+
dispatch = new agency(replace)
|
|
26
70
|
}
|
|
27
71
|
|
|
28
|
-
export
|
|
72
|
+
export function call<T>(method: string, ...params: unknown[]): Promise<T> {
|
|
29
73
|
const replyId = generateId()
|
|
30
74
|
postMessage({
|
|
31
75
|
type: messageTypes.methodCall,
|
|
32
76
|
data: {
|
|
33
77
|
replyId,
|
|
34
78
|
method: method,
|
|
35
|
-
params: params
|
|
36
|
-
}
|
|
79
|
+
params: params,
|
|
80
|
+
},
|
|
37
81
|
})
|
|
38
82
|
|
|
39
83
|
return new Promise<T>((resolve, reject) => {
|
|
40
|
-
tasks.set(replyId, resolve)
|
|
84
|
+
tasks.set(replyId, resolve as (value: unknown) => void)
|
|
41
85
|
})
|
|
42
86
|
}
|
|
43
87
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
88
|
+
addEventListener('message', async (event) => {
|
|
89
|
+
const data = event.data as messageData
|
|
90
|
+
if (!data?.data) {
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
switch (data.type) {
|
|
94
|
+
case messageTypes.methodCall:
|
|
95
|
+
// 调用方法
|
|
96
|
+
const callData = data.data as methodCall<methodKeys<ReplaceInterface>>
|
|
97
|
+
const method = callData.method
|
|
98
|
+
if (!allowCallMethodNames.has(method)) {
|
|
99
|
+
//跳过不允许被调用的方法
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
103
|
+
const fun = dispatch[method] as Function | undefined
|
|
104
|
+
if (!fun) {
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
let result: unknown
|
|
108
|
+
let error: unknown = undefined
|
|
109
|
+
try {
|
|
110
|
+
result = await Promise.resolve(fun.apply(dispatch, callData.params))
|
|
111
|
+
} catch (e) {
|
|
112
|
+
console.error(e)
|
|
113
|
+
error = (e as Error).message || ((e as Error).name || "error")
|
|
114
|
+
}
|
|
115
|
+
if (!callData.replyId) {
|
|
116
|
+
return
|
|
117
|
+
}
|
|
118
|
+
const transfer: Transferable[] = []
|
|
119
|
+
if (result) {
|
|
120
|
+
const resultHandle = resultHandles.get(method)
|
|
121
|
+
if (resultHandle) {
|
|
122
|
+
resultHandle(result, transfer)
|
|
58
123
|
}
|
|
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
|
-
break;
|
|
86
|
-
case 'fileEncrypt':
|
|
87
|
-
(res as Uint8Array).length && transfer.push((res as Uint8Array).buffer)
|
|
88
|
-
break;
|
|
89
|
-
case 'filesEncrypt':
|
|
90
|
-
for (const item of (res as Uint8Array[])) {
|
|
91
|
-
transfer.push(item.buffer)
|
|
92
|
-
}
|
|
93
|
-
break;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
postMessage({
|
|
97
|
-
type: messageTypes.methodCallReply,
|
|
98
|
-
data: {
|
|
99
|
-
replyId: callData.replyId,
|
|
100
|
-
result: res
|
|
101
|
-
}
|
|
102
|
-
}, transfer.length ? { transfer } : undefined)
|
|
103
|
-
}
|
|
104
|
-
break;
|
|
105
|
-
case messageTypes.methodCallReply:
|
|
106
|
-
// 方法调用的返回数据
|
|
107
|
-
const fn = tasks.get(data?.data?.replyId)
|
|
108
|
-
if (!fn) {
|
|
109
|
-
return
|
|
110
|
-
}
|
|
111
|
-
fn(data?.data?.result)
|
|
112
|
-
tasks.delete(data?.data?.replyId)
|
|
113
|
-
}
|
|
124
|
+
}
|
|
125
|
+
postMessage(
|
|
126
|
+
{
|
|
127
|
+
type: messageTypes.methodCallReply,
|
|
128
|
+
data: {
|
|
129
|
+
replyId: callData.replyId,
|
|
130
|
+
result,
|
|
131
|
+
error
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
transfer.length ? { transfer } : undefined,
|
|
135
|
+
)
|
|
136
|
+
break
|
|
137
|
+
case messageTypes.methodCallReply:
|
|
138
|
+
// 方法调用的返回数据
|
|
139
|
+
const replyId = (data.data as methodCallReply).replyId
|
|
140
|
+
if (!replyId) {
|
|
141
|
+
return
|
|
142
|
+
}
|
|
143
|
+
const fn = tasks.get(replyId)
|
|
144
|
+
if (!fn) {
|
|
145
|
+
return
|
|
146
|
+
}
|
|
147
|
+
fn((data.data as methodCallReply).result)
|
|
148
|
+
tasks.delete(replyId)
|
|
149
|
+
}
|
|
114
150
|
})
|
package/worker/child/general.ts
CHANGED
package/worker/child/sign.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import Replace from '../../replace/sign'
|
|
2
|
-
import init, { call } from './base'
|
|
1
|
+
import Replace from '../../replace/sign'
|
|
2
|
+
import init, { call } from './base'
|
|
3
3
|
|
|
4
4
|
const replace = new Replace()
|
|
5
5
|
init(replace)
|
|
6
6
|
|
|
7
|
-
replace.sign =
|
|
8
|
-
return
|
|
7
|
+
replace.sign = (data: unknown): Promise<string> => {
|
|
8
|
+
return call<string>('sign', data)
|
|
9
9
|
}
|
package/worker/index.ts
CHANGED
|
@@ -1,66 +1,67 @@
|
|
|
1
|
-
import { messageData } from
|
|
2
|
-
import DispatcherInterface from
|
|
1
|
+
import { messageData } from './type'
|
|
2
|
+
import DispatcherInterface from './interface'
|
|
3
3
|
|
|
4
4
|
export type webworker = new () => Worker
|
|
5
5
|
|
|
6
6
|
export default class worker implements DispatcherInterface {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
#concurrency: number
|
|
8
|
+
#counter: number = 0
|
|
9
|
+
#workers: Worker[] = []
|
|
10
|
+
#listenerList: ((event: MessageEvent) => void)[] = []
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
if (!this.#concurrency || this.#concurrency < 1) {
|
|
21
|
-
this.#concurrency = 1
|
|
22
|
-
}
|
|
23
|
-
for (let index = 0; index < this.#concurrency; index++) {
|
|
24
|
-
this.#addOneWorker(webworker)
|
|
25
|
-
}
|
|
12
|
+
constructor(webworker: webworker, concurrency?: number) {
|
|
13
|
+
this.#concurrency = Number(concurrency)
|
|
14
|
+
if (this.#concurrency < 1) {
|
|
15
|
+
try {
|
|
16
|
+
this.#concurrency = navigator.hardwareConcurrency < 8 ? navigator.hardwareConcurrency : 8
|
|
17
|
+
} catch (error) { }
|
|
26
18
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const worker: Worker = new webworker()
|
|
30
|
-
worker.onmessage = async (event: MessageEvent) => {
|
|
31
|
-
const tasks = []
|
|
32
|
-
for (const fun of this.#listenerList) {
|
|
33
|
-
tasks.push(fun(event))
|
|
34
|
-
}
|
|
35
|
-
const res = await Promise.all(tasks)
|
|
36
|
-
for (const reply of res) {
|
|
37
|
-
reply as any && worker.postMessage(reply)
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
this.#workers.push(worker)
|
|
19
|
+
if (this.#concurrency < 1) {
|
|
20
|
+
this.#concurrency = 1
|
|
41
21
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return this.#concurrency
|
|
22
|
+
for (let index = 0; index < this.#concurrency; index++) {
|
|
23
|
+
this.#addOneWorker(webworker)
|
|
45
24
|
}
|
|
25
|
+
}
|
|
46
26
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
27
|
+
#addOneWorker(webworker: webworker) {
|
|
28
|
+
const worker: Worker = new webworker()
|
|
29
|
+
worker.onmessage = async (event: MessageEvent) => {
|
|
30
|
+
const tasks: unknown[] = []
|
|
31
|
+
for (const fun of this.#listenerList) {
|
|
32
|
+
tasks.push(fun(event))
|
|
33
|
+
}
|
|
34
|
+
const res = await Promise.all(tasks)
|
|
35
|
+
for (const reply of res) {
|
|
36
|
+
if (reply) {
|
|
37
|
+
worker.postMessage(reply)
|
|
50
38
|
}
|
|
51
|
-
|
|
39
|
+
}
|
|
52
40
|
}
|
|
41
|
+
this.#workers.push(worker)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
concurrency(): number {
|
|
45
|
+
return this.#concurrency
|
|
46
|
+
}
|
|
53
47
|
|
|
54
|
-
|
|
55
|
-
|
|
48
|
+
postMessage(message: messageData, options?: StructuredSerializeOptions) {
|
|
49
|
+
if (!this.#workers[++this.#counter]) {
|
|
50
|
+
this.#counter = 0
|
|
56
51
|
}
|
|
52
|
+
this.#workers[this.#counter].postMessage(message, options)
|
|
53
|
+
}
|
|
57
54
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
55
|
+
addListener(fun: (event: MessageEvent) => void) {
|
|
56
|
+
this.#listenerList.push(fun)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
removeListener(fun: (event: MessageEvent) => void) {
|
|
60
|
+
for (const i in this.#listenerList) {
|
|
61
|
+
if (this.#listenerList[i] == fun) {
|
|
62
|
+
this.#listenerList.splice(i as unknown as number, 1)
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
65
|
}
|
|
66
|
-
}
|
|
66
|
+
}
|
|
67
|
+
}
|
package/worker/interface.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import { messageData, methodCall } from
|
|
1
|
+
import { messageData, methodCall } from './type'
|
|
2
2
|
|
|
3
3
|
export default interface DispatcherInterface {
|
|
4
|
-
|
|
4
|
+
concurrency(): number
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
postMessage(
|
|
7
|
+
message: messageData | methodCall,
|
|
8
|
+
options?: StructuredSerializeOptions,
|
|
9
|
+
): void
|
|
7
10
|
|
|
8
|
-
|
|
11
|
+
addListener<T>(fun: (event: MessageEvent<T>) => void): void
|
|
9
12
|
|
|
10
|
-
|
|
11
|
-
}
|
|
13
|
+
removeListener<T>(fun: (event: MessageEvent<T>) => void): void
|
|
14
|
+
}
|
package/worker/main/general.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import webworker from
|
|
2
|
-
import base from
|
|
1
|
+
import webworker from '../child/general.ts?worker&inline'
|
|
2
|
+
import base from '../index'
|
|
3
3
|
|
|
4
4
|
export default class extends base {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
constructor(concurrency?: number) {
|
|
6
|
+
super(webworker, concurrency)
|
|
7
|
+
}
|
|
8
8
|
}
|