From 2548a525dd6b1ef4436744a880433688f71058d3 Mon Sep 17 00:00:00 2001 From: iapyang Date: Thu, 30 Jun 2022 14:02:47 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=B0=83=E6=95=B4=E4=B8=8B?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typescript/core/worker/action/worker.action.ts | 4 ++-- typescript/core/worker/worker.main_thread.ts | 9 +++++++++ typescript/core/worker/worker.worker_thread.ts | 11 +++++++++++ typescript/core/worker/workers.ts | 6 +++++- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/typescript/core/worker/action/worker.action.ts b/typescript/core/worker/action/worker.action.ts index d588e6ab4..8f6920003 100644 --- a/typescript/core/worker/action/worker.action.ts +++ b/typescript/core/worker/action/worker.action.ts @@ -3,7 +3,7 @@ import type { WorkerBaseController } from "../controller/worker.controller"; /** * 事务的基类 */ -export default abstract class WorkerBaseAction { +export class WorkerBaseAction { /** * 通信控制器 */ @@ -24,5 +24,5 @@ export default abstract class WorkerBaseAction { /** * 添加事务的处理器 */ - protected abstract addActionHandler(): void; + protected addActionHandler() {} } diff --git a/typescript/core/worker/worker.main_thread.ts b/typescript/core/worker/worker.main_thread.ts index 3140bbbe7..42e0c18dd 100644 --- a/typescript/core/worker/worker.main_thread.ts +++ b/typescript/core/worker/worker.main_thread.ts @@ -1,3 +1,4 @@ +import type { WorkerBaseAction } from "./action/worker.action"; import { WorkerMainThreadController } from "./controller/worker.main_thread.controller"; import { IWorkerOptions } from "./worker.core"; @@ -37,4 +38,12 @@ export abstract class MainThreadWorker { // 设置终止标志位 this.isTerminated = true; } + + /** + * 实例化action + * @param Action action类 + */ + protected createAction(Action: T): InstanceType { + return (new Action(this.controller, this)) as InstanceType; + } } diff --git a/typescript/core/worker/worker.worker_thread.ts b/typescript/core/worker/worker.worker_thread.ts index 28e5cd64c..9907955fb 100644 --- a/typescript/core/worker/worker.worker_thread.ts +++ b/typescript/core/worker/worker.worker_thread.ts @@ -1,3 +1,4 @@ +import type { WorkerBaseAction } from "./action/worker.action"; import { WorkerThreadController } from "./controller/worker.worker_thread.controller"; /** @@ -11,7 +12,17 @@ export abstract class WorkerThreadWorker { public constructor() { this.controller = new WorkerThreadController(); + + this.initActions(); } protected abstract initActions(): void; + + /** + * 实例化action + * @param Action action类 + */ + protected createAction(Action: T): InstanceType { + return (new Action(this.controller, this)) as InstanceType; + } } diff --git a/typescript/core/worker/workers.ts b/typescript/core/worker/workers.ts index 55853f363..f02447120 100644 --- a/typescript/core/worker/workers.ts +++ b/typescript/core/worker/workers.ts @@ -3,7 +3,9 @@ import { WorkerBaseController } from "./controller/worker.controller"; import { WorkerMessageType } from "./worker.core"; import { WorkerMainThreadController } from "./controller/worker.main_thread.controller"; import { WorkerThreadController } from "./controller/worker.worker_thread.controller"; -import WorkerBaseAction from "./action/worker.action"; +import { WorkerBaseAction } from "./action/worker.action"; +import { MainThreadWorker } from "./worker.main_thread"; +import { WorkerThreadWorker } from "./worker.worker_thread"; export const Workers = { WorkerChannel, @@ -11,5 +13,7 @@ export const Workers = { WorkerMainThreadController, WorkerThreadController, WorkerBaseAction, + MainThreadWorker, + WorkerThreadWorker, WorkerMessageType, };