How to use initWorker method in wpt

Best JavaScript code snippet using wpt

initWorker.ts

Source:initWorker.ts Github

copy

Full Screen

1import { store, Store } from "../utils/storage";2import { Data } from "../data/index";3import { flatten } from "../utils/index";4import { channel } from "../EventChannel/index";5interface sendWorkData {6 operatorType: OperatorType;7 tableName: string;8 data: {9 [key: string]: any;10 };11}12type ContextGetRequest = (13 data: Data & { tableName: string },14 url: string15) => void;16type ContextPostRequest = (17 data: Data & { tableName: string },18 url: string19) => void;20// 引入 web worker 增强计算能力?21// 还是封装一手比较 舒服22export class InitWorker {23 worker: Worker;24 store: Store;25 success: boolean;26 messageQuene: sendData[];27 constructor() {28 this.worker = new Worker("./worker.js");29 this.messageQuene = [];30 this.store = store;31 }32 /**33 * @param {sendData} data34 * @memberof InitWorker35 */36 sentMessageToWorker(data: sendData) {37 const that = this;38 if (this.success) {39 if (this.messageQuene.length) {40 this.messageQuene.forEach((val) => {41 that.worker.postMessage(JSON.stringify(val));42 });43 this.messageQuene = []; // 发完就清空44 }45 // 把这次的也发送了...46 that.worker.postMessage(JSON.stringify(data));47 } else {48 this.messageQuene.push(data); // 先暂存一下 等待时机 一波发送49 }50 }51 // 这里其实是开启监听 后续的交互都在这里交互了....52 /**53 *54 * @param {InitWorker} worker55 * @param {ContextGetRequest} req56 * @memberof InitWorker57 */58 acceptMessageFromWorker(59 worker: InitWorker,60 getReq: ContextGetRequest,61 postReq: ContextPostRequest62 ) {63 const that = this;64 this.worker.addEventListener("message", (message: MessageEvent) => {65 const { saveType, acceptLastVisited, data, success } = JSON.parse(66 message.data67 );68 // 因为这里涉及一个 异步 和 谁先谁后的问题。最后采取worker准备好了 就通知主线程 再发送确保一定能收到通知后再进行操作69 // 谁先谁后 监听到就行?70 if (success) {71 this.success = true;72 const LastVisited = worker.store.get("LastVisited");73 worker.sentMessageToWorker({74 saveType: "store",75 data: { LastVisited },76 });77 }78 if (saveType === "store") {79 that.store.set("LastVisited", acceptLastVisited);80 } else if (saveType === "indexDB") {81 // 因为这里已经是整合了的数据 可能会比较大 所以 这里要采取post的请求 不限制长度 也避免了 get 请求 出现特殊字符的时候 url 别截取的情况82 // 这里需要一个 tableName 在写入的时候 写进去83 // getReq({ ...data, tableName }, `/error`);84 // 这里得减少请求 次数85 // postReq({ ...data, tableName }, `/postError`);86 // reqQuene.push({ ...data, tableName, url: `/postError` });87 // 这里的data 还得扁平化88 const reqQuene = flatten(data); // 经过重重筛选后 最后的上报89 // 把一次所有的都上报90 postReq(reqQuene, `/postError`);91 }92 });93 this.worker.addEventListener("messageerror", (message: MessageEvent) => {});94 }95 /**96 *97 * @param {SaveType} saveType98 * @param {sendWorkData} data99 * @memberof InitWorker100 */101 add(saveType: SaveType, data: sendWorkData) {102 this.sentMessageToWorker({ saveType, data });103 }104 /**105 *106 *107 * @param {SaveType} saveType108 * @param {sendWorkData} data109 * @memberof InitWorker110 */111 read(saveType: SaveType, data: sendWorkData) {112 this.sentMessageToWorker({ saveType, data });113 }114 /**115 *116 *117 * @param {SaveType} saveType118 * @param {sendWorkData} data119 * @memberof InitWorker120 */121 clear(saveType: SaveType, data: sendWorkData) {122 this.sentMessageToWorker({ saveType, data });123 }124}125export const worker = new InitWorker();126/**127 *128 *129 * @export130 * @param {ContextGetRequest} req131 */132export function workerMain(133 getReq: ContextGetRequest,134 postReq: ContextPostRequest135) {136 worker.acceptMessageFromWorker(worker, getReq, postReq);137 channel.subscribe("IDLE", function () {138 if ((window as any).requestIdleCallback) {139 (window as any).requestIdleCallback(function () {140 console.log("idle");141 worker.worker.postMessage(JSON.stringify({ idle: true }));142 });143 } else {144 // 不支持就直接发送了 占用一点资源145 worker.worker.postMessage(JSON.stringify({ idle: true }));146 }147 });...

Full Screen

Full Screen

dataQuene.ts

Source:dataQuene.ts Github

copy

Full Screen

