How to use held method in wpt

Best JavaScript code snippet using wpt

query.tentative.https.any.js

Source:query.tentative.https.any.js Github

copy

Full Screen

1// META: title=Web Locks API: navigator.locks.query method2// META: script=resources/helpers.js3'use strict';4// Returns an array of the modes for the locks with matching name.5function modes(list, name) {6 return list.filter(item => item.name === name).map(item => item.mode);7}8// Returns an array of the clientIds for the locks with matching name.9function clients(list, name) {10 return list.filter(item => item.name === name).map(item => item.clientId);11}12promise_test(async t => {13 const res = uniqueName(t);14 await navigator.locks.request(res, async lock1 => {15 // Attempt to request this again - should be blocked.16 let lock2_acquired = false;17 navigator.locks.request(res, lock2 => { lock2_acquired = true; });18 // Verify that it was blocked.19 await navigator.locks.request(res, {ifAvailable: true}, async lock3 => {20 assert_false(lock2_acquired, 'second request should be blocked');21 assert_equals(lock3, null, 'third request should have failed');22 const state = await navigator.locks.query();23 assert_own_property(state, 'pending', 'State has `pending` property');24 assert_true(Array.isArray(state.pending),25 'State `pending` property is an array');26 const pending_info = state.pending[0];27 assert_own_property(pending_info, 'name',28 'Pending info dictionary has `name` property');29 assert_own_property(pending_info, 'mode',30 'Pending info dictionary has `mode` property');31 assert_own_property(pending_info, 'clientId',32 'Pending info dictionary has `clientId` property');33 assert_own_property(state, 'held', 'State has `held` property');34 assert_true(Array.isArray(state.held),35 'State `held` property is an array');36 const held_info = state.held[0];37 assert_own_property(held_info, 'name',38 'Held info dictionary has `name` property');39 assert_own_property(held_info, 'mode',40 'Held info dictionary has `mode` property');41 assert_own_property(held_info, 'clientId',42 'Held info dictionary has `clientId` property');43 });44 });45}, 'query() returns dictionaries with expected properties');46promise_test(async t => {47 const res = uniqueName(t);48 await navigator.locks.request(res, async lock1 => {49 const state = await navigator.locks.query();50 assert_array_equals(modes(state.held, res), ['exclusive'],51 'Held lock should appear once');52 });53 await navigator.locks.request(res, {mode: 'shared'}, async lock1 => {54 const state = await navigator.locks.query();55 assert_array_equals(modes(state.held, res), ['shared'],56 'Held lock should appear once');57 });58}, 'query() reports individual held locks');59promise_test(async t => {60 const res1 = uniqueName(t);61 const res2 = uniqueName(t);62 await navigator.locks.request(res1, async lock1 => {63 await navigator.locks.request(res2, {mode: 'shared'}, async lock2 => {64 const state = await navigator.locks.query();65 assert_array_equals(modes(state.held, res1), ['exclusive'],66 'Held lock should appear once');67 assert_array_equals(modes(state.held, res2), ['shared'],68 'Held lock should appear once');69 });70 });71}, 'query() reports multiple held locks');72promise_test(async t => {73 const res = uniqueName(t);74 await navigator.locks.request(res, async lock1 => {75 // Attempt to request this again - should be blocked.76 let lock2_acquired = false;77 navigator.locks.request(res, lock2 => { lock2_acquired = true; });78 // Verify that it was blocked.79 await navigator.locks.request(res, {ifAvailable: true}, async lock3 => {80 assert_false(lock2_acquired, 'second request should be blocked');81 assert_equals(lock3, null, 'third request should have failed');82 const state = await navigator.locks.query();83 assert_array_equals(modes(state.pending, res), ['exclusive'],84 'Pending lock should appear once');85 assert_array_equals(modes(state.held, res), ['exclusive'],86 'Held lock should appear once');87 });88 });89}, 'query() reports pending and held locks');90promise_test(async t => {91 const res = uniqueName(t);92 await navigator.locks.request(res, {mode: 'shared'}, async lock1 => {93 await navigator.locks.request(res, {mode: 'shared'}, async lock2 => {94 const state = await navigator.locks.query();95 assert_array_equals(modes(state.held, res), ['shared', 'shared'],96 'Held lock should appear twice');97 });98 });99}, 'query() reports held shared locks with appropriate count');100promise_test(async t => {101 const res = uniqueName(t);102 await navigator.locks.request(res, async lock1 => {103 let lock2_acquired = false, lock3_acquired = false;104 navigator.locks.request(res, {mode: 'shared'},105 lock2 => { lock2_acquired = true; });106 navigator.locks.request(res, {mode: 'shared'},107 lock3 => { lock3_acquired = true; });108 await navigator.locks.request(res, {ifAvailable: true}, async lock4 => {109 assert_equals(lock4, null, 'lock should not be available');110 assert_false(lock2_acquired, 'second attempt should be blocked');111 assert_false(lock3_acquired, 'third attempt should be blocked');112 const state = await navigator.locks.query();113 assert_array_equals(modes(state.held, res), ['exclusive'],114 'Held lock should appear once');115 assert_array_equals(modes(state.pending, res), ['shared', 'shared'],116 'Pending lock should appear twice');117 });118 });119}, 'query() reports pending shared locks with appropriate count');120promise_test(async t => {121 const res1 = uniqueName(t);122 const res2 = uniqueName(t);123 await navigator.locks.request(res1, async lock1 => {124 await navigator.locks.request(res2, async lock2 => {125 const state = await navigator.locks.query();126 const res1_clients = clients(state.held, res1);127 const res2_clients = clients(state.held, res2);128 assert_equals(res1_clients.length, 1, 'Each lock should have one holder');129 assert_equals(res2_clients.length, 1, 'Each lock should have one holder');130 assert_array_equals(res1_clients, res2_clients,131 'Both locks should have same clientId');132 });133 });134}, 'query() reports the same clientId for held locks from the same context');135promise_test(async t => {136 const res = uniqueName(t);137 const worker = new Worker('resources/worker.js');138 t.add_cleanup(() => { worker.terminate(); });139 await postToWorkerAndWait(140 worker, {op: 'request', name: res, mode: 'shared'});141 await navigator.locks.request(res, {mode: 'shared'}, async lock => {142 const state = await navigator.locks.query();143 const res_clients = clients(state.held, res);144 assert_equals(res_clients.length, 2, 'Clients should have same resource');145 assert_not_equals(res_clients[0], res_clients[1],146 'Clients should have different ids');147 });148}, 'query() reports different ids for held locks from different contexts');149promise_test(async t => {150 const res1 = uniqueName(t);151 const res2 = uniqueName(t);152 const worker = new Worker('resources/worker.js');153 t.add_cleanup(() => { worker.terminate(); });154 // Acquire 1 in the worker.155 await postToWorkerAndWait(worker, {op: 'request', name: res1})156 // Acquire 2 here.157 await new Promise(resolve => {158 navigator.locks.request(res2, lock => {159 resolve();160 return new Promise(() => {}); // Never released.161 });162 });163 // Request 2 in the worker.164 postToWorkerAndWait(worker, {op: 'request', name: res2});165 assert_true((await postToWorkerAndWait(worker, {166 op: 'request', name: res2, ifAvailable: true167 })).failed, 'Lock request should have failed');168 // Request 1 here.169 navigator.locks.request(170 res1, t.unreached_func('Lock should not be acquired'));171 // Verify that we're seeing a deadlock.172 const state = await navigator.locks.query();173 const res1_held_clients = clients(state.held, res1);174 const res2_held_clients = clients(state.held, res2);175 const res1_pending_clients = clients(state.pending, res1);176 const res2_pending_clients = clients(state.pending, res2);177 assert_equals(res1_held_clients.length, 1);178 assert_equals(res2_held_clients.length, 1);179 assert_equals(res1_pending_clients.length, 1);180 assert_equals(res2_pending_clients.length, 1);181 assert_equals(res1_held_clients[0], res2_pending_clients[0]);182 assert_equals(res2_held_clients[0], res1_pending_clients[0]);...

