Best JavaScript code snippet using best
hub.ts
Source:hub.ts
...62 // If the client is actively running something we need to kill it63 if (this.activeClients.has(remoteClient)) {64 const remoteAgent = this.activeClients.get(remoteClient);65 if (remoteAgent && remoteAgent.isBusy()) {66 remoteAgent.interruptRunner();67 }68 this.activeClients.delete(remoteClient);69 }70 });71 remoteClient.on(BEST_RPC.BENCHMARK_START, (benchmarkId: string) => {72 const agent = this.activeClients.get(remoteClient);73 this.emit(BEST_RPC.BENCHMARK_START, { agentId: agent && agent.getId(), clientId: remoteClient.getId(), benchmarkId });74 });75 remoteClient.on(BEST_RPC.BENCHMARK_END, (benchmarkId: string) => {76 const agent = this.activeClients.get(remoteClient);77 this.emit(BEST_RPC.BENCHMARK_END, { agentId: agent && agent.getId(), clientId: remoteClient.getId(), benchmarkId });78 });79 remoteClient.on(BEST_RPC.BENCHMARK_UPDATE, (benchmarkId: string, state: BenchmarkUpdateState, opts: BenchmarkRuntimeConfig) => {80 const agent = this.activeClients.get(remoteClient);81 this.emit(BEST_RPC.BENCHMARK_UPDATE, { agentId: agent && agent.getId(), clientId: remoteClient.getId(), benchmarkId, state, opts });82 });83 // If we are done with the job, make sure after a short time the client gets removed84 remoteClient.on(BEST_RPC.REMOTE_CLIENT_EMPTY_QUEUE, () => {85 console.log(`[HUB] Remote client ${remoteClient.getId()} is done. Scheduling a force disconnect.`);86 setTimeout(() => {87 if (this.connectedClients.has(remoteClient)) {88 console.log(`[HUB] Force client disconnect (${remoteClient.getId()}): With no more jobs to run an agent must disconnect`);89 remoteClient.disconnectClient(`Forced disconnect: With no more jobs client should have disconnected`);90 }91 }, 10000);92 });93 return remoteClient;94 }95 // -- Agent lifecycle ---------------------------------------------------------------96 onAgentConnect(agentSocket: Socket) {97 const query = agentSocket.handshake.query;98 const specs = normalizeSpecs(query);99 const validToken = validateToken(query.authToken as string, this.hubConfig.authToken);100 const hasSpecs = specs.length > 0;101 if (!validToken) {102 agentSocket.emit(BEST_RPC.AGENT_REJECTION, 'Invalid Token');103 return agentSocket.disconnect(true);104 }105 if (!hasSpecs) {106 agentSocket.emit(BEST_RPC.AGENT_REJECTION, 'An agent must provide specs');107 return agentSocket.disconnect(true);108 }109 if (!query.agentUri) {110 agentSocket.emit(BEST_RPC.AGENT_REJECTION, 'An agent must provide a URI');111 return agentSocket.disconnect(true);112 }113 const remoteAgent = this.setupNewAgent(agentSocket, specs, { agentUri: query.agentUri, agentToken: query.agentAuthToken });114 if (remoteAgent) {115 // If queued jobs with those specs, run them...116 }117 }118 setupNewAgent(socketAgent: Socket, specs: BrowserSpec[], { agentUri, agentToken }: any): RemoteAgent {119 // Create and new RemoteAgent and add it to the pool120 const remoteAgent = new RemoteAgent(socketAgent, { uri: agentUri, token: agentToken, specs });121 this.connectedAgents.add(remoteAgent);122 console.log(`[HUB] New Agent ${remoteAgent.getId()} connected with specs: ${JSON.stringify(remoteAgent.getSpecs())}`);123 this.emit(BEST_RPC.HUB_CONNECTED_AGENT, { agentId: remoteAgent.getId(), specs: remoteAgent.getSpecs(), uri: remoteAgent.getUri()});124 // Make sure we remove it from an agent's perspective if the client is disconnected125 remoteAgent.on(BEST_RPC.DISCONNECT, () => {126 console.log(`[HUB] Disconnected Agent ${remoteAgent.getId()}`);127 this.emit(BEST_RPC.HUB_DISCONNECTED_AGENT, { agentId: remoteAgent.getId() });128 this.connectedAgents.delete(remoteAgent);129 if (remoteAgent.isBusy()) {130 remoteAgent.interruptRunner();131 }132 });133 return remoteAgent;134 }135 // -- Private methods ---------------------------------------------------------------136 async runBenchmarks(remoteClient: RemoteClient) {137 // New agent setup138 if (!this.activeClients.has(remoteClient)) {139 const matchingAgents = this.findAgentMatchingSpecs(remoteClient, { ignoreBusy: true });140 if (matchingAgents.length > 0) {141 const remoteAgent = matchingAgents[0];142 this.activeClients.set(remoteClient, remoteAgent);143 this.emit(BEST_RPC.AGENT_RUNNING_CLIENT, { clientId: remoteClient.getId(), agentId: remoteAgent.getId(), jobs: remoteClient.getPendingBenchmarks() });144 try {...
home.component.ts
Source:home.component.ts
...213 );214 this.selection.end = this.selection.start;215 this.onChange();216 }217 public interruptRunner(ev: MouseEvent): void {218 chrome.tabs.sendMessage(window.currentTabId, { call: "interruptRunner" });219 this.running = false;220 }221 public onChange(): void {222 this.dirty = true;223 if (this.testCase.items.length === 0) {224 this.testCase.items.push(225 new TestCaseItem({ type: "command" } as TestCaseItem)226 );227 }228 if (this.selection.start >= this.testCase.items.length) {229 this.selection.start = this.testCase.items.length - 1;230 }231 if (this.selection.end >= this.testCase.items.length) {...
remote-agent.ts
Source:remote-agent.ts
...73 }74 getUri() {75 return this.uri;76 }77 interruptRunner() {78 if (this.isBusy() && this.runner) {79 this.runner.interruptRunner();80 }81 }82 isBusy(): boolean {83 return this.state === AgentState.BUSY;84 }85 isIdle(): boolean {86 return this.state === AgentState.IDLE;87 }88 async runBenchmarks(remoteClient: RemoteClient, jobsToRun: number = remoteClient.getPendingBenchmarks()) {89 if (this.isIdle() && remoteClient.getPendingBenchmarks() > 0) {90 this.state = AgentState.BUSY;91 const iterator = Array.from(Array(jobsToRun), (x, index) => index + 1);92 const runnerConfig: any = { uri: this.uri, specs: remoteClient.getSpecs(), jobs: 1, options: {} };93 if (this.token) {...
Using AI Code Generation
1var BestTimeToBuyAndSellStockWithCooldown = require('./BestTimeToBuyAndSellStockWithCooldown');2var obj = new BestTimeToBuyAndSellStockWithCooldown();3obj.interruptRunner();4var BestTimeToBuyAndSellStockWithCooldown = function() {5 this.interruptRunner = function() {6 var prices = [1, 2, 3, 0, 2];7 console.log(this.maxProfit(prices));8 };9 this.maxProfit = function(prices) {10 var n = prices.length;11 if (n <= 1) return 0;12 var dp = new Array(n);13 for (var i = 0; i < n; i++) dp[i] = new Array(2);14 dp[0][0] = 0;15 dp[0][1] = -prices[0];16 dp[1][0] = Math.max(dp[0][0], dp[0][1] + prices[1]);17 dp[1][1] = Math.max(dp[0][1], dp[0][0] - prices[1]);18 for (var i = 2; i < n; i++) {19 dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);20 dp[i][1] = Math.max(dp[i - 1][1], dp[i - 2][0] - prices[i]);21 }22 return dp[n - 1][0];23 };24};25module.exports = BestTimeToBuyAndSellStockWithCooldown;
Using AI Code Generation
1var BestTimeToBuyAndSellStock = require('./BestTimeToBuyAndSellStock');2var prices = [2,4,1];3var bestTimeToBuyAndSellStock = new BestTimeToBuyAndSellStock(prices);4var result = bestTimeToBuyAndSellStock.interruptRunner();5console.log(result);6var BestTimeToBuyAndSellStock = function(prices){7 this.prices = prices;8 this.interruptRunner = function(){9 var profit = 0;10 var buyPrice = 0;11 var sellPrice = 0;12 var changeBuyPrice = true;13 for(var i=0; i<prices.length; i++){14 if(changeBuyPrice){15 buyPrice = prices[i];16 }17 sellPrice = prices[i+1];18 if(sellPrice < buyPrice){19 changeBuyPrice = true;20 }else{21 var tempProfit = sellPrice - buyPrice;22 if(tempProfit > profit){23 profit = tempProfit;24 }25 changeBuyPrice = false;26 }27 }28 return profit;29 }30}31module.exports = BestTimeToBuyAndSellStock;
Using AI Code Generation
1var BestRunner = require('./BestRunner');2var runner = new BestRunner();3runner.interruptRunner(function(){4 console.log('interrupted');5});6var BestRunner = require('./BestRunner');7var runner = new BestRunner();8runner.interruptRunner(function(){9 console.log('interrupted');10});11var BestRunner = require('./BestRunner');12var runner = new BestRunner();13runner.interruptRunner(function(){14 console.log('interrupted');15});16var BestRunner = require('./BestRunner');17var runner = new BestRunner();18runner.interruptRunner(function(){19 console.log('interrupted');20});21var BestRunner = require('./BestRunner');22var runner = new BestRunner();23runner.interruptRunner(function(){24 console.log('interrupted');25});26var BestRunner = require('./BestRunner');27var runner = new BestRunner();28runner.interruptRunner(function(){29 console.log('interrupted');30});31var BestRunner = require('./BestRunner');32var runner = new BestRunner();33runner.interruptRunner(function(){34 console.log('interrupted');35});36var BestRunner = require('./BestRunner');37var runner = new BestRunner();38runner.interruptRunner(function(){39 console.log('interrupted');40});41var BestRunner = require('./BestRunner');42var runner = new BestRunner();43runner.interruptRunner(function(){44 console.log('interrupted');45});46var BestRunner = require('./BestRunner');47var runner = new BestRunner();48runner.interruptRunner(function(){49 console.log('interrupted');50});51var BestRunner = require('./BestRunner');52var runner = new BestRunner();
Using AI Code Generation
1var runner = new BestRunner();2runner.setName("John");3var name = runner.getName();4runner.setBestTime(100);5var bestTime = runner.getBestTime();6runner.setCurrentTime(100);7var currentTime = runner.getCurrentTime();8var timeDiff = runner.getTimeDiff();9var status = runner.getStatus();10var statusMessage = runner.getStatusMessage();11var statusColor = runner.getStatusColor();12var statusIcon = runner.getStatusIcon();13var statusIconColor = runner.getStatusIconColor();14var statusIconSize = runner.getStatusIconSize();15var statusIconURL = runner.getStatusIconURL();16var statusIconURL = runner.getStatusIconURL();17var statusIconSize = runner.getStatusIconSize();18var statusIconColor = runner.getStatusIconColor();19var statusIcon = runner.getStatusIcon();20var statusColor = runner.getStatusColor();21var statusMessage = runner.getStatusMessage();22var status = runner.getStatus();23var timeDiff = runner.getTimeDiff();24var currentTime = runner.getCurrentTime();25var bestTime = runner.getBestTime();26var name = runner.getName();27runner.interruptRunner();28var status = runner.getStatus();29var statusMessage = runner.getStatusMessage();30var statusColor = runner.getStatusColor();31var statusIcon = runner.getStatusIcon();
Using AI Code Generation
1var BestRunner = require('./BestRunner.js');2var runner = new BestRunner();3runner.on('start', function() {4 console.log('start');5});6runner.on('stop', function() {7 console.log('stop');8});9runner.on('tick', function() {10 console.log('tick');11});12runner.start();13runner.interruptRunner(5000);14var BestRunner = require('./BestRunner.js');15var runner = new BestRunner();16runner.on('start', function() {17 console.log('start');18});19runner.on('stop', function() {20 console.log('stop');21});22runner.on('tick', function() {23 console.log('tick');24});25runner.start();26runner.interruptRunner(5000);27var BestRunner = require('./BestRunner.js');28var runner = new BestRunner();29runner.on('start', function() {30 console.log('start');31});32runner.on('stop', function() {33 console.log('stop');34});35runner.on('tick', function() {36 console.log('tick');37});38runner.start();39runner.interruptRunner(5000);40var BestRunner = require('./BestRunner.js');41var runner = new BestRunner();42runner.on('start', function() {43 console.log('start');44});45runner.on('stop', function() {46 console.log('stop');47});48runner.on('tick', function() {49 console.log('tick');50});51runner.start();52runner.interruptRunner(5000);
Using AI Code Generation
1var BestPracticesRunner = require('best-practices-runner');2var runner = new BestPracticesRunner();3runner.setTimeout(5000);4runner.run(function(err, result) {5 if (err) {6 console.log(err);7 } else {8 console.log(result);9 }10});11runner.interruptRunner();
Using AI Code Generation
1var BestTimeToBuyAndSellStock = require('./BestTimeToBuyAndSellStock.js');2var bestTimeToBuyAndSellStock = new BestTimeToBuyAndSellStock();3var prices = [7, 1, 5, 3, 6, 4];4console.log("prices: " + prices);5var profit = bestTimeToBuyAndSellStock.interruptRunner(prices, true);6console.log("profit: " + profit);7console.log("");8prices = [7, 6, 4, 3, 1];9console.log("prices: " + prices);10profit = bestTimeToBuyAndSellStock.interruptRunner(prices, true);11console.log("profit: " + profit);12console.log("");13prices = [1, 2];14console.log("prices: " + prices);15profit = bestTimeToBuyAndSellStock.interruptRunner(prices, true);16console.log("profit: " + profit);17console.log("");18prices = [2, 1];19console.log("prices: " + prices);20profit = bestTimeToBuyAndSellStock.interruptRunner(prices, true);21console.log("profit: " + profit);22console.log("");23prices = [2, 4, 1];24console.log("prices: " + prices);25profit = bestTimeToBuyAndSellStock.interruptRunner(prices, true);26console.log("profit: " + profit);27console.log("");28prices = [3, 2, 6, 5, 0, 3];29console.log("prices: " + prices);30profit = bestTimeToBuyAndSellStock.interruptRunner(prices, true);31console.log("profit: " + profit);32console.log("");33prices = [1, 2, 4, 2, 5, 7, 2, 4, 9, 0];34console.log("prices: " + prices);35profit = bestTimeToBuyAndSellStock.interruptRunner(prices, true);36console.log("profit: " + profit);37console.log("");
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!!