Browse Source

feat: 更新下脚本

es6
iapyang 2 years ago
parent
commit
0038b76320
  1. 29
      bin/cli/worker/cli.worker.js
  2. 0
      bin/cli/worker/template/action_type.t
  3. 5
      bin/cli/worker/template/main_thread.t
  4. 13
      bin/cli/worker/template/main_thread/action/action.worker_ability_test.t
  5. 25
      bin/cli/worker/template/main_thread/main_thread.t
  6. 8
      bin/cli/worker/template/utils/action_type.t
  7. 13
      bin/cli/worker/template/utils/payload_type.t
  8. 5
      bin/cli/worker/template/worker_thread.t
  9. 24
      bin/cli/worker/template/worker_thread/action/action.worker_ability_test.t
  10. 12
      bin/cli/worker/template/worker_thread/worker_thread.t

29
bin/cli/worker/cli.worker.js

@ -1,17 +1,25 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); 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) Object.keys(structure)
.forEach(name => { .forEach(name => {
if (typeof structure[name] === 'object') { if (typeof structure[name] === 'object') {
fs.mkdirSync(path.resolve(root, name)); 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] === '') { } else if (structure[name] === '') {
fs.appendFileSync(path.resolve(root, name), ''); fs.appendFileSync(path.resolve(root, name), '');
} else if (typeof structure[name] === 'string') { } 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); fs.appendFileSync(path.resolve(root, name), content);
} }
@ -33,15 +41,20 @@ module.exports = {
const structure = { const structure = {
[`${name}_worker`]: { [`${name}_worker`]: {
'main_thread': { 'main_thread': {
action: {}, action: {
[`${name}_main_thread.ts`]: path.resolve(__dirname, './template/main_thread_template.t'), '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: { 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': { 'worker_thread': {
action: {}, action: {
[`${name}_worker_thread.ts`]: path.resolve(__dirname, './template/worker_thread_template.t'), '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'),
}, },
}, },
}; };

0
bin/cli/worker/template/action_type.t

5
bin/cli/worker/template/main_thread.t

@ -1,5 +0,0 @@
class CrudMainTreadWorker extends BI.Workers.MainThreadWorker {
protected initActions(): void {
// to init some actions
}
}

13
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<WorkerAbilityTestReponse['CommunicationTest']> {
const mainThreadPostTime: WorkerAbilityTestPayload['CommunicationTest'] = Date.now();
return this.controller.requestPromise(WorkerAbilityTestActionType.CommunicationTest, mainThreadPostTime);
}
}

25
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);
});

8
bin/cli/worker/template/utils/action_type.t

@ -0,0 +1,8 @@
/*
* Worker 事务标识
* 每类事务有命名空间, 包含多个具体事务
*/
export const enum WorkerAbilityTestActionType {
CommunicationTest = 'CommunicationTest',
}

13
bin/cli/worker/template/utils/payload_type.t

@ -0,0 +1,13 @@
/**
* 跨线程通信各事务的发送数据类型声明
*/
export interface WorkerAbilityTestPayload {
CommunicationTest: number;
}
/**
* 跨线程通信各事务的响应数据类型声明
*/
export interface WorkerAbilityTestReponse {
CommunicationTest: number;
}

5
bin/cli/worker/template/worker_thread.t

@ -1,5 +0,0 @@
class CrudWorkerTreadWorker extends BI.Workers.MainThreadWorker {
protected initActions(): void {
// to init some actions
}
}

24
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;
}
}

12
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);
Loading…
Cancel
Save