diff --git a/examples/worker_new/index.js b/examples/worker_new/index.js index 67646a1ec..b95fbbad6 100644 --- a/examples/worker_new/index.js +++ b/examples/worker_new/index.js @@ -42,7 +42,7 @@ const HeartBeatCheckAction = BI.inherit(BI.Workers.WorkerBaseAction, { } }); -var WorkerThreadWorker = BI.inherit(BI.Workers.MainThreadWorker, { +var MainThreadWorker = BI.inherit(BI.Workers.MainThreadWorker, { initActions: function() { this.cookieAction = this.createAction(CookieAction); @@ -64,7 +64,7 @@ var WorkerThreadWorker = BI.inherit(BI.Workers.MainThreadWorker, { } }); -var mainThreadWorker = new WorkerThreadWorker({ +var mainThreadWorker = BI.Workers.createWorker(MainThreadWorker, { workerUrl: "./worker_new/worker.js", workerName: "demo" }); diff --git a/examples/worker_new/worker.js b/examples/worker_new/worker.js index f30856b21..96e88e542 100644 --- a/examples/worker_new/worker.js +++ b/examples/worker_new/worker.js @@ -58,7 +58,7 @@ const HeartBeatCheckAction = BI.inherit(BI.Workers.WorkerBaseAction, { } }); -var WorkerThreadWorker = BI.inherit(BI.Workers.WorkerThreadWorker, { +var MainThreadWorker = BI.inherit(BI.Workers.WorkerThreadWorker, { initActions: function() { this.cookieAction = this.createAction(CookieAction); @@ -75,6 +75,6 @@ var WorkerThreadWorker = BI.inherit(BI.Workers.WorkerThreadWorker, { } }); -var workerThreadWorker = new WorkerThreadWorker(); +var workerThreadWorker = BI.Workers.createWorker(MainThreadWorker); workerThreadWorker.fetchCookie(); diff --git a/typescript/core/worker/worker.main_thread.ts b/typescript/core/worker/worker.main_thread.ts index 42e0c18dd..c87bc9833 100644 --- a/typescript/core/worker/worker.main_thread.ts +++ b/typescript/core/worker/worker.main_thread.ts @@ -5,7 +5,7 @@ import { IWorkerOptions } from "./worker.core"; /** * 主线程Worker */ -export abstract class MainThreadWorker { +export class MainThreadWorker { /** * Worker 名称 */ @@ -24,10 +24,12 @@ export abstract class MainThreadWorker { public constructor(options: IWorkerOptions) { this.name = options.workerName; this.controller = new WorkerMainThreadController(options); - this.initActions(); } - protected abstract initActions(): void; + /** + * 初始化业务actions + */ + public initActions() {} /** * 销毁 worker 实例 diff --git a/typescript/core/worker/worker.worker_thread.ts b/typescript/core/worker/worker.worker_thread.ts index 9907955fb..53b621403 100644 --- a/typescript/core/worker/worker.worker_thread.ts +++ b/typescript/core/worker/worker.worker_thread.ts @@ -4,7 +4,7 @@ import { WorkerThreadController } from "./controller/worker.worker_thread.contro /** * worker线程实例 */ -export abstract class WorkerThreadWorker { +export class WorkerThreadWorker { /** * Worker 线程通信控制器 */ @@ -12,11 +12,12 @@ export abstract class WorkerThreadWorker { public constructor() { this.controller = new WorkerThreadController(); - - this.initActions(); } - protected abstract initActions(): void; + /** + * 初始化业务actions + */ + public initActions() {} /** * 实例化action diff --git a/typescript/core/worker/workers.ts b/typescript/core/worker/workers.ts index f02447120..5ab99dddc 100644 --- a/typescript/core/worker/workers.ts +++ b/typescript/core/worker/workers.ts @@ -1,12 +1,22 @@ import { WorkerChannel } from "./worker.channel"; import { WorkerBaseController } from "./controller/worker.controller"; -import { WorkerMessageType } from "./worker.core"; +import { IWorkerOptions, 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 { MainThreadWorker } from "./worker.main_thread"; import { WorkerThreadWorker } from "./worker.worker_thread"; +function createWorker(ThreadWorker: T, options: IWorkerOptions): InstanceType +function createWorker(ThreadWorker: T): InstanceType +function createWorker(ThreadWorker: T, options?: IWorkerOptions): InstanceType { + const threadWorker = new ThreadWorker(options as any) as InstanceType; + + threadWorker.initActions(); + + return threadWorker; +} + export const Workers = { WorkerChannel, WorkerBaseController, @@ -16,4 +26,5 @@ export const Workers = { MainThreadWorker, WorkerThreadWorker, WorkerMessageType, + createWorker, };