Best JavaScript code snippet using playwright-internal
ReactUpdates.js
Source:ReactUpdates.js
1/* */ 2(function(process) {3 'use strict';4 var CallbackQueue = require("./CallbackQueue");5 var PooledClass = require("./PooledClass");6 var ReactCurrentOwner = require("./ReactCurrentOwner");7 var ReactPerf = require("./ReactPerf");8 var ReactReconciler = require("./ReactReconciler");9 var Transaction = require("./Transaction");10 var assign = require("./Object.assign");11 var invariant = require("./invariant");12 var warning = require("./warning");13 var dirtyComponents = [];14 var asapCallbackQueue = CallbackQueue.getPooled();15 var asapEnqueued = false;16 var batchingStrategy = null;17 function ensureInjected() {18 ("production" !== process.env.NODE_ENV ? invariant(ReactUpdates.ReactReconcileTransaction && batchingStrategy, 'ReactUpdates: must inject a reconcile transaction class and batching ' + 'strategy') : invariant(ReactUpdates.ReactReconcileTransaction && batchingStrategy));19 }20 var NESTED_UPDATES = {21 initialize: function() {22 this.dirtyComponentsLength = dirtyComponents.length;23 },24 close: function() {25 if (this.dirtyComponentsLength !== dirtyComponents.length) {26 dirtyComponents.splice(0, this.dirtyComponentsLength);27 flushBatchedUpdates();28 } else {29 dirtyComponents.length = 0;30 }31 }32 };33 var UPDATE_QUEUEING = {34 initialize: function() {35 this.callbackQueue.reset();36 },37 close: function() {38 this.callbackQueue.notifyAll();39 }40 };41 var TRANSACTION_WRAPPERS = [NESTED_UPDATES, UPDATE_QUEUEING];42 function ReactUpdatesFlushTransaction() {43 this.reinitializeTransaction();44 this.dirtyComponentsLength = null;45 this.callbackQueue = CallbackQueue.getPooled();46 this.reconcileTransaction = ReactUpdates.ReactReconcileTransaction.getPooled();47 }48 assign(ReactUpdatesFlushTransaction.prototype, Transaction.Mixin, {49 getTransactionWrappers: function() {50 return TRANSACTION_WRAPPERS;51 },52 destructor: function() {53 this.dirtyComponentsLength = null;54 CallbackQueue.release(this.callbackQueue);55 this.callbackQueue = null;56 ReactUpdates.ReactReconcileTransaction.release(this.reconcileTransaction);57 this.reconcileTransaction = null;58 },59 perform: function(method, scope, a) {60 return Transaction.Mixin.perform.call(this, this.reconcileTransaction.perform, this.reconcileTransaction, method, scope, a);61 }62 });63 PooledClass.addPoolingTo(ReactUpdatesFlushTransaction);64 function batchedUpdates(callback, a, b, c, d) {65 ensureInjected();66 batchingStrategy.batchedUpdates(callback, a, b, c, d);67 }68 function mountOrderComparator(c1, c2) {69 return c1._mountOrder - c2._mountOrder;70 }71 function runBatchedUpdates(transaction) {72 var len = transaction.dirtyComponentsLength;73 ("production" !== process.env.NODE_ENV ? invariant(len === dirtyComponents.length, 'Expected flush transaction\'s stored dirty-components length (%s) to ' + 'match dirty-components array length (%s).', len, dirtyComponents.length) : invariant(len === dirtyComponents.length));74 dirtyComponents.sort(mountOrderComparator);75 for (var i = 0; i < len; i++) {76 var component = dirtyComponents[i];77 var callbacks = component._pendingCallbacks;78 component._pendingCallbacks = null;79 ReactReconciler.performUpdateIfNecessary(component, transaction.reconcileTransaction);80 if (callbacks) {81 for (var j = 0; j < callbacks.length; j++) {82 transaction.callbackQueue.enqueue(callbacks[j], component.getPublicInstance());83 }84 }85 }86 }87 var flushBatchedUpdates = function() {88 while (dirtyComponents.length || asapEnqueued) {89 if (dirtyComponents.length) {90 var transaction = ReactUpdatesFlushTransaction.getPooled();91 transaction.perform(runBatchedUpdates, null, transaction);92 ReactUpdatesFlushTransaction.release(transaction);93 }94 if (asapEnqueued) {95 asapEnqueued = false;96 var queue = asapCallbackQueue;97 asapCallbackQueue = CallbackQueue.getPooled();98 queue.notifyAll();99 CallbackQueue.release(queue);100 }101 }102 };103 flushBatchedUpdates = ReactPerf.measure('ReactUpdates', 'flushBatchedUpdates', flushBatchedUpdates);104 function enqueueUpdate(component) {105 ensureInjected();106 ("production" !== process.env.NODE_ENV ? warning(ReactCurrentOwner.current == null, 'enqueueUpdate(): Render methods should be a pure function of props ' + 'and state; triggering nested component updates from render is not ' + 'allowed. If necessary, trigger nested updates in ' + 'componentDidUpdate.') : null);107 if (!batchingStrategy.isBatchingUpdates) {108 batchingStrategy.batchedUpdates(enqueueUpdate, component);109 return ;110 }111 dirtyComponents.push(component);112 }113 function asap(callback, context) {114 ("production" !== process.env.NODE_ENV ? invariant(batchingStrategy.isBatchingUpdates, 'ReactUpdates.asap: Can\'t enqueue an asap callback in a context where' + 'updates are not being batched.') : invariant(batchingStrategy.isBatchingUpdates));115 asapCallbackQueue.enqueue(callback, context);116 asapEnqueued = true;117 }118 var ReactUpdatesInjection = {119 injectReconcileTransaction: function(ReconcileTransaction) {120 ("production" !== process.env.NODE_ENV ? invariant(ReconcileTransaction, 'ReactUpdates: must provide a reconcile transaction class') : invariant(ReconcileTransaction));121 ReactUpdates.ReactReconcileTransaction = ReconcileTransaction;122 },123 injectBatchingStrategy: function(_batchingStrategy) {124 ("production" !== process.env.NODE_ENV ? invariant(_batchingStrategy, 'ReactUpdates: must provide a batching strategy') : invariant(_batchingStrategy));125 ("production" !== process.env.NODE_ENV ? invariant(typeof _batchingStrategy.batchedUpdates === 'function', 'ReactUpdates: must provide a batchedUpdates() function') : invariant(typeof _batchingStrategy.batchedUpdates === 'function'));126 ("production" !== process.env.NODE_ENV ? invariant(typeof _batchingStrategy.isBatchingUpdates === 'boolean', 'ReactUpdates: must provide an isBatchingUpdates boolean attribute') : invariant(typeof _batchingStrategy.isBatchingUpdates === 'boolean'));127 batchingStrategy = _batchingStrategy;128 }129 };130 var ReactUpdates = {131 ReactReconcileTransaction: null,132 batchedUpdates: batchedUpdates,133 enqueueUpdate: enqueueUpdate,134 flushBatchedUpdates: flushBatchedUpdates,135 injection: ReactUpdatesInjection,136 asap: asap137 };138 module.exports = ReactUpdates;...
01ded5ReactUpdates.js
Source:01ded5ReactUpdates.js
1'use strict';2var CallbackQueue = require('CallbackQueue');3var PooledClass = require('PooledClass');4var ReactFeatureFlags = require('ReactFeatureFlags');5var ReactReconciler = require('ReactReconciler');6var Transaction = require('Transaction');7var invariant = require('fbjs/lib/invariant');8var dirtyComponents = [];9var updateBatchNumber = 0;10var asapCallbackQueue = CallbackQueue.getPooled();11var asapEnqueued = false;12var batchingStrategy = null;13function ensureInjected() {14 invariant(ReactUpdates.ReactReconcileTransaction && batchingStrategy, 'ReactUpdates: must inject a reconcile transaction class and batching ' + 'strategy');15}16var NESTED_UPDATES = {17 initialize: function initialize() {18 this.dirtyComponentsLength = dirtyComponents.length;19 },20 close: function close() {21 if (this.dirtyComponentsLength !== dirtyComponents.length) {22 dirtyComponents.splice(0, this.dirtyComponentsLength);23 flushBatchedUpdates();24 } else {25 dirtyComponents.length = 0;26 }27 }28};29var UPDATE_QUEUEING = {30 initialize: function initialize() {31 this.callbackQueue.reset();32 },33 close: function close() {34 this.callbackQueue.notifyAll();35 }36};37var TRANSACTION_WRAPPERS = [NESTED_UPDATES, UPDATE_QUEUEING];38function ReactUpdatesFlushTransaction() {39 this.reinitializeTransaction();40 this.dirtyComponentsLength = null;41 this.callbackQueue = CallbackQueue.getPooled();42 this.reconcileTransaction = ReactUpdates.ReactReconcileTransaction.getPooled(true);43}44babelHelpers.extends(ReactUpdatesFlushTransaction.prototype, Transaction, {45 getTransactionWrappers: function getTransactionWrappers() {46 return TRANSACTION_WRAPPERS;47 },48 destructor: function destructor() {49 this.dirtyComponentsLength = null;50 CallbackQueue.release(this.callbackQueue);51 this.callbackQueue = null;52 ReactUpdates.ReactReconcileTransaction.release(this.reconcileTransaction);53 this.reconcileTransaction = null;54 },55 perform: function perform(method, scope, a) {56 return Transaction.perform.call(this, this.reconcileTransaction.perform, this.reconcileTransaction, method, scope, a);57 }58});59PooledClass.addPoolingTo(ReactUpdatesFlushTransaction);60function batchedUpdates(callback, a, b, c, d, e) {61 ensureInjected();62 return batchingStrategy.batchedUpdates(callback, a, b, c, d, e);63}64function mountOrderComparator(c1, c2) {65 return c1._mountOrder - c2._mountOrder;66}67function runBatchedUpdates(transaction) {68 var len = transaction.dirtyComponentsLength;69 invariant(len === dirtyComponents.length, 'Expected flush transaction\'s stored dirty-components length (%s) to ' + 'match dirty-components array length (%s).', len, dirtyComponents.length);70 dirtyComponents.sort(mountOrderComparator);71 updateBatchNumber++;72 for (var i = 0; i < len; i++) {73 var component = dirtyComponents[i];74 var callbacks = component._pendingCallbacks;75 component._pendingCallbacks = null;76 var markerName;77 if (ReactFeatureFlags.logTopLevelRenders) {78 var namedComponent = component;79 if (component._currentElement.type.isReactTopLevelWrapper) {80 namedComponent = component._renderedComponent;81 }82 markerName = 'React update: ' + namedComponent.getName();83 console.time(markerName);84 }85 ReactReconciler.performUpdateIfNecessary(component, transaction.reconcileTransaction, updateBatchNumber);86 if (markerName) {87 console.timeEnd(markerName);88 }89 if (callbacks) {90 for (var j = 0; j < callbacks.length; j++) {91 transaction.callbackQueue.enqueue(callbacks[j], component.getPublicInstance());92 }93 }94 }95}96var flushBatchedUpdates = function flushBatchedUpdates() {97 while (dirtyComponents.length || asapEnqueued) {98 if (dirtyComponents.length) {99 var transaction = ReactUpdatesFlushTransaction.getPooled();100 transaction.perform(runBatchedUpdates, null, transaction);101 ReactUpdatesFlushTransaction.release(transaction);102 }103 if (asapEnqueued) {104 asapEnqueued = false;105 var queue = asapCallbackQueue;106 asapCallbackQueue = CallbackQueue.getPooled();107 queue.notifyAll();108 CallbackQueue.release(queue);109 }110 }111};112function enqueueUpdate(component) {113 ensureInjected();114 if (!batchingStrategy.isBatchingUpdates) {115 batchingStrategy.batchedUpdates(enqueueUpdate, component);116 return;117 }118 dirtyComponents.push(component);119 if (component._updateBatchNumber == null) {120 component._updateBatchNumber = updateBatchNumber + 1;121 }122}123function asap(callback, context) {124 invariant(batchingStrategy.isBatchingUpdates, 'ReactUpdates.asap: Can\'t enqueue an asap callback in a context where' + 'updates are not being batched.');125 asapCallbackQueue.enqueue(callback, context);126 asapEnqueued = true;127}128var ReactUpdatesInjection = {129 injectReconcileTransaction: function injectReconcileTransaction(ReconcileTransaction) {130 invariant(ReconcileTransaction, 'ReactUpdates: must provide a reconcile transaction class');131 ReactUpdates.ReactReconcileTransaction = ReconcileTransaction;132 },133 injectBatchingStrategy: function injectBatchingStrategy(_batchingStrategy) {134 invariant(_batchingStrategy, 'ReactUpdates: must provide a batching strategy');135 invariant(typeof _batchingStrategy.batchedUpdates === 'function', 'ReactUpdates: must provide a batchedUpdates() function');136 invariant(typeof _batchingStrategy.isBatchingUpdates === 'boolean', 'ReactUpdates: must provide an isBatchingUpdates boolean attribute');137 batchingStrategy = _batchingStrategy;138 },139 getBatchingStrategy: function getBatchingStrategy() {140 return batchingStrategy;141 }142};143var ReactUpdates = {144 ReactReconcileTransaction: null,145 batchedUpdates: batchedUpdates,146 enqueueUpdate: enqueueUpdate,147 flushBatchedUpdates: flushBatchedUpdates,148 injection: ReactUpdatesInjection,149 asap: asap150};...
dragger.js
Source:dragger.js
1var Dragger = function(init)2{3 var default_init =4 {5 source:document.createElement('div')6 }7 var self = this;8 doMapInitDefaults(self,init,default_init);9 var draggables = [];10 var dragging = [];11 var callbackQueue = [];12 var evtQueue = [];13 var queues = {};14 queues.callbackQueue = callbackQueue;15 queues.evtQueue = evtQueue;16 self.register = function(draggable) { draggables.push(draggable); }17 self.unregister = function(draggable) { var i = draggables.indexOf(draggable); if(i != -1) draggables.splice(i,1); }18 self.ignore = function() { dragging = []; callbackQueue = []; evtQueue = []; }19 self.clear = function() { draggables = []; }20 self.attach = function() //will get auto-called on create21 {22 if(platform == "PC")23 {24 self.source.addEventListener('mousedown', begin, false);25 self.source.addEventListener('mousemove', drag, false);26 self.source.addEventListener('mouseup', end, false);27 window.addEventListener('mousemove', detectOut, false);28 }29 else if(platform == "MOBILE")30 {31 self.source.addEventListener('touchstart', begin, false);32 self.source.addEventListener('touchmove', drag, false);33 self.source.addEventListener('touchend', end, false);34 window.addEventListener('touchmove', detectOut, false);35 }36 }37 self.detach = function()38 {39 if(platform == "PC")40 {41 self.source.removeEventListener('mousedown', begin);42 self.source.removeEventListener('mousemove', drag);43 self.source.removeEventListener('mouseup', end);44 window.removeEventListener('mousemove', detectOut, false);45 }46 else if(platform == "MOBILE")47 {48 self.source.removeEventListener('touchstart', begin);49 self.source.removeEventListener('touchmove', drag);50 self.source.removeEventListener('touchend', end);51 window.removeEventListener('touchmove', detectOut, false);52 }53 }54 function begin(evt)55 {56 doSetPosOnEvent(evt);57 self.injectDragStart(evt);58 }59 self.injectDragStart = function(evt)60 {61 for(var i = 0; i < draggables.length; i++)62 {63 if(ptWithinObj(draggables[i], evt.doX, evt.doY))64 {65 var already_dragging = false;66 for(var j = 0; j < dragging.length; j++)67 if(draggables[i] == dragging[j]) already_dragging = true;68 if(!already_dragging)69 {70 dragging.push(draggables[i]);71 callbackQueue.push(draggables[i].dragStart);72 evtQueue.push(evt);73 }74 }75 }76 }77 function drag(evt)78 {79 doSetPosOnEvent(evt);80 self.injectDrag(evt);81 }82 self.injectDrag = function(evt)83 {84 var r = self.source.getBoundingClientRect();85 if(evt.clientX < r.left || evt.clientY < r.top || evt.clientX > r.right || evt.clientY > r.bottom)86 {87 end(evt);88 return;89 }90 for(var i = 0; i < dragging.length; i++)91 {92 callbackQueue.push(dragging[i].drag);93 evtQueue.push(evt);94 }95 }96 function end(evt)97 {98 doSetPosOnEvent(evt);99 self.injectDragFinish(evt);100 }101 self.injectDragFinish = function(evt)102 {103 for(var i = 0; i < dragging.length; i++)104 {105 callbackQueue.push(dragging[i].dragFinish);106 evtQueue.push(evt);107 }108 dragging = [];109 }110 self.flush = function()111 {112 for(var i = 0; i < callbackQueue.length; i++)113 callbackQueue[i](evtQueue[i]);114 callbackQueue = [];115 evtQueue = [];116 }117 self.requestManualFlush = function()118 {119 queues.callbackQueue = callbackQueue;120 queues.evtQueue = evtQueue;121 return queues;122 }123 self.manualFlush = function()124 {125 callbackQueue = [];126 evtQueue = [];127 }128 function detectOut(evt)129 {130 var r = self.source.getBoundingClientRect();131 if(evt.clientX < r.left || evt.clientY < r.top || evt.clientX > r.right || evt.clientY > r.bottom)132 end(evt);133 }134 self.attach();135}136//example draggable- just needs x,y,w,h and dragStart, drag, and dragFinish callback137var Draggable = function(args)138{139 var self = this;140 //nice in smooth dragging141 self.offX = 0;142 self.offY = 0;143 self.x = args.x ? args.x : 0;144 self.y = args.y ? args.y : 0;145 self.w = args.w ? args.w : 0;146 self.h = args.h ? args.h : 0;147 self.dragStart = args.dragStart ? args.dragStart : function(evt)148 {149 self.offX = evt.doX-self.x;150 self.offY = evt.doY-self.y;151 }152 self.drag = args.drag ? args.drag : function(evt)153 {154 self.deltaX = ((evt.doX-self.x)-self.offX);155 self.deltaY = ((evt.doY-self.y)-self.offY);156 self.x = self.x + self.deltaX;157 self.y = self.y + self.deltaY;158 self.offX = evt.doX - self.x;159 self.offY = evt.doY - self.y;160 }161 self.dragFinish = args.dragFinish ? args.dragFinish : function()162 {163 }164 //nice for debugging purposes165 self.draw = function(canv)166 {167 canv.context.strokeStyle = "#00FF00";168 canv.context.strokeRect(self.x,self.y,self.w,self.h);169 }...
presser.js
Source:presser.js
1var Presser = function(init)2{3 var default_init =4 {5 source:document.createElement('div')6 }7 var self = this;8 doMapInitDefaults(self,init,default_init);9 var pressables = [];10 var pressing = [];11 var callbackQueue = [];12 var evtQueue = [];13 var queues = {};14 queues.callbackQueue = callbackQueue;15 queues.evtQueue = evtQueue;16 var down = false;17 self.register = function(pressable) { pressables.push(pressable); }18 self.unregister = function(pressable) { var i = pressables.indexOf(pressable); if(i != -1) pressables.splice(i,1); }19 self.ignore = function() { callbackQueue = []; evtQueue = []; }20 self.clear = function() { pressables = []; }21 self.attach = function() //will auto-call on creation22 {23 if(platform == "PC")24 {25 self.source.addEventListener('mousedown', begin, false);26 self.source.addEventListener('mousemove', press, false);27 self.source.addEventListener('mouseup', end, false);28 }29 else if(platform == "MOBILE")30 {31 self.source.addEventListener('touchstart', begin, false);32 self.source.addEventListener('touchmove', press, false);33 self.source.addEventListener('touchend', end, false);34 }35 }36 self.detach = function()37 {38 if(platform == "PC")39 {40 self.source.removeEventListener('mousedown', begin);41 self.source.removeEventListener('mousemove', press);42 self.source.removeEventListener('mouseup', end);43 }44 else if(platform == "MOBILE")45 {46 self.source.removeEventListener('touchstart', begin);47 self.source.removeEventListener('touchmove', press);48 self.source.removeEventListener('touchend', end);49 }50 }51 function begin(evt)52 {53 down = true;54 doSetPosOnEvent(evt);55 self.injectPress(evt);56 }57 function press(evt)58 {59 if(!down) return;60 doSetPosOnEvent(evt);61 self.injectPress(evt);62 }63 self.injectPress = function(evt)64 {65 var alreadypressing;66 for(var i = 0; i < pressables.length; i++)67 {68 if(69 evt.doX >= pressables[i].x &&70 evt.doX <= pressables[i].x+pressables[i].w &&71 evt.doY >= pressables[i].y &&72 evt.doY <= pressables[i].y+pressables[i].h73 )74 {75 alreadypressing = false;76 for(var j = 0; j < pressing.length; j++)77 if(pressables[i] == pressing[j]) alreadypressing = true;78 if(!alreadypressing)79 {80 pressing.push(pressables[i]);81 callbackQueue.push(pressables[i].press);82 evtQueue.push(evt);83 }84 }85 }86 for(var i = 0; i < pressing.length; i++)87 {88 if(89 evt.doX < pressing[i].x ||90 evt.doX > pressing[i].x+pressing[i].w ||91 evt.doY < pressing[i].y ||92 evt.doY > pressing[i].y+pressing[i].h93 )94 {95 callbackQueue.push(pressing[i].unpress);96 evtQueue.push(evt);97 pressing.splice(i,1);98 i--;99 }100 }101 }102 function end(evt)103 {104 self.injectUnpress(evt);105 }106 self.injectUnpress = function(evt)107 {108 down = false;109 for(var i = 0; i < pressing.length; i++)110 {111 callbackQueue.push(pressing[i].unpress);112 evtQueue.push(evt);113 }114 pressing = [];115 }116 self.flush = function()117 {118 for(var i = 0; i < callbackQueue.length; i++)119 callbackQueue[i](evtQueue[i]);120 callbackQueue = [];121 evtQueue = [];122 }123 self.requestManualFlush = function()124 {125 queues.callbackQueue = callbackQueue;126 queues.evtQueue = evtQueue;127 return queues;128 }129 self.manualFlush = function()130 {131 callbackQueue = [];132 evtQueue = [];133 }134 self.attach();135}136//example pressable- just needs x,y,w,h and press callback137var Pressable = function(args)138{139 var self = this;140 self.x = args.x ? args.x : 0;141 self.y = args.y ? args.y : 0;142 self.w = args.w ? args.w : 0;143 self.h = args.h ? args.h : 0;144 self.press = args.press ? args.press : function(evt){ };145 self.unpress = args.unpress ? args.unpress : function(evt){ };146 //nice for debugging purposes147 self.draw = function(canv)148 {149 canv.context.strokeStyle = "#00FF00";150 canv.context.strokeRect(self.x,self.y,self.w,self.h);151 }...
renderer.js
Source:renderer.js
1/*2 * This file is part of gorilla-repl. Copyright (C) 2014-, Jony Hudson.3 *4 * gorilla-repl is licenced to you under the MIT licence. See the file LICENCE.txt for full details.5 */6/* Takes a data structure representing the output data and renders it in to the given element. */7var render = function (data, element, errorCallback) {8 // Some parts of the output might need to run js functions to complete the rendering (like Vega graphs for instance)9 // We maintain a list of those functions that we accumulate as we put together the HTML, and then call them all10 // after the HTML has been inserted into the document.11 var callbackQueue = [];12 var htmlString = renderPart(data, callbackQueue, errorCallback);13 $(element).html("<pre>" + htmlString + "</pre>");14 _.each(callbackQueue, function (callback) {callback()});15 // Attach a click event handler to each element for value copy and paste.16 $(".value", element).click(function (ed) {17 if (ed.altKey) {18 var value = $(this).attr('data-value');19 eventBus.trigger("app:show-value", value);20 }21 return false;22 });23};24var renderPart = function (data, callbackQueue, errorCallback) {25 switch (data.type) {26 case "html":27 return renderHTML(data, callbackQueue, errorCallback);28 case "list-like":29 return renderListLike(data, callbackQueue, errorCallback);30 case "vega":31 return renderVega(data, callbackQueue, errorCallback);32 case "latex":33 return renderLatex(data, callbackQueue, errorCallback);34 }35 return "Unknown render type";36};37// This helper supports value copy and paste.38var wrapWithValue = function (data, content) {39 return "<span class='value' data-value='" + _.escape(data.value) + "'>" + content + "</span>";40};41var renderHTML = function (data, callbackQueue, errorCallback) {42 return wrapWithValue(data, data.content);43};44var renderListLike = function (data, callbackQueue, errorCallback) {45 // first of all render the items46 var renderedItems = data.items.map(function (x) {return renderPart(x, callbackQueue, errorCallback)});47 // and then assemble the list48 return wrapWithValue(data, data.open + renderedItems.join(data.separator) + data.close);49};50var renderVega = function (data, callbackQueue, errorCallback) {51 var uuid = UUID.generate();52 // for some reason, Vega will sometimes try and pop up an alert if there's an error, which is not a53 // great user experience. Here we patch the error handling function to re-route any generated message54 // to the segment.55 vg.error = function (msg) {56 errorCallback("Vega error (js): " + msg);57 };58 callbackQueue.push(function () {59 vg.parse.spec(data.content, function (chart) {60 try {61 var element = $("#" + uuid).get()[0];62 chart({el: element, renderer: 'svg'}).update();63 } catch (e) {64 // we'll end up here if vega throws an error. We try and route this error back to the65 // segment so the user has an idea of what's going on.66 errorCallback("Vega error (js): " + e.message);67 }68 });69 });70 return wrapWithValue(data, "<span class='vega-span' id='" + uuid + "'></span>");71};72var renderLatex = function (data, callbackQueue, errorCallback) {73 var uuid = UUID.generate();74 callbackQueue.push(function () {75 MathJax.Hub.Queue(["Typeset", MathJax.Hub, uuid]);76 });77 return wrapWithValue(data, "<span class='latex-span' id='" + uuid + "'>@@" + data.content + "@@</span>");...
CallbackQueue.js
Source:CallbackQueue.js
...24 * @implements PooledClass25 * @internal26 */27var CallbackQueue = function () {28 function CallbackQueue(arg) {29 _classCallCheck(this, CallbackQueue);30 this._callbacks = null;31 this._contexts = null;32 this._arg = arg;33 }34 /**35 * Enqueues a callback to be invoked when `notifyAll` is invoked.36 *37 * @param {function} callback Invoked when `notifyAll` is invoked.38 * @param {?object} context Context to call `callback` with.39 * @internal40 */41 CallbackQueue.prototype.enqueue = function enqueue(callback, context) {42 this._callbacks = this._callbacks || [];...
clicker.js
Source:clicker.js
1var Clicker = function(init)2{3 var default_init =4 {5 source:document.createElement('div')6 }7 var self = this;8 doMapInitDefaults(self,init,default_init);9 var clickables = [];10 var callbackQueue = [];11 var evtQueue = [];12 var queues = {};13 queues.callbackQueue = callbackQueue;14 queues.evtQueue = evtQueue;15 self.register = function(clickable) { clickables.push(clickable); }16 self.unregister = function(clickable) { var i = clickables.indexOf(clickable); if(i != -1) clickables.splice(i,1); }17 self.ignore = function() { callbackQueue = []; evtQueue = []; }18 self.clear = function() { clickables = []; }19 self.attach = function() //will get auto-called at creation20 {21 if(platform == "PC") self.source.addEventListener('mousedown', click, false);22 else if(platform == "MOBILE") self.source.addEventListener('touchstart', click, false);23 }24 self.detach = function()25 {26 if(platform == "PC") self.source.removeEventListener('mousedown', click);27 else if(platform == "MOBILE") self.source.removeEventListener('touchstart', click);28 }29 function click(evt)30 {31 doSetPosOnEvent(evt);32 self.injectClick(evt);33 }34 self.injectClick = function(evt)35 {36 for(var i = 0; i < clickables.length; i++)37 {38 if(clicked(clickables[i], evt))39 {40 callbackQueue.push(clickables[i].click);41 evtQueue.push(evt);42 }43 }44 }45 self.flush = function()46 {47 for(var i = 0; i < callbackQueue.length; i++)48 callbackQueue[i](evtQueue[i]);49 callbackQueue = [];50 evtQueue = [];51 }52 self.requestManualFlush = function()53 {54 queues.callbackQueue = callbackQueue;55 queues.evtQueue = evtQueue;56 return queues;57 }58 self.manualFlush = function()59 {60 callbackQueue = [];61 evtQueue = [];62 }63 self.attach();64}65var clicked = function(clickable, evt)66{67 return ptWithinObj(clickable, evt.doX, evt.doY);68}69//example clickable- just needs x,y,w,h and click callback70var Clickable = function(args)71{72 var self = this;73 self.x = args.x ? args.x : 0;74 self.y = args.y ? args.y : 0;75 self.w = args.w ? args.w : 0;76 self.h = args.h ? args.h : 0;77 self.click = args.click ? args.click : function(){};78 //nice for debugging purposes79 self.draw = function(canv)80 {81 canv.context.strokeStyle = "#00FF00";82 canv.context.strokeRect(self.x,self.y,self.w,self.h);83 }...
Readyable.js
Source:Readyable.js
1// A base class that has an onReady() function.2var Readyable = (function() {3 function Readyable() {4 this.ready = false;5 this.callbackQueue = [];6 this.error = null;7 return this;8 }9 Readyable.prototype.isReady = function() {10 return this.ready;11 }12 Readyable.prototype.onReady = function(callback) {13 if (this.error) { callback(this.error, null); return; }14 if (this.ready) { callback(null, this); return; }15 this.callbackQueue.push(callback);16 };17 Readyable.prototype.setReady = function() {18 this.ready = true;19 this.error = null;20 for (var i in this.callbackQueue) {21 var callback = this.callbackQueue[i];22 callback(null, this)23 }24 this.callbackQueue = [];25 }26 Readyable.prototype.setError = function(err) {27 this.ready = false;28 if (typeof err == 'string') { this.error = new Error(err); }29 else { this.error = err; }30 for (var i in this.callbackQueue) {31 var callback = this.callbackQueue[i];32 callback(this.error, null);33 }34 this.callbackQueue = [];35 }36 return Readyable;37})();38// for use in Node:39if (typeof module == 'object') {40 module.exports = Readyable;41 module.exports.default = Readyable;...
Using AI Code Generation
1const { chromium } = require('playwright');2const { CallbackQueue } = require('playwright/lib/client/callbacks');3(async () => {4 const browser = await chromium.launch();5 const page = await browser.newPage();6 const callbackQueue = new CallbackQueue();7 const context = await browser.newContext({ callbackQueue });8 const page = await context.newPage();9 await callbackQueue.drain();10 await browser.close();11})();12const { chromium } = require('playwright');13const { CallbackQueue } = require('playwright/lib/client/callbacks');14(async () => {15 const browser = await chromium.launch();16 const page = await browser.newPage();17 const context = await browser.newContext();18 const page = await context.newPage();19 await context.drain();20 await browser.close();21})();22const { chromium } = require('playwright');23const { CallbackQueue } = require('playwright/lib/client/callbacks');24(async () => {25 const browser = await chromium.launch();26 const page = await browser.newPage();27 const context = await browser.newContext();28 const page = await context.newPage();29 await page.drain();30 await browser.close();31})();32const { chromium } = require('playwright');33const { CallbackQueue } = require('playwright/lib/client/callbacks');34(async () => {35 const browser = await chromium.launch();36 const page = await browser.newPage();37 const context = await browser.newContext();38 const page = await context.newPage();39 await browser.drain();40 await browser.close();41})();42const { chromium } = require('playwright');43const { CallbackQueue } = require('playwright/lib/client/callbacks');44(async () => {45 const browser = await chromium.launch();46 const page = await browser.newPage();47 const context = await browser.newContext();48 const page = await context.newPage();
Using AI Code Generation
1(async () => {2 const {chromium, devices} = require('playwright');3 const browser = await chromium.launch();4 const context = await browser.newContext(devices['iPhone 11']);5 const page = await context.newPage();6 const callbackQueue = new CallbackQueue(page);
Using AI Code Generation
1const { CallbackQueue } = require('playwright/lib/utils/callbacks');2const { chromium } = require('playwright');3const fs = require('fs');4const path = require('path');5(async () => {6 const browser = await chromium.launch();7 const context = await browser.newContext();8 const page = await context.newPage();9 await page.waitForSelector('input');10 await page.type('input', 'playwright');11 await page.screenshot({ path: 'google.png' });12 await browser.close();13})();14const { CallbackQueue } = require('playwright/lib/utils/callbacks');15const { chromium } = require('playwright');16const fs = require('fs');17const path = require('path');18(async () => {19 const browser = await chromium.launch();20 const context = await browser.newContext();21 const page = await context.newPage();22 await page.waitForSelector('input');23 await page.type('input', 'playwright');24 await page.screenshot({ path: 'google.png' });25 await browser.close();26})();27const { CallbackQueue } = require('playwright/lib/utils/callbacks');28const { chromium } = require('playwright');29const fs = require('fs');30const path = require('path');31(async () => {32 const browser = await chromium.launch();33 const context = await browser.newContext();34 const page = await context.newPage();35 await page.waitForSelector('input');36 await page.type('input', 'playwright');37 await page.screenshot({ path: 'google.png' });38 await browser.close();39})();40const { CallbackQueue } = require('playwright/lib/utils/callbacks');41const { chromium } = require('playwright');42const fs = require('fs');43const path = require('path');44(async () => {45 const browser = await chromium.launch();46 const context = await browser.newContext();47 const page = await context.newPage();48 await page.waitForSelector('input');49 await page.type('input', 'playwright');50 await page.screenshot({ path: 'google.png' });51 await browser.close();52})();
Using AI Code Generation
1const {CallbackQueue} = require('playwright/lib/utils/callbacks');2const queue = new CallbackQueue();3queue.add(async () => {4 console.log('1');5});6queue.add(async () => {7 console.log('2');8});9queue.runAll();10const {CallbackQueue} = require('playwright/lib/utils/callbacks');11const queue = new CallbackQueue();12queue.add(async () => {13 console.log('1');14});15queue.add(async () => {16 console.log('2');17});18queue.runAll();19const {CallbackQueue} = require('playwright/lib/utils/callbacks');20const queue = new CallbackQueue();21queue.add(async () => {22 console.log('1');23});24queue.add(async () => {25 console.log('2');26});27queue.runAll();28const {CallbackQueue} = require('playwright/lib/utils/callbacks');29const queue = new CallbackQueue();30queue.add(async () => {31 console.log('1');32});33queue.add(async () => {34 console.log('2');35});36queue.runAll();37const {CallbackQueue} = require('playwright/lib/utils/callbacks');38const queue = new CallbackQueue();39queue.add(async () => {40 console.log('1');41});42queue.add(async () => {43 console.log('2');44});45queue.runAll();46const {CallbackQueue} = require('playwright/lib/utils/callbacks');47const queue = new CallbackQueue();48queue.add(async () => {49 console.log('1');50});51queue.add(async () => {52 console.log('2');53});54queue.runAll();55const {CallbackQueue} = require('playwright/lib/utils/callbacks');56const queue = new CallbackQueue();
Using AI Code Generation
1const { chromium } = require('playwright');2const { CallbackQueue } = require('playwright/lib/utils/callbacks');3const { createReadStream } = require('fs');4(async () => {5 const browser = await chromium.launch();6 const context = await browser.newContext();7 const page = await context.newPage();8 const callbackQueue = new CallbackQueue();9 const stream = createReadStream('test.pdf');10 await page.pdf({11 margin: {12 },13 });14 await page.pdf({15 margin: {16 },17 });18 await page.pdf({19 margin: {20 },21 });22 await page.pdf({23 margin: {
Using AI Code Generation
1const { CallbackQueue } = require('playwright-core/lib/server/callbacks');2const queue = new CallbackQueue();3queue.onIdle().then(() => console.log('All done'));4queue.postTask(() => console.log('First'));5queue.postTask(() => console.log('Second'));6queue.postTask(() => console.log('Third'));7queue.postTask(() => console.log('Fourth'));8const { CallbackQueue } = require('playwright-core/lib/server/callbacks');9const queue = new CallbackQueue();10queue.onIdle().then(() => console.log('All done'));11queue.postTask(() => {12 console.log('First');13 queue.postTask(() => console.log('Second'));14 queue.postTask(() => console.log('Third'));15 queue.postTask(() => console.log('Fourth'));16});17const { CallbackQueue } = require('playwright-core/lib/server/callbacks');18const queue = new CallbackQueue();19queue.onIdle().then(() => console.log('All done'));20queue.postTask(() => {21 console.log('First');22 queue.postTask(() => console.log('Second'));23 queue.postTask(() => console.log('Third'));24 queue.postTask(() => console.log('Fourth'));25});26queue.postTask(() => console.log('Fifth'));27queue.postTask(() => console.log('Sixth'));28const { CallbackQueue } = require('playwright-core/lib/server/callbacks');29const queue = new CallbackQueue();30queue.onIdle().then(() => console.log('All done'));31queue.postTask(() => {32 console.log('First');33 queue.postTask(() => console.log('Second'));34 queue.postTask(() => console.log('Third'));35 queue.postTask(() => console.log('Fourth'));36});37queue.postTask(() => console.log('Fifth'));38queue.postTask(() => console.log('Sixth'));39queue.postTask(() => {40 console.log('Se
Using AI Code Generation
1const { CallbackQueue } = require('playwright/lib/utils/callbacks');2const { chromium } = require('playwright');3const callbackQueue = new CallbackQueue();4callbackQueue.enqueue(() => {5 return new Promise((resolve, reject) => {6 setTimeout(() => {7 resolve('hello world');8 }, 1000);9 });10});11callbackQueue.enqueue(() => {12 return new Promise((resolve, reject) => {13 setTimeout(() => {14 resolve('hello world2');15 }, 1000);16 });17});18(async () => {19 const browser = await chromium.launch();20 const context = await browser.newContext();21 const page = await context.newPage();22 await page.evaluate(() => {23 console.log('hello world');24 });25 await page.evaluate(() => {26 console.log('hello world2');27 });28 await page.evaluate(() => {29 console.log('hello world3');30 });31 await page.evaluate(() => {32 console.log('hello world4');33 });34 await page.evaluate(() => {35 console.log('hello world5');36 });37 await page.evaluate(() => {38 console.log('hello world6');39 });40 await page.evaluate(() => {41 console.log('hello world7');42 });43 await page.evaluate(() => {44 console.log('hello world8');45 });46 await page.evaluate(() => {47 console.log('hello world9');48 });49 await page.evaluate(() => {50 console.log('hello world10');51 });52 await page.evaluate(() => {53 console.log('hello world11');54 });55 await page.evaluate(() => {56 console.log('hello world12');57 });58 await page.evaluate(() => {59 console.log('hello world13');60 });61 await page.evaluate(() => {62 console.log('hello world14');63 });64 await page.evaluate(() => {65 console.log('hello world15');66 });67 await page.evaluate(() => {68 console.log('hello world16');69 });70 await page.evaluate(() => {71 console.log('hello world17');72 });73 await page.evaluate(() => {74 console.log('hello world18');75 });76 await page.evaluate(() => {77 console.log('hello world19');78 });79 await page.evaluate(() => {80 console.log('hello world20');81 });82 await page.evaluate(() => {
Using AI Code Generation
1const { CallbackQueue, Page } = require('playwright');2const { chromium } = require('playwright-chromium');3const callbackQueue = new CallbackQueue();4(async () => {5 const browser = await chromium.launch({ headless: false });6 const page = await browser.newPage();7 const callback = callbackQueue.newCallback();8 page.on('load', callback);9 await callbackQueue.waitForCallback();10 await browser.close();11})();12const { CallbackQueue, Page } = require('playwright');13const { chromium } = require('playwright-chromium');14const callbackQueue = new CallbackQueue();15(async () => {16 const browser = await chromium.launch({ headless: false });17 const page = await browser.newPage();18 const callback = callbackQueue.newCallback();19 page.on('load', callback);20 await callbackQueue.waitForCallback();21 await browser.close();22})();23const { CallbackQueue, Page } = require('playwright');24const { chromium } = require('playwright-chromium');25const callbackQueue = new CallbackQueue();26(async () => {27 const browser = await chromium.launch({ headless: false });28 const page = await browser.newPage();29 const callback = callbackQueue.newCallback();30 page.on('load', callback);
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!