From 0038b76320079328d69d27e34e87981d414d05af Mon Sep 17 00:00:00 2001 From: iapyang Date: Fri, 22 Jul 2022 14:44:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E4=B8=8B=E8=84=9A?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/cli/worker/cli.worker.js | 29 ++++++++++++++----- bin/cli/worker/template/action_type.t | 0 bin/cli/worker/template/main_thread.t | 5 ---- .../action/action.worker_ability_test.t | 13 +++++++++ .../worker/template/main_thread/main_thread.t | 25 ++++++++++++++++ bin/cli/worker/template/utils/action_type.t | 8 +++++ bin/cli/worker/template/utils/payload_type.t | 13 +++++++++ bin/cli/worker/template/worker_thread.t | 5 ---- .../action/action.worker_ability_test.t | 24 +++++++++++++++ .../template/worker_thread/worker_thread.t | 12 ++++++++ 10 files changed, 116 insertions(+), 18 deletions(-) delete mode 100644 bin/cli/worker/template/action_type.t delete mode 100644 bin/cli/worker/template/main_thread.t create mode 100644 bin/cli/worker/template/main_thread/action/action.worker_ability_test.t create mode 100644 bin/cli/worker/template/main_thread/main_thread.t create mode 100644 bin/cli/worker/template/utils/action_type.t create mode 100644 bin/cli/worker/template/utils/payload_type.t delete mode 100644 bin/cli/worker/template/worker_thread.t create mode 100644 bin/cli/worker/template/worker_thread/action/action.worker_ability_test.t create mode 100644 bin/cli/worker/template/worker_thread/worker_thread.t diff --git a/bin/cli/worker/cli.worker.js b/bin/cli/worker/cli.worker.js index 3549e75ca..85b4507c3 100644 --- a/bin/cli/worker/cli.worker.js +++ b/bin/cli/worker/cli.worker.js @@ -1,17 +1,25 @@ const fs = require('fs'); const path = require('path'); -function scanAndCreate(structure, root = process.cwd()) { +function first2UpperCase(str) { + return str.toLowerCase().replace(/( |^)[a-z]/g, L => L.toUpperCase()); +} + +function scanAndCreate(structure, workerName, root = process.env.INIT_CWD) { Object.keys(structure) .forEach(name => { if (typeof structure[name] === 'object') { fs.mkdirSync(path.resolve(root, name)); - scanAndCreate(structure[name], path.resolve(root, `./${name}`)); + scanAndCreate(structure[name], workerName, path.resolve(root, `./${name}`)); } else if (structure[name] === '') { fs.appendFileSync(path.resolve(root, name), ''); } else if (typeof structure[name] === 'string') { - const content = fs.readFileSync(structure[name]).toString(); + let content = fs.readFileSync(structure[name]).toString(); + + content = content + .replace(/\${WorkerName}/g, first2UpperCase(workerName)) + .replace(/\${workerName}/g, workerName); fs.appendFileSync(path.resolve(root, name), content); } @@ -33,15 +41,20 @@ module.exports = { const structure = { [`${name}_worker`]: { 'main_thread': { - action: {}, - [`${name}_main_thread.ts`]: path.resolve(__dirname, './template/main_thread_template.t'), + action: { + 'action.worker_ability_test.ts': path.resolve(__dirname, './template/main_thread/action/action.worker_ability_test.t'), + }, + [`${name}_main_thread.ts`]: path.resolve(__dirname, './template/main_thread/main_thread.t'), }, utils: { - 'action_type.ts': '', + 'action_type.ts': path.resolve(__dirname, './template/utils/action_type.t'), + 'payload_type.ts': path.resolve(__dirname, './template/utils/payload_type.t'), }, 'worker_thread': { - action: {}, - [`${name}_worker_thread.ts`]: path.resolve(__dirname, './template/worker_thread_template.t'), + action: { + 'action.worker_ability_test.ts': path.resolve(__dirname, './template/worker_thread/action/action.worker_ability_test.t'), + }, + [`${name}_worker_thread.ts`]: path.resolve(__dirname, './template/worker_thread/worker_thread.t'), }, }, }; diff --git a/bin/cli/worker/template/action_type.t b/bin/cli/worker/template/action_type.t deleted file mode 100644 index e69de29bb..000000000 diff --git a/bin/cli/worker/template/main_thread.t b/bin/cli/worker/template/main_thread.t deleted file mode 100644 index 8df764495..000000000 --- a/bin/cli/worker/template/main_thread.t +++ /dev/null @@ -1,5 +0,0 @@ -class CrudMainTreadWorker extends BI.Workers.MainThreadWorker { - protected initActions(): void { - // to init some actions - } -} diff --git a/bin/cli/worker/template/main_thread/action/action.worker_ability_test.t b/bin/cli/worker/template/main_thread/action/action.worker_ability_test.t new file mode 100644 index 000000000..42425b859 --- /dev/null +++ b/bin/cli/worker/template/main_thread/action/action.worker_ability_test.t @@ -0,0 +1,13 @@ +import { WorkerAbilityTestActionType } from '../../utils/action_type'; +import { WorkerAbilityTestPayload, WorkerAbilityTestReponse } from '../../utils/payload_type'; + +export class WorkerAbilityTestMainThreadAction extends BI.Workers.WorkerBaseAction { + /** + * 通信能力检测 + */ + public communicationTest(): Promise { + const mainThreadPostTime: WorkerAbilityTestPayload['CommunicationTest'] = Date.now(); + + return this.controller.requestPromise(WorkerAbilityTestActionType.CommunicationTest, mainThreadPostTime); + } +} diff --git a/bin/cli/worker/template/main_thread/main_thread.t b/bin/cli/worker/template/main_thread/main_thread.t new file mode 100644 index 000000000..75f8fb674 --- /dev/null +++ b/bin/cli/worker/template/main_thread/main_thread.t @@ -0,0 +1,25 @@ +import { WorkerAbilityTestMainThreadAction } from './action/action.worker_ability_test'; +// 不需要一起打包的话则不需要引入这行 +import { workerUrl } from 'fui-worker!../worker_thread/${workerName}_worker_thread'; + +class ${WorkerName}MainTreadWorker extends BI.Workers.MainThreadWorker { + private communicationTest: WorkerAbilityTestMainThreadAction; + + public initActions(): void { + this.communicationTest = this.createAction(WorkerAbilityTestMainThreadAction); + } + + public testCommunication() { + return this.communicationTest.communicationTest(); + } +} + +const ${workerName}MainTreadWorker = BI.Workers.createWorker(${WorkerName}MainTreadWorker, { + workerUrl, + workerName: BI.UUID(), +}); + +${workerName}MainTreadWorker.testCommunication() + .then(v => { + console.log(v); + }); diff --git a/bin/cli/worker/template/utils/action_type.t b/bin/cli/worker/template/utils/action_type.t new file mode 100644 index 000000000..c92de897a --- /dev/null +++ b/bin/cli/worker/template/utils/action_type.t @@ -0,0 +1,8 @@ +/* + * Worker 事务标识 + * 每类事务有命名空间, 包含多个具体事务 + */ + +export const enum WorkerAbilityTestActionType { + CommunicationTest = 'CommunicationTest', +} diff --git a/bin/cli/worker/template/utils/payload_type.t b/bin/cli/worker/template/utils/payload_type.t new file mode 100644 index 000000000..6b9a71509 --- /dev/null +++ b/bin/cli/worker/template/utils/payload_type.t @@ -0,0 +1,13 @@ +/** + * 跨线程通信各事务的发送数据类型声明 + */ +export interface WorkerAbilityTestPayload { + CommunicationTest: number; +} + +/** + * 跨线程通信各事务的响应数据类型声明 + */ +export interface WorkerAbilityTestReponse { + CommunicationTest: number; +} diff --git a/bin/cli/worker/template/worker_thread.t b/bin/cli/worker/template/worker_thread.t deleted file mode 100644 index fc457c9c7..000000000 --- a/bin/cli/worker/template/worker_thread.t +++ /dev/null @@ -1,5 +0,0 @@ -class CrudWorkerTreadWorker extends BI.Workers.MainThreadWorker { - protected initActions(): void { - // to init some actions - } -} diff --git a/bin/cli/worker/template/worker_thread/action/action.worker_ability_test.t b/bin/cli/worker/template/worker_thread/action/action.worker_ability_test.t new file mode 100644 index 000000000..f7d1248f4 --- /dev/null +++ b/bin/cli/worker/template/worker_thread/action/action.worker_ability_test.t @@ -0,0 +1,24 @@ +import { WorkerAbilityTestActionType } from '../../utils/action_type'; +import { WorkerAbilityTestPayload, WorkerAbilityTestReponse } from '../../utils/payload_type'; + +export class WorkerAbilityTestWorkerThreadAction extends BI.Workers.WorkerBaseAction { + protected addActionHandler(): void { + this.controller.addActionHandler( + WorkerAbilityTestActionType.CommunicationTest, + this.communicationTest.bind(this) + ); + } + + /** + * 通信能力检测的处理器 + */ + private communicationTest( + payload: WorkerAbilityTestPayload['CommunicationTest'] + ): WorkerAbilityTestReponse['CommunicationTest'] { + const mainThreadPostTime = payload; + // 收到主线程信息的耗时 + const workerGetMessageDuration = Date.now() - mainThreadPostTime; + + return workerGetMessageDuration; + } +} diff --git a/bin/cli/worker/template/worker_thread/worker_thread.t b/bin/cli/worker/template/worker_thread/worker_thread.t new file mode 100644 index 000000000..24c5b91e8 --- /dev/null +++ b/bin/cli/worker/template/worker_thread/worker_thread.t @@ -0,0 +1,12 @@ +// TODO: 这边需要先import fineui资源 +import { WorkerAbilityTestWorkerThreadAction } from './action/action.worker_ability_test'; + +class ${WorkerName}WorkerTreadWorker extends BI.Workers.MainThreadWorker { + public communicationTest: WorkerAbilityTestWorkerThreadAction; + + public initActions(): void { + this.communicationTest = this.createAction(WorkerAbilityTestWorkerThreadAction); + } +} + +export const ${workerName}WorkerTreadWorker = BI.Workers.createWorker(${WorkerName}WorkerTreadWorker);