Full Screen

Full Screen

finalizationRegistry.js

Source:finalizationRegistry.js Github

copy

Full Screen

1function checkPropertyDescriptor(obj, property, writable, enumerable,2 configurable) {3 let desc = Object.getOwnPropertyDescriptor(obj, property);4 assertEq(typeof desc, "object");5 assertEq(desc.writable, writable);6 assertEq(desc.enumerable, enumerable);7 assertEq(desc.configurable, configurable);8}9function assertThrowsTypeError(thunk) {10 let error;11 try {12 thunk();13 } catch (e) {14 error = e;15 }16 assertEq(error instanceof TypeError, true);17}18// 3.1 The FinalizationRegistry Constructor19assertEq(typeof this.FinalizationRegistry, "function");20// 3.1.1 FinalizationRegistry ( cleanupCallback ) 21assertThrowsTypeError(() => new FinalizationRegistry());22assertThrowsTypeError(() => new FinalizationRegistry(1));23new FinalizationRegistry(x => 0);24// 3.2 Properties of the FinalizationRegistry Constructor25assertEq(Object.getPrototypeOf(FinalizationRegistry), Function.prototype);26// 3.2.1 FinalizationRegistry.prototype27checkPropertyDescriptor(FinalizationRegistry, 'prototype', false, false, false);28// 3.3 Properties of the FinalizationRegistry Prototype Object29let proto = FinalizationRegistry.prototype;30assertEq(Object.getPrototypeOf(proto), Object.prototype);31// 3.3.1 FinalizationRegistry.prototype.constructor32assertEq(proto.constructor, FinalizationRegistry);33// 3.3.2 FinalizationRegistry.prototype.register ( target , holdings [, unregisterToken ] )34assertEq(proto.hasOwnProperty('register'), true);35assertEq(typeof proto.register, 'function');36// 3.3.3 FinalizationRegistry.prototype.unregister ( unregisterToken )37assertEq(proto.hasOwnProperty('unregister'), true);38assertEq(typeof proto.unregister, 'function');39// 3.3.4 FinalizationRegistry.prototype.cleanupSome ( [ callback ] )40assertEq(proto.hasOwnProperty('cleanupSome'), true);41assertEq(typeof proto.cleanupSome, 'function');42// 3.3.5 FinalizationRegistry.prototype [ @@toStringTag ]43assertEq(proto[Symbol.toStringTag], "FinalizationRegistry");44checkPropertyDescriptor(proto, Symbol.toStringTag, false, false, true);45// 3.4 Properties of FinalizationRegistry Instances46let registry = new FinalizationRegistry(x => 0);47assertEq(Object.getPrototypeOf(registry), proto);48assertEq(Object.getOwnPropertyNames(registry).length, 0);49let heldValues = [];50registry = new FinalizationRegistry(value => {51 heldValues.push(value);52});53// Test a single target.54heldValues = [];55registry.register({}, 42);56gc();57drainJobQueue();58assertEq(heldValues.length, 1);59assertEq(heldValues[0], 42);60// Test multiple targets.61heldValues = [];62for (let i = 0; i < 100; i++) {63 registry.register({}, i);64}65gc();66drainJobQueue();67assertEq(heldValues.length, 100);68heldValues = heldValues.sort((a, b) => a - b);69for (let i = 0; i < 100; i++) {70 assertEq(heldValues[i], i);71}72// Test a single object in multiple registries73heldValues = [];74let heldValues2 = [];75let registry2 = new FinalizationRegistry(value => {76 heldValues2.push(value);77});78{79 let object = {};80 registry.register(object, 1);81 registry2.register(object, 2);82 object = null;83}84gc();85drainJobQueue();86assertEq(heldValues.length, 1);87assertEq(heldValues[0], 1);88assertEq(heldValues2.length, 1);89assertEq(heldValues2[0], 2);90// Unregister a single target.91heldValues = [];92let token = {};93registry.register({}, 1, token);94registry.unregister(token);95gc();96drainJobQueue();97assertEq(heldValues.length, 0);98// Unregister multiple targets.99heldValues = [];100let token2 = {};101registry.register({}, 1, token);102registry.register({}, 2, token2);103registry.register({}, 3, token);104registry.register({}, 4, token2);105registry.unregister(token);106gc();107drainJobQueue();108assertEq(heldValues.length, 2);109heldValues = heldValues.sort((a, b) => a - b);110assertEq(heldValues[0], 2);111assertEq(heldValues[1], 4);112// Watch object in another global.113let other = newGlobal({newCompartment: true});114heldValues = [];115registry.register(evalcx('({})', other), 1);116gc();117drainJobQueue();118assertEq(heldValues.length, 1);119assertEq(heldValues[0], 1);120// Pass heldValues from another global.121let heldValue = evalcx('{}', other);122heldValues = [];123registry.register({}, heldValue);124gc();125drainJobQueue();126assertEq(heldValues.length, 1);127assertEq(heldValues[0], heldValue);128// Pass unregister token from another global.129token = evalcx('({})', other);130heldValues = [];131registry.register({}, 1, token);132gc();133drainJobQueue();134assertEq(heldValues.length, 1);135assertEq(heldValues[0], 1);136heldValues = [];137registry.register({}, 1, token);138registry.unregister(token);139gc();140drainJobQueue();141assertEq(heldValues.length, 0);142// FinalizationRegistry is designed to be subclassable.143class MyRegistry extends FinalizationRegistry {144 constructor(callback) {145 super(callback);146 }147}148let r2 = new MyRegistry(value => {149 heldValues.push(value);150});151heldValues = [];152r2.register({}, 42);153gc();154drainJobQueue();155assertEq(heldValues.length, 1);156assertEq(heldValues[0], 42);157// Test cleanupSome.158heldValues = [];159let r5 = new FinalizationRegistry(v => heldValues.push(v));160r5.register({}, 1);161r5.register({}, 2);162r5.register({}, 3);163gc();164r5.cleanupSome();165assertEq(heldValues.length, 3);166heldValues = heldValues.sort((a, b) => a - b);167assertEq(heldValues[0], 1);168assertEq(heldValues[1], 2);169assertEq(heldValues[2], 3);170// Test trying to call cleanupSome in callback.171let r6 = new FinalizationRegistry(x => {172 r6.cleanupSome();173});174r6.register({}, 1);175gc();176drainJobQueue();177// Test trying to call cleanupSome in callback with multiple values.178let callbackCounter7 = 0;179let r7 = new FinalizationRegistry(x => {180 callbackCounter7++;181 r7.cleanupSome();182});183r7.register({}, 1);184r7.register({}, 2);185r7.register({}, 3);186r7.register({}, 4);187gc();188drainJobQueue();189assertEq(callbackCounter7, 4);190// Test that targets don't keep the finalization registry alive.191let target = {};192registry = new FinalizationRegistry(value => undefined);193registry.register(target, 1);194let weakRef = new WeakRef(registry);195registry = undefined;196assertEq(typeof weakRef.deref(), 'object');197drainJobQueue();198gc();199assertEq(weakRef.deref(), undefined);200assertEq(typeof target, 'object');201// Test that targets don't keep the finalization registry alive when also202// used as the unregister token.203registry = new FinalizationRegistry(value => undefined);204registry.register(target, 1, target);205weakRef = new WeakRef(registry);206registry = undefined;207assertEq(typeof weakRef.deref(), 'object');208drainJobQueue();209gc();210assertEq(weakRef.deref(), undefined);211assertEq(typeof target, 'object');212// Test that cleanup doesn't happen if the finalization registry dies.213heldValues = [];214new FinalizationRegistry(value => {215 heldValues.push(value);216}).register({}, 1);217gc();218drainJobQueue();...

Full Screen

Full Screen

app.js

Source:app.js Github

copy

Full Screen

1let heldValue = null23let heldOperation = null45let nextValue = null67$('.digits button').click(function(){8 if (nextValue === null){9 nextValue = '0'10 } 1112 nextValue += $(this).text()1314 updateDisplay()15})1617$('.add').click(function (){18 setHeldOp(add)1920 $('.next-operation').text('+')21 updateDisplay()22})2324$('.subtract').click(function(){25 setHeldOp(subtract)2627 $('.next-operation').text('-')28 updateDisplay()29})3031$('.multiply').click(function(){32 setHeldOp(multiply)3334 $('.next-operation').text('X')35 updateDisplay()36})3738$('.divide').click(function(){39 setHeldOp(divide)4041 $('.next-operation').text('/')42 updateDisplay()43})4445$('.equals').click(function (){46 setHeldOp(null)47 48 $('.next-operation').text('')4950 updateDisplay()51})5253$('.clear-all').click(function (){54 heldValue = null;55 nextValue = null;56 heldOperation = null;5758 $('.next-operation').text('')5960 updateDisplay()61})6263$('.clear-entry').click(function (){64 nextValue = null6566 updateDisplay()67})6869function updateDisplay(){70 showValue('.held-value', heldValue)71 showValue('.next-value', nextValue)72}7374function showValue(location, value){75 if (value === null){76 $(location).text('')77 } else {78 $(location).text(Number(value))79 }80}81828384function add(num1, num2){85 return num1 + num286}8788function subtract(num1, num2){89 return num1 - num290}9192function multiply(num1, num2){93 return num1 * num294}9596function divide(num1, num2){97 return num1 / num298}99100function setHeldOp(operation){101 if(heldOperation !== null){102 heldValue = heldOperation(Number(heldValue), Number(nextValue))103 } else if (nextValue !== null && heldOperation === null){104 heldValue = nextValue105 }106107 nextValue = null108109 heldOperation = operation110}111 ...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) return console.error(err);4 console.log(data);5 wpt.getTestResults(data.data.testId, function(err, data) {6 if (err) return console.error(err);7 console.log(data);8 });9});10#### wpt.getTestResults(testId, callback)11var wpt = require('webpagetest');12var wpt = new WebPageTest('www.webpagetest.org');13wpt.getTestResults(150619_8E_8c9e9e4c4f1d1d26b8c7a0f5c3e3d3c3, function(err, data) {14 if (err) return console.error(err);15 console.log(data);16});17#### wpt.getLocations(callback)18var wpt = require('webpagetest');19var wpt = new WebPageTest('www.webpagetest.org');20wpt.getLocations(function(err, data) {21 if (err) return console.error(err);22 console.log(data);23});24#### wpt.getTesters(callback)25var wpt = require('webpagetest');

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var options = {3};4var webPageTest = new wpt(options);5var testOptions = {6 videoParams: {7 },8 timelineParams: {9 }10};11webPageTest.runTest(testURL, testOptions, function (err, data) {12 if (err) {13 console.log(err);14 } else {15 console.log(data);16 }17});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt.js');2wpt.held('API_KEY', 'TEST_URL', function(data) {3 console.log(data);4});5var wpt = require('wpt.js');6wpt.test('API_KEY', 'TEST_URL', function(data) {7 console.log(data);8});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2var page = wptools.page('Albert Einstein', {verbose: false});3page.held(function (err, result) {4 console.log(result);5});6var wptools = require('wptools');7var page = wptools.page('Albert Einstein', {verbose: false});8page.held(function (err, result) {9 console.log(result);10});11var wptools = require('wptools');12var page = wptools.page('Albert Einstein', {verbose: false});13page.held(function (err, result) {14 console.log(result);15});16var wptools = require('wptools');17var page = wptools.page('Albert Einstein', {verbose: false});18page.held(function (err, result) {19 console.log(result);20});21var wptools = require('wptools');22var page = wptools.page('Albert Einstein', {verbose: false});23page.held(function (err, result) {24 console.log(result);25});26var wptools = require('wptools');27var page = wptools.page('Albert Einstein', {verbose: false});28page.held(function (err, result) {29 console.log(result);30});31var wptools = require('wptools');32var page = wptools.page('Albert Einstein', {verbose: false});33page.held(function (err, result) {34 console.log(result);35});36var wptools = require('wptools');37var page = wptools.page('Albert Einstein', {verbose: false});38page.held(function (err, result) {39 console.log(result);40});41var wptools = require('wptools');

Full Screen

Using AI Code Generation

copy

Full Screen

1var WptDriver = require('wptdriver').WptDriver;2var driver = new WptDriver();3driver.runTest(function(err, result) {4 if (err) {5 console.log(err);6 } else {7 console.log(result);8 }9});

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