Best JavaScript code snippet using wpt
worker-task.specs.ts
Source:worker-task.specs.ts
1import {WorkerTask} from "../../../src/common/task/worker-task";2import {ITaskDefinition} from "../../../src/common/task/task-definition";3import {functionId} from "../../../src/common/function/function-id";4describe("WorkerTask", function () {5 let workerTask: WorkerTask<number>;6 const taskDefinition: ITaskDefinition = {7 main: {8 ______serializedFunctionCall: true,9 functionId: functionId("test", 2),10 parameters: [5, 10]11 },12 usedFunctionIds: [functionId("test", 2)]13 };14 beforeEach(function () {15 workerTask = new WorkerTask<number>(taskDefinition);16 });17 describe("isCanceled", function () {18 it("is false by default", function () {19 expect(workerTask.isCanceled).toBe(false);20 });21 it("is true if if the task has been resolved", function () {22 // arrange23 workerTask.cancel();24 // act25 workerTask.resolve(10);26 // assert27 expect(workerTask.isCanceled).toBe(true);28 });29 it("is true if if the task has been resolved as canceled", function () {30 // arrange31 workerTask.cancel();32 // act33 workerTask.resolveCancelled();34 // assert35 expect(workerTask.isCanceled).toBe(true);36 });37 });38 describe("isCancellationRequested", function () {39 it("is false by default", function () {40 expect(workerTask.isCancellationRequested).toBe(false);41 });42 it("is true if the user requested cancellation", function () {43 // act44 workerTask.cancel();45 // assert46 expect(workerTask.isCancellationRequested).toBe(true);47 });48 });49 describe("resolve", function () {50 it("triggers the then handlers", function (done) {51 // arrange52 const handler = jasmine.createSpy("thenHandler");53 workerTask.then(handler);54 // act55 workerTask.resolve(10);56 workerTask.then(function () {57 // assert58 expect(handler).toHaveBeenCalledWith(10);59 done();60 }, done.fail);61 });62 it("triggers the catch handlers if the task has been canceled in the meantime", function (done) {63 // arrange64 const catchHandler = jasmine.createSpy("catchHandler");65 workerTask.catch(catchHandler);66 workerTask.cancel();67 // act68 workerTask.resolve(10);69 workerTask.then(() => done.fail("Then handler called, but catch handler should have been called"), function () {70 // assert71 expect(catchHandler).toHaveBeenCalledWith("Task has been canceled");72 expect(workerTask.isCanceled).toBe(true);73 done();74 });75 });76 });77 describe("resolveCanceled", function () {78 it("triggers the catch handlers when the task is resolved canceled", function (done) {79 // arrange80 const catchHandler = jasmine.createSpy("catchHandler");81 workerTask.catch(catchHandler);82 workerTask.cancel();83 // act84 workerTask.resolveCancelled();85 workerTask.then(() => done.fail("Then handler called, but catch handler should have been called"), function () {86 // assert87 expect(catchHandler).toHaveBeenCalledWith("Task has been canceled");88 done();89 });90 });91 it("marks the task as cancelled", function (done) {92 const catchHandler = jasmine.createSpy("catchHandler");93 workerTask.catch(catchHandler);94 workerTask.cancel();95 // act96 workerTask.resolveCancelled();97 workerTask.then(() => done.fail("Then handler called, but catch handler should have been called"), function () {98 // assert99 expect(workerTask.isCanceled).toBe(true);100 done();101 });102 });103 });104 describe("reject", function () {105 it("triggers the catch handlers", function (done) {106 // arrange107 const catchHandler = jasmine.createSpy("catchHandler");108 workerTask.catch(catchHandler);109 workerTask.cancel();110 // act111 workerTask.reject("Error");112 workerTask.then(() => done.fail("Then handler called, but catch handler should have been called"), function () {113 // assert114 expect(catchHandler).toHaveBeenCalledWith("Error");115 done();116 });117 });118 });119 describe("catch", function () {120 let doneFn: DoneFn;121 let unhandledRejctionHandler: () => void;122 beforeEach(function () {123 unhandledRejctionHandler = function () {124 doneFn.fail("Promise has rejection handler and therefore global unrejected handler should not be called");125 };126 window.addEventListener("unhandledrejection", unhandledRejctionHandler);127 });128 afterEach(function () {129 window.removeEventListener("unhandledrejection", unhandledRejctionHandler);130 });131 it("does not trigger 'unhandled exception in promise' if catch handler is registered", function (done) {132 // arrange133 doneFn = done;134 workerTask.catch(() => done());135 // act136 workerTask.reject("Error occurred");137 });138 });...
thread.js
Source:thread.js
1// namespaces2var dwv = dwv || {};3dwv.utils = dwv.utils || {};4/**5 * Thread Pool.6 * Highly inspired from {@link http://www.smartjava.org/content/html5-easily-parallelize-jobs-using-web-workers-and-threadpool}.7 * @constructor8 * @param {Number} size The size of the pool.9 */10dwv.utils.ThreadPool = function (size) {11 // closure to self12 var self = this;13 // task queue14 this.taskQueue = [];15 // worker queue16 this.workerQueue = [];17 // pool size18 this.poolSize = size;19 /**20 * Initialise.21 */22 this.init = function () {23 // create 'size' number of worker threads24 for (var i = 0; i < size; ++i) {25 self.workerQueue.push(new dwv.utils.WorkerThread(self));26 }27 };28 /**29 * Add a worker task to the queue.30 * Will be run when a thread is made available.31 * @return {Object} workerTask The task to add.32 */33 this.addWorkerTask = function (workerTask) {34 if (self.workerQueue.length > 0) {35 // get the worker thread from the front of the queue36 var workerThread = self.workerQueue.shift();37 workerThread.run(workerTask);38 } else {39 // no free workers, add to queue40 self.taskQueue.push(workerTask);41 }42 };43 /**44 * Free a worker thread.45 * @param {Object} workerThread The thread to free.46 */47 this.freeWorkerThread = function (workerThread) {48 self.onworkerend();49 if (self.taskQueue.length > 0) {50 // don't put back in queue, but execute next task51 var workerTask = self.taskQueue.shift();52 workerThread.run(workerTask);53 } else {54 // no task to run, add to queue55 self.workerQueue.push(workerThread);56 // the work is done when the queue is back to its initial size57 if ( self.workerQueue.length === size ) {58 self.onpoolworkend();59 }60 }61 };62};63/**64 * Handle a pool work end event.65 */66dwv.utils.ThreadPool.prototype.onpoolworkend = function ()67{68 // default does nothing.69};70/**71 * Handle a pool worker end event.72 */73dwv.utils.ThreadPool.prototype.onworkerend = function ()74{75 // default does nothing.76};77/**78 * Worker thread.79 * @external Worker80 * @constructor81 * @param {Object} parentPool The parent pool.82 */83dwv.utils.WorkerThread = function (parentPool) {84 // closure to self85 var self = this;86 // parent pool87 this.parentPool = parentPool;88 // associated task89 this.workerTask = {};90 // associated web worker91 var worker;92 /**93 * Run a worker task94 * @param {Object} workerTask The task to run.95 */96 this.run = function (workerTask) {97 // closure to task98 this.workerTask = workerTask;99 // create a new web worker100 if (this.workerTask.script !== null) {101 worker = new Worker(workerTask.script);102 worker.addEventListener('message', ontaskend, false);103 // launch the worker104 worker.postMessage(workerTask.startMessage);105 }106 };107 /**108 * Handle once the task is done.109 * For now assume we only get a single callback from a worker110 * which also indicates the end of this worker.111 * @param {Object} event The callback event.112 */113 function ontaskend(event) {114 // pass to original callback115 self.workerTask.callback(event);116 // stop the worker117 worker.terminate();118 // tell the parent pool this thread is free119 self.parentPool.freeWorkerThread(self);120 }121};122/**123 * Worker task.124 * @constructor125 * @param {String} script The worker script.126 * @param {Function} parentPool The worker callback.127 * @param {Object} message The data to pass to the worker.128 */129dwv.utils.WorkerTask = function (script, callback, message) {130 // worker script131 this.script = script;132 // worker callback133 this.callback = callback;134 // worker start message135 this.startMessage = message;...
WorkerPool.ts
Source:WorkerPool.ts
...40 for (let i = 0; i < size; i++) {41 this.workerQueue.push(new WorkerThread(this));42 }43 }44 addWorkerTask(workerTask: WorkerTask): void {45 // get the worker from the front of the queue46 let workerThread = this.workerQueue.shift();47 if (workerThread) {48 workerThread.run(workerTask);49 } else {50 // no free workers51 this.taskQueue.push(workerTask);52 }53 }54 freeWorkerThread(workerThread: WorkerThread): void {55 let workerTask = this.taskQueue.shift();56 if (workerTask) {57 // don't put back in queue, but execute next task58 workerThread.run(workerTask);...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var WorkerTask = wpt.WorkerTask;4task.on('log', function(data) {5 console.log(data);6});7task.on('done', function(data) {8 console.log(data);9});10task.run();11var wpt = require('webpagetest');12var wpt = new WebPageTest('www.webpagetest.org');13var WorkerTask = wpt.WorkerTask;14task.on('log', function(data) {15 console.log(data);16});17task.on('done', function(data) {18 console.log(data);19});20task.run();21var wpt = require('webpagetest');22var wpt = new WebPageTest('www.webpagetest.org');23var WorkerTask = wpt.WorkerTask;24task.on('log', function(data) {25 console.log(data);26});27task.on('done', function(data) {28 console.log(data);29});30task.run();31var wpt = require('webpagetest');32var wpt = new WebPageTest('www.webpagetest.org');33var WorkerTask = wpt.WorkerTask;34task.on('log', function(data) {35 console.log(data);36});37task.on('done', function(data) {38 console.log(data);39});40task.run();41var wpt = require('webpagetest');42var wpt = new WebPageTest('www.webpagetest.org');43var WorkerTask = wpt.WorkerTask;44task.on('log', function(data) {45 console.log(data);46});47task.on('done', function(data) {48 console.log(data);49});50task.run();51var wpt = require('webpag
Using AI Code Generation
1var wptask = require('wptask');2var worker = new wptask.WorkerTask();3worker.onmessage = function(e) {4 console.log('Message received from worker');5};6worker.postMessage({foo: 'bar'});7var wptask = require('wptask');8var worker = new wptask.WorkerTask();9worker.onmessage = function(e) {10 console.log('Message received from worker');11};12worker.postMessage({foo: 'bar'});13var http = require('http');14var wptask = require('wptask');15var worker = new wptask.WorkerTask();16worker.onmessage = function (e) {17 console.log('Message received from worker');18};19worker.postMessage({ foo: 'bar' });20http.createServer(function (req, res) {21 res.writeHead(200, { 'Content-Type': 'text/plain' });22 res.end('Hello World23');24}).listen(process.env.PORT || 1337);
Using AI Code Generation
1var myWorker = new Worker('worker.js');2myWorker.postMessage('Hello World');3myWorker.onmessage = function(e) {4 console.log('Message received from worker');5}6onmessage = function(e) {7 console.log('Message received from main script');8 var workerResult = 'Result: ' + (e.data);9 console.log('Posting message back to main script');10 postMessage(workerResult);11}12app.use(function(req, res, next) {13 res.header("Access-Control-Allow-Origin", "*");14 res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");15 next();16});17 url={this.props.videoURL}18 playing={this.props.playing}19 controls={this.props.controls}20 onEnded={this.props.onEnded}
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new wpt('key');3 console.log(data);4});5var wpt = require('wpt');6var wpt = new wpt('key');7 console.log(data);8});9var wpt = require('wpt');10var wpt = new wpt('key');11 console.log(data);12});13var wpt = require('wpt');14var wpt = new wpt('key');15 console.log(data);16});17var wpt = require('wpt');18var wpt = new wpt('key');19 console.log(data);20});21var wpt = require('wpt');22var wpt = new wpt('key');23 console.log(data);24});25var wpt = require('wpt');26var wpt = new wpt('key');
Using AI Code Generation
1var wptask = require('wptask');2var task = wptask.createWorkerTask('task.js');3task.on('message', function (data) {4 console.log(data);5});6task.send({ hello: 'world' });7var wptask = require('wptask');8var task = wptask.createWorkerTask();9task.on('message', function (data) {10 console.log(data);11 task.send({ hello: 'world' });12});13task.send({ hello: 'world' });14var wptask = require('wptask');15var task = wptask.createWorkerTask('task.js');16task.on('message', function (data) {17 console.log(data);18});19task.send({ hello: 'world' });20var wptask = require('wptask');21var task = wptask.createWorkerTask();22task.on('message', function (data) {23 console.log(data);24 task.send({ hello: 'world' });25});26task.send({ hello: 'world' });27var wptask = require('wptask');28var task = wptask.createWorkerTask('task.js');29task.on('message', function (data) {30 console.log(data);31});32task.send({ hello: 'world' });33var wptask = require('wptask');34var task = wptask.createWorkerTask();35task.on('message', function (data) {36 console.log(data);37 task.send({ hello: 'world' });38});39task.send({ hello: 'world' });40var wptask = require('wptask');41var task = wptask.createWorkerTask('task.js');42task.on('message', function (data) {43 console.log(data);44});45task.send({ hello: 'world' });
Using AI Code Generation
1var worker = new Worker('wpt.js');2worker.onmessage = function(e) {3 console.log(e.data);4};5onmessage = function(e) {6 var data = e.data;7 if (data.type == 'start') {8 var url = data.url;9 var runs = data.runs;10 var results = [];11 for (var i = 0; i < runs; i++) {12 results.push({run: i, result: Math.random()});13 }14 postMessage(results);15 }16};17var worker = new Worker('wpt.js');18worker.onmessage = function(e) {19 console.log(e.data);20};21onmessage = function(e) {22 var data = e.data;23 if (data.type == 'start') {24 var url = data.url;25 var runs = data.runs;26 var results = [];27 for (var i = 0; i < runs; i++) {28 results.push({run: i, result: Math.random()});29 }30 postMessage(results);31 }32};33var worker = new Worker('wpt.js');34worker.onmessage = function(e) {35 console.log(e.data);36};37onmessage = function(e) {38 var data = e.data;39 if (data.type == 'start') {
Using AI Code Generation
1var task = new WorkerTask("worker.js");2task.addEventListener("message", function(e) {3 console.log(e.data);4}, false);5task.postMessage("start");6self.addEventListener('message', function(e) {7 self.postMessage("Hello " + e.data);8}, false);9Web Workers: Web Worker Shim (Minified)10Web Workers: Web Worker Shim (Source)11Web Workers: Web Worker Shim (Test Suite)12Web Workers: Web Worker Shim (Tests)13Web Workers: Web Worker Shim (Tests) (Minified)14Web Workers: Web Worker Shim (Tests) (Source)15Web Workers: Web Workers Polyfill (Minified)16Web Workers: Web Workers Polyfill (Source)17Web Workers: Web Workers Polyfill (Test Suite)18Web Workers: Web Workers Polyfill (Tests)19Web Workers: Web Workers Polyfill (Tests) (Minified)20Web Workers: Web Workers Polyfill (Tests) (Source)21Web Workers: Web Workers Shim (Minified
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!