Best JavaScript code snippet using wpt
theSource.js
Source:theSource.js
1const axios = require("axios");2const moment = require("moment");3const Recommendation = require("../models/recommendation");4const Cycle = require("../models/cycle");5let seedDB = require("../seeds2");6let theSource = {};7theSource.sendRecommendationToPast = () => {8 Recommendation.findOne({status:'present'})9 .exec()10 .then( (presentRecommendation) => {11 let timestampDifference = (new Date).getTime() - presentRecommendation.endingRecommendationTimestamp;12 presentRecommendation.status = "past";13 presentRecommendation.timestampDifference = timestampDifference;14 presentRecommendation.save()15 .then(()=>{16 console.log('The recommendation ' + presentRecommendation.name + ' was sent to the past, now the theMind function should be executed');17 console.log('The difference between the supposed ending timestamp and the actual one is: ' + timestampDifference)18 theSource.theMind();19 })20 })21}22theSource.checkSystem = () => {23 console.log("inside the checkSystem function");24 Recommendation.findOne({status:"present"})25 .exec()26 .then((presentRecommendation)=>{27 if (presentRecommendation) {28 let now = (new Date).getTime();29 let timestampDifference = presentRecommendation.endingRecommendationTimestamp - now;30 if (timestampDifference >= 0) {31 console.log("A setTimeout will start now and be triggered in " + timestampDifference/1000 + " seconds")32 setTimeout(theSource.sendRecommendationToPast, timestampDifference)33 } else { 34 theSource.sendRecommendationToPast();35 }36 } else {37 console.log("There was not a recommendation in the present. This is weird. The theMind function will be called")38 theSource.theMind();39 }40 })41}42theSource.theMind = () => {43 Recommendation.find({reviewed:true}).exec()44 .then((allRecommendations)=>{45 let pastRecommendations = allRecommendations.filter(({status}) => "past".includes(status));46 let presentRecommendation = allRecommendations.filter(({status}) => "present".includes(status));47 let futureRecommendations = allRecommendations.filter(({status}) => "future".includes(status));48 if ( presentRecommendation.length > 1 ) {49 theSource.checkSystem();50 } else if ( futureRecommendations.length > 0 ) {51 let randomIndex = Math.floor(Math.random()*futureRecommendations.length);52 let newPresentRecommendation = futureRecommendations[randomIndex];53 let now = (new Date).getTime();54 newPresentRecommendation.status = "present";55 newPresentRecommendation.startingRecommendationTimestamp = now;56 newPresentRecommendation.endingRecommendationTimestamp = now + newPresentRecommendation.duration;57 newPresentRecommendation.index = pastRecommendations.length;58 newPresentRecommendation.save()59 .then(()=>{60 console.log('The recommendation ' + newPresentRecommendation.name + ' was brought to the present, and the timeout will send this recommendation to the past in ' + newPresentRecommendation.duration/1000 + ' seconds');61 setTimeout(theSource.sendRecommendationToPast, newPresentRecommendation.duration);62 })63 } else {64 theSource.bigBang();65 }66 })67}68theSource.bigBang = async (callback) => {69 console.log("inside the big bang function... emptyness, void, silence...................")70 await Recommendation.find().exec()71 .then( (pastRecommendations) => {72 if (pastRecommendations.length > 0) {73 for (i=0; i<pastRecommendations.length;i++) {74 pastRecommendations[i].status = "future";75 pastRecommendations[i].startingRecommendationTimestamp = ""; 76 pastRecommendations[i].endingRecommendationTimestamp = ""; 77 pastRecommendations[i].timestampDifference = ""; 78 pastRecommendations[i].index = ""; 79 pastRecommendations[i].save()80 }81 theSource.closeCycle(pastRecommendations.length)82 }83 })84 .then(()=>{85 console.log('the theMind function will be executed from inside the bigBang function')86 setTimeout(theSource.theMind, 0);87 })88}89theSource.openCycle = () => {90 Cycle.find({}).exec()91 .then((foundCycles)=>{92 let newCycle = new Cycle({93 startingTimestamp : (new Date).getTime(),94 })95 if (foundCycles.length > 0 ) {96 newCycle.cycleIndex = foundCycles.length;97 } else {98 newCycle.cycleIndex = 0;99 }100 newCycle.save()101 .then(()=>{102 console.log("The new cycle was opened")103 })104 })105}106theSource.closeCycle = (numberOfRecommendations = 0) => {107 Cycle.find({}).exec()108 .then((foundCycles)=>{109 if(foundCycles.length > 0){110 let lastCycle = foundCycles[foundCycles.length-1];111 lastCycle.numberOfRecommendations = numberOfRecommendations;112 lastCycle.cycleDuration = (new Date).getTime() - lastCycle.startingTimestamp;113 lastCycle.save()114 .then(()=>{115 console.log("The cycle #" + lastCycle.cycleIndex + " was closed and saved in the DB");116 })117 }118 })119 .then(()=>{120 theSource.openCycle();121 })122}...
webgl-utils.js
Source:webgl-utils.js
1function start() {2 canvas = document.getElementById("glcanvas");34 gl = null;5 6 try {7 gl = canvas.getContext("experimental-webgl");8 }9 catch(e) {10 }11 if (!gl)12 {13 alert("Unable to initialize WebGL. Your browser may not support it.");14 return;15 }16 17 gl.clearColor(0.0, 0.0, 0.0, 1.0); // Clear to black, fully opaque18 gl.clearDepth(1.0); // Clear everything19 gl.enable(gl.DEPTH_TEST); // Enable depth testing20 gl.depthFunc(gl.LEQUAL); // Near things obscure far things21 gl.enable(gl.BLEND);22 gl.blendFunc(gl.SRC_ALPHA, gl.ONE);23 24 initShaders();2526 setInterval(drawScene, 20);27}2829function initShaders() 30{31 var fragmentShader = getShader(gl, "fragment");32 var vertexShader = getShader(gl, "vertex");33 34 shaderProgram = gl.createProgram();35 gl.attachShader(shaderProgram, vertexShader);36 gl.attachShader(shaderProgram, fragmentShader);37 gl.linkProgram(shaderProgram);38 39 if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS))40 alert("Unable to initialize the shader program.");41 42 gl.useProgram(shaderProgram);43 vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");44 gl.enableVertexAttribArray(vertexPositionAttribute);45 46 vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");47 gl.enableVertexAttribArray(vertexColorAttribute);4849 textureCoordAttribute = gl.getAttribLocation(shaderProgram, "aTextureCoord");50 gl.enableVertexAttribArray(textureCoordAttribute);5152 shaderProgram.enableFog = gl.getUniformLocation(shaderProgram, "uEnableFog");53}5455function getShader(gl, id) 56{57 var shader;58 if (id=="fragment")59 {60 shaderScript = {type:"x-shader/x-fragment"};61/*62 theSource = "varying lowp vec4 vColor;\n"63 theSource+= "void main(void) {\n";64 theSource+= "gl_FragColor = vColor;\n";65 theSource+= "}";66*/67 theSource = 68 "#ifdef GL_ES\n"+69 "precision highp float;\n"+70 "#endif\n"+7172 "uniform sampler2D uSampler;\n"+7374 "varying vec4 vColor;\n"+75 "varying vec2 vTextureCoord;\n"+76 "varying vec3 vLighting;\n"+77 "varying float vEnableFog;\n"+7879 "float uAlpha;\n"+80 "float density = 0.15;\n"+8182 "void main(void) {\n"+83 " uAlpha = 1.0;\n"+84 " float z = gl_FragCoord.z / gl_FragCoord.w;\n"+85 " float fogFactor = exp2( -density * density * z * z * 1.442695);\n"+86 " fogFactor += 1.0 - vEnableFog;\n"+87 " fogFactor = clamp(fogFactor, 0.0, 1.0);\n"+8889 " mediump vec4 texelColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));\n"+90 " gl_FragColor = vec4(texelColor.rgb * vLighting * fogFactor, texelColor.a * fogFactor) * vColor;\n"+91 "}\n";9293 shader = gl.createShader(gl.FRAGMENT_SHADER);94 }95 if (id=="vertex")96 {97 shaderScript = {type:"x-shader/x-vertex"};98 theSource = "attribute vec3 aVertexPosition;\n";99 theSource+= "attribute vec4 aVertexColor;\n";100 theSource+= "attribute vec2 aTextureCoord;\n";101 theSource+= "uniform float uEnableFog;\n";102103 theSource+= "uniform mat4 uMVMatrix;\n";104 theSource+= "uniform mat4 uPMatrix;\n";105 theSource+= "varying lowp vec4 vColor;\n";106 theSource+= "varying vec2 vTextureCoord;\n";107 theSource+= "varying vec3 vLighting;\n";108 theSource+= "varying float vEnableFog;\n";109110 theSource+= "void main(void) {\n";111 theSource+= " gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);\n";112 theSource+= " vColor = aVertexColor;\n";113 theSource+=" vTextureCoord = aTextureCoord;\n";114 theSource+=" vLighting = vec3(0.5, 0.5, 0.5);\n";115 theSource+=" vEnableFog = uEnableFog;\n";116 theSource+= "}\n";117 shader = gl.createShader(gl.VERTEX_SHADER);118 } 119 gl.shaderSource(shader, theSource);120 gl.compileShader(shader);121 if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) 122 {123 alert("An error occurred compiling the shaders: " + gl.getShaderInfoLog(shader));124 return null;125 }126 return shader;127}128129function loadIdentity() {130 mvMatrix = Matrix.I(4);131}132133function multMatrix(m) {134 mvMatrix = mvMatrix.x(m);135}136137function mvTranslate(v) {138 multMatrix(Matrix.Translation($V([v[0], v[1], v[2]])).ensure4x4());139}140141function setMatrixUniforms() {142 var pUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");143 gl.uniformMatrix4fv(pUniform, false, new Float32Array(perspectiveMatrix.flatten()));144145 var mvUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");146 gl.uniformMatrix4fv(mvUniform, false, new Float32Array(mvMatrix.flatten()));147}148149var mvMatrixStack = [];150151function mvPushMatrix(m) {152 if (m) {153 mvMatrixStack.push(m.dup());154 mvMatrix = m.dup();155 } else {156 mvMatrixStack.push(mvMatrix.dup());157 }158}159160function mvPopMatrix() {161 if (!mvMatrixStack.length) {162 throw("Can't pop from an empty matrix stack.");163 }164 165 mvMatrix = mvMatrixStack.pop();166 return mvMatrix;167}168169function mvRotate(angle, v) {170 var inRadians = angle * Math.PI / 180.0;171 172 var m = Matrix.Rotation(inRadians, $V([v[0], v[1], v[2]])).ensure4x4();173 multMatrix(m);
...
Using AI Code Generation
1var wpt = require('webpagetest');2var test = wpt('www.webpagetest.org');3 if (err) return console.log(err);4 test.getTestResults(data.data.testId, function(err, data) {5 if (err) return console.log(err);6 console.log(data.data.median.firstView.SpeedIndex);7 });8});
Using AI Code Generation
1var wptb = require('./wptb');2var w = new wptb();3w.setSource(__dirname);4var source = w.getSource();5console.log(source);6var wptb = require('./wptb');7var w = new wptb();8w.setDestination(__dirname);9var destination = w.getDestination();10console.log(destination);11var wptb = require('./wptb');12var w = new wptb();13w.setTemplate(__dirname);14var template = w.getTemplate();15console.log(template);16var wptb = require('./wptb');17var w = new wptb();18w.setTemplatePath(__dirname);19var templatePath = w.getTemplatePath();20console.log(templatePath);21var wptb = require('./wptb');
Using AI Code Generation
1var wpt = require('./wpt.js');2wpt.theSource(function(source) {3 console.log(source);4});5var wpt = require('./wpt.js');6console.log(wpt.theSourceSync());7var wpt = require('./wpt.js');8wpt.theTitle(function(title) {9 console.log(title);10});
Using AI Code Generation
1var wpt = require('webpagetest');2var options = {3};4var wpt = new WebPageTest(options);5wpt.runTest(url, function(err, data) {6 if (err) return console.error(err);7 console.log('Test submitted to WebPageTest for %s', url);8 console.log('View your test at: %s', data.data.userUrl);9 wpt.getTestStatus(data.data.testId, function(err, data) {10 if (err) return console.error(err);11 if (data.statusCode == 200) {12 console.log('Test complete for %s', url);13 wpt.getTestResults(data.data.testId, function(err, data) {14 if (err) return console.error(err);15 console.log('Test results for %s', url);16 console.log('First View (i.e. Load Time): %s', data.data.average.firstView.loadTime);17 console.log('Repeat View (i.e. Load Time): %s', data.data.average.repeatView.loadTime);18 });19 }20 });21});
Using AI Code Generation
1var wpt = require('webpagetest');2var test = new wpt('A.0c2b2bdc8f6e1c6a7b6d3b6e8c6c3ca6');3 if (err) return console.error(err);4 console.log('Test submitted. Polling for results.');5 test.waitForTestRun(data.data.testId, function(err, data) {6 if (err) return console.error(err);7 console.log('Got test results: %j', data.data);8 test.getTestResults(data.data.testId, function(err, data) {9 if (err) return console.error(err);10 console.log('Got test results: %j', data.data);11 });12 });13});14var wpt = require('webpagetest');15var test = new wpt('A.0c2b2bdc8f6e1c6a7b6d3b6e8c6c3ca6');16 if (err) return console.error(err);17 console.log('Test submitted. Polling for results.');18 test.waitForTestRun(data.data.testId, function(err, data) {19 if (err) return console.error(err);20 console.log('Got test results: %j', data.data);21 test.getTestResults(data.data.testId, function(err, data) {22 if (err) return console.error(err);23 console.log('Got test results: %j', data.data);24 });25 });26});27var wpt = require('webpagetest');28var test = new wpt('A.0c2b2bdc8f6e1c6a7b6d3b6e8c6c3ca6');29test.getTestList(function(err, data) {30 if (err) return console.error(err);31 console.log('Got test list: %j', data.data);32});
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!!