Browse Source

KERNEL-12095 fix: 封装new部分的代码

es6
iapyang 2 years ago
parent
commit
f4f0fdc27c
  1. 4
      examples/worker_new/index.js
  2. 4
      examples/worker_new/worker.js
  3. 8
      typescript/core/worker/worker.main_thread.ts
  4. 9
      typescript/core/worker/worker.worker_thread.ts
  5. 13
      typescript/core/worker/workers.ts

4
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"
});

4
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();

8
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

9
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

13
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<T extends typeof MainThreadWorker>(ThreadWorker: T, options: IWorkerOptions): InstanceType<T>
function createWorker<T extends typeof WorkerThreadWorker>(ThreadWorker: T): InstanceType<T>
function createWorker<T extends typeof MainThreadWorker | typeof WorkerThreadWorker>(ThreadWorker: T, options?: IWorkerOptions): InstanceType<T> {
const threadWorker = new ThreadWorker(options as any) as InstanceType<T>;
threadWorker.initActions();
return threadWorker;
}
export const Workers = {
WorkerChannel,
WorkerBaseController,
@ -16,4 +26,5 @@ export const Workers = {
MainThreadWorker,
WorkerThreadWorker,
WorkerMessageType,
createWorker,
};

Loading…
Cancel
Save