-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
102 additions
and
25 deletions.
There are no files selected for viewing
26 changes: 21 additions & 5 deletions
26
apps/web-visualization-demos/src/page/cesiumPage/worker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,27 @@ | ||
import { WorkerHelper } from "@zxtool/utils" | ||
import { WorkerFn, WorkerHelper } from "@zxtool/utils" | ||
|
||
function work() { | ||
onmessage = ({ data: { key, message } }) => { | ||
postMessage({ key, result: `result is ${message}` }) | ||
const work: WorkerFn<string> = getWorkerUtil => { | ||
onmessage = e => { | ||
const { getMsg, postMsg, throwError } = getWorkerUtil(e) | ||
const message = getMsg() | ||
if (message === "hello 2") throwError(`error is ${message}`) | ||
else postMsg(`result is ${message}`) | ||
} | ||
} | ||
|
||
const workerHelper = new WorkerHelper(work) | ||
|
||
workerHelper.postMessage("hello world").then(console.log) | ||
workerHelper | ||
.postMessage<string>("hello 1") | ||
.then(res => { | ||
console.log(res) | ||
}) | ||
.catch(err => { | ||
console.error(err) | ||
}) | ||
workerHelper.postMessage("hello 2").then(console.log).catch(console.error) | ||
workerHelper.postMessage("hello 3").then(console.log).catch(console.error) | ||
setTimeout(() => { | ||
workerHelper.terminate() | ||
workerHelper.postMessage("hello 4").then(console.log).catch(console.error) | ||
}, 1000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 61 additions & 14 deletions
75
packages/zxtool-utils/src/widget/WorkerHelper/WorkerHelper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,76 @@ | ||
// https://zhuanlan.zhihu.com/p/83001302 | ||
|
||
import { nanoid } from "nanoid" | ||
import { genZUInfo } from "../../util" | ||
import { CommonUtil } from "../CommonUtil/CommonUtil" | ||
|
||
const genInfo = genZUInfo("WorkerHelper") | ||
|
||
function getWorkerUtil<M, R>(e: MessageEvent<{ key: number; message: M }>) { | ||
const { key, message } = e.data | ||
return { | ||
getMsg() { | ||
return message | ||
}, | ||
postMsg(message: R) { | ||
postMessage({ key, message }) | ||
}, | ||
throwError(message: R) { | ||
throw new Error(JSON.stringify({ key, message })) | ||
}, | ||
} | ||
} | ||
|
||
export type GetWorkerUtilType<M = unknown, R = unknown> = (e: MessageEvent<{ key: number; message: M }>) => { | ||
getMsg(): M | ||
postMsg(message: R): void | ||
throwError(message: R): never | ||
} | ||
|
||
export type WorkerFn<T = unknown> = (getWorkerUtil: GetWorkerUtilType<T>) => void | ||
|
||
export class WorkerHelper { | ||
worker: Worker | ||
jobMap: Map<PropertyKey, Function> = new Map() | ||
private worker: Worker | null = null | ||
private jobMap: Map<PropertyKey, Function[]> = new Map() | ||
private key = 0 | ||
|
||
static fn2url(fn: Function) { | ||
return URL.createObjectURL(new Blob([`(${fn.toString()})()`])) | ||
static fn2url(fn: WorkerFn) { | ||
return URL.createObjectURL(new Blob([`(${fn.toString()})(${getWorkerUtil.toString()})`])) | ||
} | ||
|
||
constructor(fn: Function) { | ||
constructor(fn: WorkerFn<any>) { | ||
this.worker = new Worker(WorkerHelper.fn2url(fn)) | ||
|
||
this.worker.onmessage = ({ data: { result, key } }) => { | ||
this.jobMap.get(key)?.(result) | ||
this.jobMap.delete(key) | ||
this.worker.onmessage = ({ data }) => { | ||
if ("message" in data && "key" in data) { | ||
const { message, key } = data | ||
this.jobMap.get(key)?.[0]?.(message) | ||
this.jobMap.delete(key) | ||
} | ||
} | ||
|
||
this.worker.onerror = e => { | ||
e.preventDefault() | ||
const data = CommonUtil.parseJson<{ key: number; message: unknown }>(e.message.replace("Uncaught Error: ", "")) | ||
if (data && "message" in data && "key" in data) { | ||
const { message, key } = data | ||
this.jobMap.get(key)?.[1]?.(message) | ||
this.jobMap.delete(key) | ||
} | ||
} | ||
} | ||
|
||
postMessage(message: any) { | ||
return new Promise(resolve => { | ||
const key = nanoid() | ||
this.jobMap.set(key, resolve) | ||
this.worker.postMessage({ key, message }) | ||
postMessage<T>(message: unknown) { | ||
if (!this.worker) throw new Error(genInfo("worker 没有初始化或者已经销毁")) | ||
|
||
return new Promise<T>((resolve, reject) => { | ||
const key = this.key++ | ||
this.jobMap.set(key, [resolve, reject]) | ||
this.worker!.postMessage({ key, message }) | ||
}) | ||
} | ||
|
||
terminate() { | ||
this.worker?.terminate() | ||
this.worker = null | ||
} | ||
} |