1import { Data } from "./index";2import { InitWorker } from "./../worker/initWorker";3import DB from "./dataBase";4// 主线程专用添加修改数据的 队列5// 因为IndexDB 异步的原因。获取DBDataBase对象需要异步 这里就会出现很多问题6// 在实例方法里多次拿 会造成 后续拿不到的情况 并直接跳过不执行后续方法7// 假如全局Context 中 异步获取的时候,又因为监听事件在 异步获取之前,导致监听的时候 没有执行 patch的代码 导致无法上报错误8// 将所有IndexDB的读写都放进 worker里面做。9// 各司其职10// 主线程的IndexDB读写 应该 也要放到webWorker里面 假如 不断的读写 也能不阻塞主线程的情况下进行读写11// 后续发现 直接操作 worker 就可以了 这个东西暂时就废弃了12type Quene =13 | {14 operatorType: OperatorType;15 tableName: string;16 data: Data;17 }[]18 | undefined;19export default class DataQuene {20 dataQuene: Quene;21 db: DB;22 worker: InitWorker;23 dataOperator: Map<OperatorType, Function>;24 constructor() {25 this.worker = new InitWorker();26 this.db = new DB("monitor");27 // 事件队列28 this.dataQuene = [];29 }30 // 推进队列中31 add(operatorType: OperatorType, tableName: string, data?: Data) {32 // 好像之前写的都没有判空....33 this.dataQuene.push({ operatorType, tableName, data });34 // this.run();35 }36 // 关键的run 方法?37 run() {38 // 实例化DB对象39 if (this.dataQuene.length) {40 // 拿到队列的第一个41 const params = this.dataQuene.shift();42 this.worker[params.operatorType as OperatorType]("indexDB", params);43 // 保证把 队列里面所有的CallBack全部执行44 this.run();45 }46 }...

Full Screen

Full Screen

index.js

Source:index.js Github

copy

Full Screen

...3} = require('worker_threads');4try{require('child_process').execSync('killall chrome')}catch{}56let t = parseInt(parseInt(process.argv[3]))7function initWorker(i) {8 const worker = new Worker('./worker.js', {9 workerData: {10 count: process.argv[2],11 id: i12 }13 });14 worker.on('message', message => {15 console.log(message);16 });17 worker.on('error', error => {18 console.log(error);19 initWorker(i)20 })21 worker.on('exit', code => {22 console.log(`Worker stopped with exit code ${code}`);23 initWorker(i)24 });25}26for (let i = 0; i < t; i++) {27 initWorker(i) ...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var worker = new Worker('wpt-worker.js');2worker.onmessage = function(e) {3 console.log(e.data);4};5self.addEventListener('message', function(e) {6 var data = e.data;7 switch (data.cmd) {8 var url = data.data.url;9 var xhr = new XMLHttpRequest();10 xhr.open('GET', url, true);11 xhr.onload = function() {12 var response = xhr.responseText;13 self.postMessage(response);14 };15 xhr.send();16 break;17 self.postMessage('Unknown command: ' + data.msg);18 };19}, false);

Full Screen

Using AI Code Generation

copy

Full Screen

1function initWorker() {2 var worker = new Worker('worker.js');3 worker.onmessage = function(event) {4 console.log(event.data);5 };6 worker.postMessage('Hello, World!');7}8onmessage = function(event) {9 postMessage('Worker: ' + event.data);10};11var worker = new Worker('worker.js');12var worker = new Worker('worker.js');13onmessage = function(e) {14 worker.postMessage(e.data);15}16worker.onmessage = function(e) {17 postMessage(e.data);18}19var worker = new Worker('worker.js');20onmessage = function(e) {21 worker.postMessage(e.data);22}23worker.onmessage = function(e) {24 postMessage(e.data);25}26var worker = new Worker('worker.js');27onmessage = function(e) {28 worker.postMessage(e.data);29}30worker.onmessage = function(e) {31 postMessage(e.data);32}33var worker = new Worker('worker.js');34onmessage = function(e) {35 worker.postMessage(e.data);36}37worker.onmessage = function(e) {38 postMessage(e.data);39}

Full Screen

Using AI Code Generation

copy

Full Screen

1const wpt = require('./wpt-worker.js');2const wpt = require('./wpt-worker.js');3 if (err) {4 console.log(err);5 } else {6 console.log(data);7 }8});9const wpt = require('./wpt-worker.js');10 if (err) {11 console.log(err);12 } else {13 wpt.getTestResults(data.data.testId, (err, data) => {14 if (err) {15 console.log(err);16 } else {17 console.log(data);18 }19 });20 }21});22const wpt = require('./wpt-worker.js');23 if (err) {24 console.log(err);25 } else {26 wpt.getTestResults(data.data.testId, (err, data) => {27 if (err) {28 console.log(err);29 } else {30 wpt.getTestResults(data.data.testId, (err, data) => {31 if (err) {32 console.log(err);33 } else {34 console.log(data);35 }36 });37 }38 });39 }40});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful