From 4f0523e2e4216b6966f8169d780c29c0b6a74d93 Mon Sep 17 00:00:00 2001 From: Treecat Date: Thu, 13 Apr 2023 14:43:56 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-14512=20chore:=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/cli/cli.js | 21 +++++ bin/cli/worker/cli.worker.js | 76 +++++++++++++++++++ bin/cli/worker/template/main_thread.helper.t | 48 ++++++++++++ .../action/action.worker_ability_test.t | 13 ++++ .../worker/template/main_thread/main_thread.t | 13 ++++ bin/cli/worker/template/utils/action_type.t | 8 ++ bin/cli/worker/template/utils/payload_type.t | 13 ++++ .../action/action.worker_ability_test.t | 24 ++++++ .../template/worker_thread/worker_thread.t | 12 +++ packages/demo/package.json | 2 +- packages/fineui/package.json | 2 +- {script => scripts}/chinese.js | 0 {script => scripts}/code.static.js | 0 {script => scripts}/lib/fui.component.json | 0 {script => scripts}/lib/fui.export.txt | 0 {script => scripts}/lib/utils.js | 0 {script => scripts}/rename.js | 0 17 files changed, 230 insertions(+), 2 deletions(-) create mode 100644 bin/cli/cli.js create mode 100644 bin/cli/worker/cli.worker.js create mode 100644 bin/cli/worker/template/main_thread.helper.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 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 rename {script => scripts}/chinese.js (100%) rename {script => scripts}/code.static.js (100%) rename {script => scripts}/lib/fui.component.json (100%) rename {script => scripts}/lib/fui.export.txt (100%) rename {script => scripts}/lib/utils.js (100%) rename {script => scripts}/rename.js (100%) diff --git a/bin/cli/cli.js b/bin/cli/cli.js new file mode 100644 index 000000000..513024023 --- /dev/null +++ b/bin/cli/cli.js @@ -0,0 +1,21 @@ +#!/usr/bin/env node +const yargs = require('yargs/yargs'); +const { hideBin } = require('yargs/helpers'); + +const workerCmd = require('./worker/cli.worker'); + +const argv = yargs(hideBin(process.argv)).argv; + +const cmd = argv._[0]; + +const cmds = new Map([ + ['worker', workerCmd], +]); + +if (!cmd) throw new Error('Command is undefined!'); + +if (cmds.has(cmd)) { + cmds.get(cmd)?.exec(argv); +} else { + throw new Error(`Command ${cmd} not supported`); +} diff --git a/bin/cli/worker/cli.worker.js b/bin/cli/worker/cli.worker.js new file mode 100644 index 000000000..b67513fbb --- /dev/null +++ b/bin/cli/worker/cli.worker.js @@ -0,0 +1,76 @@ +const fs = require('fs'); +const path = require('path'); + +function first2UpperCase(str) { + return str.toLowerCase().replace(/( |^)[a-z]/g, L => L.toUpperCase()); +} + +function scanAndCreate(structure, workerName, root) { + Object.keys(structure) + .forEach(name => { + if (typeof structure[name] === 'object') { + fs.mkdirSync(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') { + 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); + } + }); +} + +module.exports = { + exec: async args => { + if (!args.init) { + throw new Error(`Command init not found in args`); + } + + if (!args.name) { + throw new Error('Command --name=... not found in args'); + } + + const name = args.name; + + const structure = { + [`${name}_worker`]: { + 'main_thread': { + 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': path.resolve(__dirname, './template/utils/action_type.t'), + 'payload_type.ts': path.resolve(__dirname, './template/utils/payload_type.t'), + }, + 'worker_thread': { + 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'), + }, + [`${name}_main_thread.helper.ts`]: path.resolve(__dirname, './template/main_thread.helper.t'), + }, + }; + + scanAndCreate(structure, name, args.where ? path.resolve(args.where) : process.cwd()); + }, +}; + +// 结构 +// -xxx_worker +// -|--main_thread +// -|--|--action +// -|--|--xxx_main_thread.ts +// -|--utils +// -|--|--action_type.ts +// -|--worker_thread +// -|--|--action +// -|--|--worker_main_thread.ts diff --git a/bin/cli/worker/template/main_thread.helper.t b/bin/cli/worker/template/main_thread.helper.t new file mode 100644 index 000000000..2ee06acb7 --- /dev/null +++ b/bin/cli/worker/template/main_thread.helper.t @@ -0,0 +1,48 @@ +import { ${WorkerName}MainThreadWorker } from './main_thread/${workerName}_main_thread'; +// 不需要一起打包的话则不需要引入这行 +// FuiWorkerPlugin中的属性会同步到fui-worker中,同时支持loader行内传入属性 +// 根据实际需求传入inline,返回格式 true -> blob url,false -> servicePath +import workerUrl from 'fui-worker!./worker_thread/${workerName}_worker_thread'; + +export class ${WorkerName}WorkerHelper { + private worker: ${WorkerName}MainThreadWorker; + + /** + * 拿到helper中的worker + */ + public getWorker() { + if (this.worker) { + return this.worker; + } + + this.worker = BI.Workers.createWorker(${WorkerName}MainThreadWorker, { + workerUrl: this.urlFormatter(workerUrl), + workerName: BI.UUID(), + }); + + return this.worker; + } + + /** + * 格式化worker url,比如补充一些环境信息到参数里 + * 可通过 #hash 将参数传入blob url + * @param url worker url + */ + private urlFormatter(url: string) { + return url; + } + + /** + * 终止worker + */ + public terminate() { + this.worker?.terminate(); + } +} + +// 使用示例 +// const workerHelper = new ${WorkerName}WorkerHelper(); + +// workerHelper.getWorker() +// .testCommunication() +// .then(res => console.log(res)); 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..00fb09177 --- /dev/null +++ b/bin/cli/worker/template/main_thread/main_thread.t @@ -0,0 +1,13 @@ +import { WorkerAbilityTestMainThreadAction } from './action/action.worker_ability_test'; + +export class ${WorkerName}MainThreadWorker extends BI.Workers.MainThreadWorker { + private communicationTest: WorkerAbilityTestMainThreadAction; + + public initActions(): void { + this.communicationTest = this.createAction(WorkerAbilityTestMainThreadAction); + } + + public testCommunication() { + return this.communicationTest.communicationTest(); + } +} 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/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..f437bbc23 --- /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.WorkerThreadWorker { + public communicationTest: WorkerAbilityTestWorkerThreadAction; + + public initActions(): void { + this.communicationTest = this.createAction(WorkerAbilityTestWorkerThreadAction); + } +} + +export const ${workerName}WorkerTreadWorker = BI.Workers.createWorker(${WorkerName}WorkerTreadWorker); diff --git a/packages/demo/package.json b/packages/demo/package.json index feadc29f9..67c18d5f7 100644 --- a/packages/demo/package.json +++ b/packages/demo/package.json @@ -4,7 +4,7 @@ "description": "", "main": "index.js", "scripts": { - "dev": "webpack-dev-server --open --config=webpack.dev.js", + "dev": "webpack-dev-server --open --config=webpack.dev.js --progress", "build": "webpack --config=webpack.prod.js" }, "keywords": [], diff --git a/packages/fineui/package.json b/packages/fineui/package.json index 707b58a46..217d64e51 100644 --- a/packages/fineui/package.json +++ b/packages/fineui/package.json @@ -20,7 +20,7 @@ ], "scripts": { "dev": "babel src -d dist/es --config-file ./esm.babel.js -w", - "build": "webpack --config=webpack/webpack.prod.js", + "build": "webpack --progress --config=webpack/webpack.prod.js", "webpack:css": "webpack --config=webpack/webpack.css.js --mode production", "biCss": "cross-env LESS_CONFIG_PATH=lessconfig/bi.lessconfig.json LESS_FILE_NAME=bi npm run webpack:css", "jsyCss": "cross-env LESS_CONFIG_PATH=lessconfig/jsy.lessconfig.json LESS_FILE_NAME=jsy npm run webpack:css" diff --git a/script/chinese.js b/scripts/chinese.js similarity index 100% rename from script/chinese.js rename to scripts/chinese.js diff --git a/script/code.static.js b/scripts/code.static.js similarity index 100% rename from script/code.static.js rename to scripts/code.static.js diff --git a/script/lib/fui.component.json b/scripts/lib/fui.component.json similarity index 100% rename from script/lib/fui.component.json rename to scripts/lib/fui.component.json diff --git a/script/lib/fui.export.txt b/scripts/lib/fui.export.txt similarity index 100% rename from script/lib/fui.export.txt rename to scripts/lib/fui.export.txt diff --git a/script/lib/utils.js b/scripts/lib/utils.js similarity index 100% rename from script/lib/utils.js rename to scripts/lib/utils.js diff --git a/script/rename.js b/scripts/rename.js similarity index 100% rename from script/rename.js rename to scripts/rename.js