From 9a5d7694ec079bd753097d3212801c761dc3d061 Mon Sep 17 00:00:00 2001 From: iapyang Date: Tue, 28 Jun 2022 20:01:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=BB=E6=AC=A1=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E5=85=A5=E5=8F=A3abstract=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typescript/core/worker/worker.main_thread.ts | 40 +++++++++++++++++++ .../core/worker/worker.worker_thread.ts | 17 ++++++++ 2 files changed, 57 insertions(+) create mode 100644 typescript/core/worker/worker.main_thread.ts create mode 100644 typescript/core/worker/worker.worker_thread.ts diff --git a/typescript/core/worker/worker.main_thread.ts b/typescript/core/worker/worker.main_thread.ts new file mode 100644 index 000000000..3140bbbe7 --- /dev/null +++ b/typescript/core/worker/worker.main_thread.ts @@ -0,0 +1,40 @@ +import { WorkerMainThreadController } from "./controller/worker.main_thread.controller"; +import { IWorkerOptions } from "./worker.core"; + +/** + * 主线程Worker + */ +export abstract class MainThreadWorker { + /** + * Worker 名称 + */ + public name: string; + + /** + * 主线程通信控制器 + */ + public controller: WorkerMainThreadController; + + /** + * 是否已经终止掉 Worker + */ + protected isTerminated = false; + + public constructor(options: IWorkerOptions) { + this.name = options.workerName; + this.controller = new WorkerMainThreadController(options); + this.initActions(); + } + + protected abstract initActions(): void; + + /** + * 销毁 worker 实例 + * 子实例需要销毁action + */ + public terminate(): void { + this.controller.terminate(); + // 设置终止标志位 + this.isTerminated = true; + } +} diff --git a/typescript/core/worker/worker.worker_thread.ts b/typescript/core/worker/worker.worker_thread.ts new file mode 100644 index 000000000..28e5cd64c --- /dev/null +++ b/typescript/core/worker/worker.worker_thread.ts @@ -0,0 +1,17 @@ +import { WorkerThreadController } from "./controller/worker.worker_thread.controller"; + +/** + * worker线程实例 + */ +export abstract class WorkerThreadWorker { + /** + * Worker 线程通信控制器 + */ + protected controller: WorkerThreadController; + + public constructor() { + this.controller = new WorkerThreadController(); + } + + protected abstract initActions(): void; +}