Best JavaScript code snippet using wpt
ConnectionService.js
Source:ConnectionService.js
2 function HttpConnection(constructorBaseURL, constructorTarget) {3 var xmlHttp;4 var baseURL = processBaseURL(constructorBaseURL);5 var thisID = 0;6 var idPromise = createPromiseCapability();7 var hasCloseSignal = false;8 var mDataCallback = null;9 var reqCounter = 0;10 function processBaseURL(inBaseURL) {11 var end = inBaseURL.lastIndexOf('/');12 if (end < 0) {13 end = inBaseURL.length;14 }15 return inBaseURL.slice(0, end);16 }17 function send(inData) {18 idPromiseFunc().then(function(connectionID) {19 var xmlHttp = new XMLHttpRequest();20 var url = baseURL + constructorTarget + '?id=' + connectionID;21 var formData = new FormData();22 formData.append('data', JSON.stringify(inData));23 xmlHttp.open('POST', url);24 xmlHttp.withCredentials = true;25 xmlHttp.send(formData);26 });27 }28 function idPromiseFunc() {29 if (idPromise) {30 return idPromise.promise;31 }32 return Promise.resolve(thisID);33 }34 function close() {35 resetID();36 hasCloseSignal = true;37 xmlHttp.close();38 }39 function dataToJsonChunks(inputJson) {40 var splitStrings = inputJson.split('\n');41 var last = splitStrings[splitStrings.length - 1];42 if (last) {43 splitStrings.pop();44 }45 // get rid of the trailing ]46 while (splitStrings.length > 0 && splitStrings[splitStrings.length - 1].length < 3) {47 if (splitStrings.pop() === ']') {48 resetID();49 }50 }51 // or the leading [52 if (splitStrings.length > 0 && splitStrings[0].length < 3) {53 splitStrings.shift();54 }55 for (var i = 0; i < splitStrings.length; ++i) {56 if (splitStrings[i].endsWith(',')) {57 splitStrings[i] = splitStrings[i].substr(0, splitStrings[i].length - 1);58 }59 }60 return splitStrings;61 }62 function processStrings(stringArray) {63 var data = null;64 for (var i = 0; i < stringArray.length; ++i) {65 data = JSON.parse(stringArray[i]);66 if (data && data['end']) {67 close();68 } else if (data && data['hb'] && data['id'] === thisID) {69 send({ 'hb': true });70 } else {71 mDataCallback(data);72 }73 }74 }75 function resetID() {76 thisID = 0;77 if (!idPromise) {78 idPromise = createPromiseCapability();79 }80 }81 function resolveID(data) {82 thisID = data['id'];83 var prom = idPromise;84 idPromise = null;85 prom.resolve(thisID);86 }87 function onReadyStateChange(request, requestContext) {88 var thisSize;89 if (request.readyState >= 3 && !requestContext.done) {90 try {91 thisSize = request.responseText.length;92 } catch (e) {93 console.log('caught exception');94 // send({t:"ignore", except:true});95 return;96 }97 if (thisSize > 0) {98 // send({t:"ignore", state:request.readyState, size:thisSize});99 try {100 var stringArray = dataToJsonChunks(request.responseText);101 if (thisID === 0 && stringArray.length > 0) {102 resolveID(JSON.parse(stringArray.shift()));103 }104 processStrings(stringArray);105 // send({t:"ignore", processed:true});106 } catch (e) {107 // send({t:"ignore", processex:e});108 }109 }110 if (!hasCloseSignal) {111 requestContext.done = true;112 // send({t:"ignore", requestContext:requestContext});113 fireOffRequest();114 } else {115 // send({t:"ignore", closed:true});116 mCloseCallback();117 console.log('dead');118 }119 }120 }121 function fireOffRequest() {122 xmlHttp = new XMLHttpRequest();123 var url = baseURL + constructorTarget;124 if (thisID !== 0) {125 url += '?id=' + thisID + '&uc=' + reqCounter;126 } else {127 url += '?uc=' + reqCounter;128 }129 reqCounter++;130 xmlHttp.open('GET', url, true);131 xmlHttp.setRequestHeader('Cache-Control', 'no-cache');132 xmlHttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');133 xmlHttp.withCredentials = true;134 var currentRequest = xmlHttp;135 var requestContext = {136 done: false137 };138 xmlHttp.onreadystatechange = function() {139 onReadyStateChange(currentRequest, requestContext);140 };141 xmlHttp.send();142 }143 function startConnection(openCallback, dataCallback, closeCallback) {144 mDataCallback = dataCallback;145 mCloseCallback = closeCallback;146 hasCloseSignal = false;147 resetID();148 fireOffRequest();149 openCallback();150 return Promise.resolve();151 }152 return {153 send: send,154 startConnection: startConnection,155 close: close156 };157 }158 function WebsocketConnection(constructorBaseURL, constructorTarget) {159 var wsPromise = createPromiseCapability();160 var wsClosePromise = null;161 var baseURL = wsBaseURL(constructorBaseURL);162 var mDataCallback = null;163 var mConnectionClosed = false;164 var mHasError = false;165 function wsBaseURL(inBaseURL) {166 var start = inBaseURL.indexOf('://');167 var protocol = 'ws://';168 if (start < 0) {169 start = 0;170 } else {171 if (start === 5) {172 protocol = 'wss://';173 }174 start += 3;175 }176 var end = inBaseURL.lastIndexOf('/');177 if (end < 0) {178 end = inBaseURL.length;179 }180 return protocol + inBaseURL.slice(start, end);181 }182 function send(jsObject) {183 getWSPromise().then(function(ws) {184 if (mConnectionClosed && !mHasError) {185 setTimeout(function() {186 send(jsObject);187 }, 1);188 } else if (mConnectionClosed) {189 ws.send(JSON.stringify(jsObject));190 } else {191 ws.send(JSON.stringify(jsObject));192 }193 });194 }195 function startConnection(onOpen, onMessage, onClose) {196 var startRet = createPromiseCapability();197 var outerRet = startRet;198 mDataCallback = onMessage;199 try {200 var initWS = new WebSocket(joinPaths(baseURL + constructorTarget));201 initWS.onopen = function() {202 startRet.resolve();203 startRet = null;204 mConnectionClosed = false;205 onOpen();206 wsPromise.resolve(initWS);207 };208 initWS.onerror = function(err) {209 mHasError = true;210 mConnectionClosed = true;211 if (startRet) {212 startRet.reject(err);213 }214 if (wsClosePromise) {215 wsClosePromise.reject();216 }217 };218 initWS.onclose = function() {219 wsPromise = createPromiseCapability();220 mConnectionClosed = true;221 onClose();222 if (wsClosePromise) {223 var ret = wsClosePromise;224 wsClosePromise = null;225 ret.resolve();226 }227 };228 initWS.onmessage = function(message) {229 if (message && message.data) {230 var json = JSON.parse(message.data);231 if (json['hb']) {232 send({ 'hb': true });233 } else if (json['end']) {234 close();235 } else {236 mDataCallback(json);237 }238 }239 };240 } catch (e) {241 startRet.reject(e);242 startRet = null;243 }244 return outerRet.promise;245 }246 function getWSPromise() {247 if (mConnectionClosed && mDataCallback) {248 startConnection(mDataCallback);249 }250 return wsPromise.promise;251 }252 function close() {253 return getWSPromise().then(function(ws) {254 wsClosePromise = createPromiseCapability();255 ws.close();256 return wsClosePromise.promise;257 });258 }259 return {260 send: send,261 startConnection: startConnection,262 close: close263 };264 }265 if (window.WebSocket) {266 return WebsocketConnection(outerUrl, outerTarget);267 }268 return HttpConnection(outerUrl, outerTarget);...
storage_controller.js
Source:storage_controller.js
...34 * Create a new document with the given metadata.35 * @return { Promise }36 */37 post() {38 let postCapability = createPromiseCapability();39 this._storageInstance.post({ title: 'enclosure' }).push((id) => {40 this._storageID = id;41 return postCapability.resolve();42 }).push(undefined, function(error) {43 return postCapability.reject(error);44 });45 return postCapability.promise;46 }47 /**48 * Get metadata from metadata store.49 * @return { Promise }50 */51 getMetaData() {52 let getMetadataCapability = createPromiseCapability();53 this._storageInstance.get(this._storageID).push(function(metadata) {54 return getMetadataCapability.resolve(metadata);55 }).push(undefined, function(error) {56 return getMetadataCapability.reject(error);57 });58 return getMetadataCapability.promise;59 }60 /**61 * Put given data in `Metadata` store of IDB.62 * @return { Promise }63 */64 put() {65 let putCapability = createPromiseCapability();66 this._storageInstance.put(this._storageID, {67 name: this._fileName,68 size: this._fileSize,69 type: this._fileType,70 lastModifiedDate: this._fileLastModifiedDate,71 }).push(function() {72 return putCapability.resolve();73 }).push(undefined, function(error) {74 return putCapability.reject(error);75 });76 return putCapability.promise;77 }78 /**79 * Put attachment in `Attachment` store of IDB.80 * @return { Promise }81 */82 putAttachment() {83 let putAttachmentCapability = createPromiseCapability();84 this._storageInstance.putAttachment(this._storageID, 'enclosure',85 this._fileData).push(function() {86 return putAttachmentCapability.resolve();87 }).push(undefined, function(error) {88 return putAttachmentCapability.reject(error);89 });90 return putAttachmentCapability.promise;91 }92 /**93 * Get attachment from `Attachment` store of IDB.94 * @return { Promise }95 */96 getAttachment({ start, end }) {97 let getCapability = createPromiseCapability();98 this._storageInstance.getAttachment(this._storageID, 'enclosure',99 { start, end }).push(function(data) {100 return getCapability.resolve(data);101 }).push(undefined, function(error) {102 return getCapability.reject(error);103 });104 return getCapability.promise;105 }106 /**107 * Cancel StorageController.108 */109 cancel() {110 }111}
stream_controller.js
Source:stream_controller.js
...10 */11 constructor(storageCtrl) {12 this.CHUNK_SIZE = 10000;13 this._index = 0;14 this._readyCapability = createPromiseCapability();15 this._storageCtrl = storageCtrl;16 this._storageCtrl.post().then(() => {17 return this._storageCtrl.put();18 }).then(() => {19 return this._storageCtrl.putAttachment();20 }).then(() => {21 return this._readyCapability.resolve();22 }).catch(function(error) {23 return error;24 });25 }26 /**27 * Read a small chunk of data from IDB storage.28 * @return { Promise } A promise resolve with a { Blob } blob.29 */30 getChunk() {31 var chunkCapability = createPromiseCapability();32 this._readyCapability.promise.then(() => {33 this._storageCtrl.getAttachment({34 start: this._index,35 end: this._index + this.CHUNK_SIZE,36 }).then((blob) => {37 this._index += this.CHUNK_SIZE;38 return RSVP.Queue().push(function() {39 return jIO.util.readBlobAsArrayBuffer(blob);40 }).push(function(evt) {41 return chunkCapability.resolve(evt.target.result);42 }).push(undefined, function(error) {43 return chunkCapability.reject(error);44 });45 }).catch(function(error) {...
Using AI Code Generation
1var wptools = require( 'wptools' );2var promiseCapability = wptools.createPromiseCapability();3promiseCapability.promise.then( function ( data ) {4 console.log( data );5} );6promiseCapability.resolve( 'Hello World' );
Using AI Code Generation
1const {createPromiseCapability} = wpt;2const {createPromiseCapability} = wpt;3const {createPromiseCapability} = wpt;4const {createPromiseCapability} = wpt;5const {createPromiseCapability} = wpt;6const {createPromiseCapability} = wpt;7const {createPromiseCapability} = wpt;8const {createPromiseCapability} = wpt;9const {createPromiseCapability} = wpt;10const {createPromiseCapability} = wpt;11const {createPromiseCapability} = wpt;12const {createPromiseCapability} = wpt;13const {createPromiseCapability} = wpt;14const {createPromiseCapability} = wpt;15const {createPromiseCapability} = wpt;16const {createPromiseCapability} = wpt;17const {createPromiseCapability} = wpt;18const {createPromiseCapability} = wpt;19const {createPromiseCapability} = wpt;20const {createPromiseCapability} = wpt;
Using AI Code Generation
1var wptools = require('wptools');2var promiseCapability = wptools.createPromiseCapability();3setTimeout(function(){4 promiseCapability.resolve(1);5}, 1000);6promiseCapability.promise.then(function(value){7 console.log(value);8});9var wptools = require('wptools');10var promiseCapability = wptools.createPromiseCapability();11setTimeout(function(){12 promiseCapability.reject(1);13}, 1000);14promiseCapability.promise.then(function(value){15 console.log(value);16}, function(reason){17 console.log(reason);18});19var wptools = require('wptools');20var promiseCapability = wptools.createPromiseCapability();21setTimeout(function(){22 promiseCapability.resolve(1);23}, 1000);24promiseCapability.promise.then(function(value){25 console.log(value);26 return 2;27}).then(function(value){28 console.log(value);29});30var wptools = require('wptools');31var promiseCapability = wptools.createPromiseCapability();32setTimeout(function(){33 promiseCapability.resolve(1);34}, 1000);35promiseCapability.promise.then(function(value){36 console.log(value);37 return 2;38}).then(function(value){39 console.log(value);40 throw new Error('error');41}).catch(function(reason){42 console.log(reason);43});44var wptools = require('wptools');45var promiseCapability = wptools.createPromiseCapability();46setTimeout(function(){47 promiseCapability.resolve(1);48}, 1000);49promiseCapability.promise.then(function(value){50 console.log(value);51 return 2;52}).then(function(value){53 console.log(value);54 throw new Error('error');55}).catch(function(reason){56 console.log(reason);57 return 3;58}).then(function(value){59 console.log(value);60});61var wptools = require('wptools');62var promiseCapability = wptools.createPromiseCapability();63setTimeout(function(){64 promiseCapability.resolve(1);65}, 1000);66promiseCapability.promise.then(function(value){
Using AI Code Generation
1var createPromiseCapability = $262.createPromiseCapability;2var promiseCapability = createPromiseCapability();3var promise = promiseCapability.promise;4promiseCapability.resolve("foo");5promise.then(function (value) {6 $262.agent.report(value);7 $262.agent.leaving();8});
Using AI Code Generation
1var wpt = require('webpage');2var page = wpt.create();3var promise = page.createPromiseCapability();4promise.then(function(result) {5 console.log(result);6}, function(error) {7 console.log(error);8});9page.evaluate(function() {10 return new Promise(function(resolve, reject) {11 setTimeout(function() {12 resolve("this is resolved");13 }, 1000);14 });15});16page.evaluateAsync(function) → Promise17var wpt = require('webpage');18var page = wpt.create();19page.evaluateAsync(function() {20 return new Promise(function(resolve, reject) {21 setTimeout(function() {22 resolve("this is resolved");23 }, 1000);24 });25}).then(function(result) {26 console.log(result);27}, function(error) {28 console.log(error);29});30page.injectJs(filePath) → boolean31var wpt = require('webpage');32var page = wpt.create();33page.injectJs('test.js');34page.includeJs(filePath) → Promise35var wpt = require('webpage');36var page = wpt.create();37page.includeJs('test.js').then(function(result) {38 console.log(result);39}, function(error) {40 console.log(error);41});
Using AI Code Generation
1var promise = new Promise(function(resolve, reject) {2 resolve('done');3});4promise.then(function(value) {5 console.log(value);6});7var promise = new Promise(function(resolve, reject) {8 resolve('done');9});10promise.then(function(value) {11 console.log(value);12});13var promise = new Promise(function(resolve, reject) {14 resolve('done');15});16promise.then(function(value) {17 console.log(value);18});19var promise = new Promise(function(resolve, reject) {20 resolve('done');21});22promise.then(function(value) {23 console.log(value);24});25var promise = new Promise(function(resolve, reject) {26 resolve('done');27});28promise.then(function(value) {29 console.log(value);30});31var promise = new Promise(function(resolve, reject) {32 resolve('done');33});34promise.then(function(value) {35 console.log(value);36});37var promise = new Promise(function(resolve, reject) {38 resolve('done');39});40promise.then(function(value) {41 console.log(value);42});43var promise = new Promise(function(resolve, reject) {44 resolve('done');45});46promise.then(function(value) {47 console.log(value);48});49var promise = new Promise(function(resolve, reject) {50 resolve('done');51});52promise.then(function(value) {53 console.log(value);54});55var promise = new Promise(function(resolve, reject) {56 resolve('done');57});58promise.then(function(value) {59 console.log(value);60});61var promise = new Promise(function(resolve, reject) {62 resolve('done');63});64promise.then(function(value) {65 console.log(value);
Using AI Code Generation
1var capability = createPromiseCapability();2capability.promise.then(function(result) {3}, function(error) {4});5capability.resolve(result);6capability.reject(error);7var createPromiseCapability = function() {8 var capability = {};9 capability.promise = new Promise(function(resolve, reject) {10 capability.resolve = resolve;11 capability.reject = reject;12 });13 return capability;14};
Using AI Code Generation
1var wpt = require('wpt');2var promise = wpt.createPromiseCapability();3promise.promise.then(function(result) {4 console.log(result);5});6promise.resolve('Hello World');7var wpt = require('wpt');8var promise = wpt.createPromiseCapability();9promise.promise.then(function(result) {10 console.log(result);11});12promise.resolve('Hello World');13var wpt = require('wpt');14var promise = wpt.createPromiseCapability();15promise.promise.then(function(result) {16 console.log(result);17});18promise.resolve('Hello World');19var wpt = require('wpt');20var promise = wpt.createPromiseCapability();21promise.promise.then(function(result) {22 console.log(result);23});24promise.resolve('Hello World');25var wpt = require('wpt');26var promise = wpt.createPromiseCapability();27promise.promise.then(function(result) {28 console.log(result);29});30promise.resolve('Hello World');31var wpt = require('wpt');32var promise = wpt.createPromiseCapability();33promise.promise.then(function(result) {34 console.log(result);35});36promise.resolve('Hello World');37var wpt = require('wpt');38var promise = wpt.createPromiseCapability();39promise.promise.then(function(result) {40 console.log(result);41});42promise.resolve('Hello World');43var wpt = require('wpt');44var promise = wpt.createPromiseCapability();45promise.promise.then(function(result) {46 console.log(result);47});48promise.resolve('Hello World');49var wpt = require('wpt');
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!!