Best JavaScript code snippet using best
phaseUpdater.js
Source:phaseUpdater.js
1const { parentPort } = require('worker_threads');2const hub = require('@textile/hub');3const models = require("../models");4const getHub = require("../config/hub");5const roomId = require('worker_threads').workerData.roomId;6const threadsInfo = require('worker_threads').workerData.threads;7const config = require('worker_threads').workerData.config;8const identity = hub.PrivateKey.fromString(config.identity);9const threadId = hub.ThreadID.fromString(config.threadId);10const threads = {11 villagerThread: hub.ThreadID.fromString(threadsInfo.villagerThread),12 gameStateThread: hub.ThreadID.fromString(threadsInfo.gameStateThread)13}14let isCancelled15if (parentPort){16 parentPort.once('message', message => {17 if (message === 'cancel') {18 isCancelled = true;19 }20 });21}22const updater = async () => {23 if (isCancelled) return;24 const hubConfig = await getHub(identity)25 const room = await hubConfig.client.findByID(threadId, 'rooms', roomId)26 console.log("update phase job running!")27 let message, winner28 switch (room.phase) {29 case "WAITING":30 room.phase = "NIGHT"31 room.currentDay = 132 await models.initCollections(33 hubConfig.client,34 threads.gameStateThread,35 [{name: `NIGHT-${room.currentDay}`, type: "game"}]36 )37 break38 case "NIGHT":39 room.phase = "DISCUSSION"40 const nightResult = await models.game.nightResult(41 hubConfig.client,42 threads.gameStateThread,43 `NIGHT-${room.currentDay}`44 )45 if(nightResult.deadPlayer !== "none"){46 await models.playerState.markPlayerDead(47 hubConfig.client,48 threads.gameStateThread,49 nightResult.deadPlayer50 )51 }52 message = {53 subject: "DEAD_PLAYER",54 message: nightResult.deadPlayer,55 to: "ALL",56 from: identity.public.toString(),57 type: "SYSTEM"58 }59 await hubConfig.client.create(threads.villagerThread, 'chat', [message])60 break61 case "DISCUSSION":62 room.phase = "VOTING"63 await models.initCollections(64 hubConfig.client,65 threads.gameStateThread,66 [{name: `DAY-${room.currentDay}`, type: "game"}]67 )68 break69 case "VOTING":70 room.phase = "NIGHT"71 console.log("day result calculation...")72 const dayResult = await models.game.dayResult(73 hubConfig.client,74 threads.gameStateThread,75 `DAY-${room.currentDay}`76 )77 console.log("init next phase")78 room.currentDay += 179 await models.initCollections(80 hubConfig.client,81 threads.gameStateThread,82 [{name: `NIGHT-${room.currentDay}`, type: "game"}]83 )84 console.log(dayResult)85 console.log("mark player dead")86 if(dayResult.ejectedPlayer !== "none"){87 await models.playerState.markPlayerDead(88 hubConfig.client,89 threads.gameStateThread,90 dayResult.ejectedPlayer91 )92 }93 console.log("notifying player")94 // await models.playerState.markPlayerDead(hubConfig.client, threads.gameStateThread, dayResult.deadPlayer)95 message = {96 subject: "EJECTED_PLAYER",97 message: dayResult.ejectedPlayer,98 to: "ALL",99 from: identity.public.toString(),100 type: "SYSTEM"101 }102 await hubConfig.client.create(threads.villagerThread, 'chat', [message])103 break104 case "ENDED":105 room.phase = "ENDED"106 break107 default:108 break109 }110 await hubConfig.client.save(threadId, "rooms", [room])111 winner = await models.game.checkWinningCondition(hubConfig.client, threads.gameStateThread)112 if (winner) {113 message = {114 subject: "WINNER",115 message: winner,116 to: "ALL",117 from: identity.public.toString(),118 type: "SYSTEM"119 }120 await hubConfig.client.create(threads.villagerThread, 'chat', [message])121 }122}123(async() => {124 try{125 await updater()126 } catch (e) {127 console.log(e);128 }129 // signal to parent that the job is done130 if (parentPort) parentPort.postMessage('done');131 else process.exit(0);...
index.js
Source:index.js
1// @flow2import { parseZoneFile } from 'zone-file'3import type { GaiaHubConfig } from './blockstack-inc'4import { connectToGaiaHub, uploadToGaiaHub } from './blockstack-inc'5import { getTokenFileUrlFromZoneFile } from '@utils/zone-utils'6const logger = console;7export const BLOCKSTACK_INC = 'gaia-hub'8const DEFAULT_PROFILE_FILE_NAME = 'profile.json'9function getProfileUploadLocation(identity: any, hubConfig: GaiaHubConfig) {10 if (identity.zoneFile) {11 const zoneFileJson = parseZoneFile(identity.zoneFile)12 return getTokenFileUrlFromZoneFile(zoneFileJson)13 } else {14 // aaron-debt: this should call a function in blockstack.js to get15 // the read url16 return `${hubConfig.url_prefix}${hubConfig.address}/${DEFAULT_PROFILE_FILE_NAME}`17 }18}19// aaron-debt: this should be moved into blockstack.js20function canWriteUrl(url: string, hubConfig: GaiaHubConfig): ?string {21 const readPrefix = `${hubConfig.url_prefix}${hubConfig.address}/`22 if (url.startsWith(readPrefix)) {23 return url.substring(readPrefix.length)24 } else {25 return null26 }27}28function tryUpload(29 urlToWrite: string,30 data: any,31 hubConfig: GaiaHubConfig,32 mimeType: ?string = undefined33) {34 const filenameWrite = canWriteUrl(urlToWrite, hubConfig)35 if (filenameWrite === null) {36 return null37 }38 return uploadToGaiaHub(filenameWrite, data, hubConfig, mimeType)39}40export function uploadPhoto(41 api: { gaiaHubConfig: GaiaHubConfig, gaiaHubUrl: string },42 identity: any,43 identityKeyPair: { key: string },44 photoFile: File | Blob,45 photoIndex: number46) {47 return connectToGaiaHub(api.gaiaHubUrl, identityKeyPair.key).then(identityHubConfig => {48 const globalHubConfig = api.gaiaHubConfig49 let uploadPrefix = getProfileUploadLocation(identity, identityHubConfig)50 if (uploadPrefix.endsWith('profile.json')) {51 uploadPrefix = uploadPrefix.substring(0, uploadPrefix.length - 'profile.json'.length)52 } else {53 throw new Error(`Cannot determine photo location based on profile location ${uploadPrefix}`)54 }55 const photoFilename = `avatar-${photoIndex}`56 const urlToWrite = `${uploadPrefix}/${photoFilename}`57 let uploadAttempt = tryUpload(urlToWrite, photoFile, identityHubConfig, photoFile.type)58 if (uploadAttempt === null) {59 uploadAttempt = tryUpload(urlToWrite, photoFile, globalHubConfig, photoFile.type)60 }61 // if still null, we don't know the write gaia-hub-config to write the file.62 if (uploadAttempt === null) {63 logger.error(`Wanted to write to ${urlToWrite} but I don't know how.` +64 ' Uploading to the default path on the configured hub.')65 uploadAttempt = uploadToGaiaHub(photoFilename, photoFile, identityHubConfig, photoFile.type)66 }67 return uploadAttempt68 })69}70export function uploadProfile(71 api: { gaiaHubConfig: GaiaHubConfig, gaiaHubUrl: string },72 identity: any,73 identityKeyPair: { key: string },74 signedProfileTokenData: string75) {76 return connectToGaiaHub(api.gaiaHubUrl, identityKeyPair.key).then(identityHubConfig => {77 const urlToWrite = getProfileUploadLocation(identity, identityHubConfig)78 let uploadAttempt = tryUpload(79 urlToWrite,80 signedProfileTokenData,81 identityHubConfig,82 'application/json'83 )84 if (uploadAttempt === null) {85 uploadAttempt = tryUpload(86 urlToWrite,87 signedProfileTokenData,88 identityHubConfig,89 'application/json'90 )91 }92 // if still null, we don't know the write gaia-hub-config to write the file.93 if (uploadAttempt === null) {94 logger.error(`Wanted to write to ${urlToWrite} but I don't know how.` +95 ' Uploading to the default path on the configured hub.')96 uploadAttempt = uploadToGaiaHub(DEFAULT_PROFILE_FILE_NAME, signedProfileTokenData,97 identityHubConfig, 'application/json')98 }99 return uploadAttempt100 })...
useExecuteTestHub.ts
Source:useExecuteTestHub.ts
1import { useEffect, useState } from 'react';2import * as SignalR from '@aspnet/signalr';3const appSettings = require('appSettings');4export type TestExecutionType = 'exe' | 'cilantro-interpreter';5interface ExecuteTestHubConfig {6 executionType: TestExecutionType;7 testId: string;8 onConnectionStart: () => void;9 onConnectionError: () => void;10 onExecutionStart: () => void;11 onExecutionEnd: () => void;12 onExecutionOutput: (line: string) => void;13 onExecutionError: (line: string) => void;14}15interface ExecuteTestHub {16 input: (line: string) => Promise<void>;17}18const useExecuteTestHub = (hubConfig: ExecuteTestHubConfig) => {19 const [, setConnection] = useState<SignalR.HubConnection | undefined>(undefined);20 const [hub, setHub] = useState<ExecuteTestHub | undefined>(undefined);21 useEffect(() => {22 const hubConnection = new SignalR.HubConnectionBuilder().withUrl(appSettings.hubsBaseUrl + '/execute-test').build();23 hubConnection.on('start', () => {24 hubConfig.onExecutionStart();25 });26 hubConnection.on('end', () => {27 hubConfig.onExecutionEnd();28 });29 hubConnection.on('output', line => {30 hubConfig.onExecutionOutput(line);31 });32 hubConnection.on('error', line => {33 hubConfig.onExecutionError(line);34 });35 hubConnection36 .start()37 .then(() => {38 setConnection(hubConnection);39 const connectedHub = {40 input: async (line: string) => {41 try {42 return await hubConnection.send('input', line);43 } catch (error) {44 hubConfig.onConnectionError();45 }46 }47 };48 setHub(connectedHub);49 hubConfig.onConnectionStart();50 if (hubConfig.executionType === 'exe') {51 hubConnection.send('runExe', hubConfig.testId);52 } else if (hubConfig.executionType === 'cilantro-interpreter') {53 hubConnection.send('runInterpreter', hubConfig.testId);54 }55 })56 .catch(error => {57 hubConfig.onConnectionError();58 });59 return () => {60 if (hubConnection) {61 hubConnection.send('kill');62 hubConnection.stop();63 setConnection(undefined);64 }65 };66 }, []);67 return hub;68};...
Using AI Code Generation
1var BestBuy = require('bestbuy')(process.env.BESTBUY_API_KEY);2BestBuy.hubsConfig().then(function (data) {3 console.log(data);4});5var BestBuy = require('bestbuy')(process.env.BESTBUY_API_KEY);6BestBuy.hubsConfig({id: 'abcat0101000'}).then(function (data) {7 console.log(data);8});9var BestBuy = require('bestbuy')(process.env.BESTBUY_API_KEY);10BestBuy.hubsConfig({id: 'abcat0101000', show: 'name,sku'}).then(function (data) {11 console.log(data);12});13var BestBuy = require('bestbuy')(process.env.BESTBUY_API_KEY);14BestBuy.hubsConfig({id: 'abcat0101000', show: 'name,sku', page: 1, pageSize: 1}).then(function (data) {15 console.log(data);16});17var BestBuy = require('bestbuy')(process.env.BESTBUY_API_KEY);18BestBuy.hubsConfig({id: 'abcat0101000', show: 'name,sku', page: 1, pageSize: 1, format: 'json'}).then(function (data) {19 console.log(data);20});21var BestBuy = require('bestbuy')(process.env.BESTBUY_API_KEY);22BestBuy.hubsConfig({id: 'abcat0101000', show: 'name,sku', page: 1, pageSize: 1, format: 'json', callback: 'myFunction'}).then(function (data) {23 console.log(data);24});25var BestBuy = require('bestbuy')(process.env.BESTBUY_API_KEY);26BestBuy.hubsConfig({id: 'abcat0101000', show: 'name,sku', page: 1, pageSize: 1, format: 'json
Using AI Code Generation
1var bestBuy = require('bestbuy')(process.env.BESTBUY_API_KEY);2var express = require('express');3var app = express();4app.get('/products', function(req, res){5 var q = req.query.q;6 var category = req.query.category;7 var sku = req.query.sku;8 var limit = req.query.limit;9 var offset = req.query.offset;10 var sort = req.query.sort;11 var show = req.query.show;12 var format = req.query.format;13 bestBuy.products(q, category, sku, limit, offset, sort, show, format, function(err, data){14 if(err){15 console.log("Error: " + err);16 }else{17 res.send(data);18 }19 });20});21app.listen(3000, function(){22 console.log("Server running on port 3000");23});24var bestBuy = require('bestbuy')(process.env.BESTBUY_API_KEY);25var express = require('express');26var app = express();27app.get('/stores', function(req, res){28 var zip = req.query.zip;29 var area = req.query.area;30 var city = req.query.city;31 var state = req.query.state;32 var long = req.query.long;33 var lat = req.query.lat;34 var radius = req.query.radius;35 var service = req.query.service;36 var limit = req.query.limit;37 var offset = req.query.offset;38 var show = req.query.show;39 var format = req.query.format;40 bestBuy.stores(zip, area, city, state, long, lat, radius, service, limit, offset, show, format, function(err, data){41 if(err){42 console.log("Error: " + err);43 }else{44 res.send(data);45 }46 });47});48app.listen(3000, function(){49 console.log("Server running on port 3000");50});51var bestBuy = require('bestbuy')(process.env.BESTBUY_API_KEY);52var express = require('express');53var app = express();54app.get('/stores', function(req, res){55 var zip = req.query.zip;56 var area = req.query.area;
Using AI Code Generation
1var bestPractices = require('./BestPractices.js');2var config = {3 capabilities: {4 },5};6bestPractices.hubConfig(config);7var bestPractices = require('./BestPractices.js');8var config = {9 capabilities: {10 },11};12bestPractices.hubConfig(config);13var bestPractices = require('./BestPractices.js');14var config = {15 capabilities: {16 },17};18bestPractices.hubConfig(config);19var bestPractices = require('./BestPractices.js');20var config = {21 capabilities: {22 },23};24bestPractices.hubConfig(config);25var bestPractices = require('./BestPractices.js');26var config = {27 capabilities: {28 },29};30bestPractices.hubConfig(config);31var bestPractices = require('./BestPractices.js');32var config = {33 capabilities: {34 },
Using AI Code Generation
1var BestPracticeHub = require('BestPracticeHub');2var hub = new BestPracticeHub();3var BestPracticeHub = require('BestPracticeHub');4var hub = new BestPracticeHub();5var BestPracticeHub = require('BestPracticeHub');6var hub = new BestPracticeHub();7var BestPracticeHub = require('BestPracticeHub');8var hub = new BestPracticeHub();9var BestPracticeHub = require('BestPracticeHub');10var hub = new BestPracticeHub();11var BestPracticeHub = require('BestPracticeHub');12var hub = new BestPracticeHub();13var BestPracticeHub = require('BestPracticeHub');14var hub = new BestPracticeHub();15var BestPracticeHub = require('BestPracticeHub');16var hub = new BestPracticeHub();17var BestPracticeHub = require('BestPracticeHub');18var hub = new BestPracticeHub();19var BestPracticeHub = require('BestPracticeHub
Using AI Code Generation
1var BestBuy = require('./BestBuy.js');2var bb = new BestBuy();3var HubConfig = require('./HubConfig.js');4var hc = new HubConfig();5function BestBuy(){6 this.hubConfig = function(){7 hc.hubConfig();8 }9}10module.exports = BestBuy;11var HubConfig = require('./HubConfig.js');12var hc = new HubConfig();13function BestBuy(){14 this.hubConfig = function(){15 hc.hubConfig();16 }17}18module.exports = BestBuy;19var HubConfig = require('./HubConfig.js');20var hc = new HubConfig();21function BestBuy(){22 this.hubConfig = function(){23 hc.hubConfig();24 }25}26module.exports = BestBuy;27var HubConfig = require('./HubConfig.js');28var hc = new HubConfig();29function BestBuy(){30 this.hubConfig = function(){31 hc.hubConfig();32 }33}34module.exports = BestBuy;35var HubConfig = require('./HubConfig.js');36var hc = new HubConfig();37function BestBuy(){38 this.hubConfig = function(){39 hc.hubConfig();40 }41}42module.exports = BestBuy;43var HubConfig = require('./HubConfig.js');44var hc = new HubConfig();45function BestBuy(){46 this.hubConfig = function(){47 hc.hubConfig();48 }49}50module.exports = BestBuy;51var HubConfig = require('./HubConfig.js');52var hc = new HubConfig();53function BestBuy(){54 this.hubConfig = function(){55 hc.hubConfig();56 }57}58module.exports = BestBuy;
Using AI Code Generation
1var bestbuy = require('bestbuy')('xxxxx');2var hubConfig = bestbuy.hubConfig('H0102');3hubConfig.then(function(data){4 console.log(data);5});6hubConfig.catch(function(err){7 console.log(err);8});9var bestbuy = require('bestbuy')('xxxxx');10var hubConfig = bestbuy.hubConfig('H0102');11hubConfig.then(function(data){12 console.log(data);13});14hubConfig.catch(function(err){15 console.log(err);16});17var bestbuy = require('bestbuy')('xxxxx');18var hubConfig = bestbuy.hubConfig('H0102');19hubConfig.then(function(data){20 console.log(data);21});22hubConfig.catch(function(err){23 console.log(err);24});25var bestbuy = require('bestbuy')('xxxxx');26var hubConfig = bestbuy.hubConfig('H0102');27hubConfig.then(function(data){28 console.log(data);29});30hubConfig.catch(function(err){31 console.log(err);32});33var bestbuy = require('bestbuy')('xxxxx');34var hubConfig = bestbuy.hubConfig('H0102');35hubConfig.then(function(data){36 console.log(data);37});38hubConfig.catch(function(err){39 console.log(err);40});41var bestbuy = require('bestbuy')('xxxxx');42var hubConfig = bestbuy.hubConfig('H0102');43hubConfig.then(function(data){44 console.log(data);45});46hubConfig.catch(function(err){47 console.log(err);48});49var bestbuy = require('bestbuy')('xxxxx');50var hubConfig = bestbuy.hubConfig('H0102');51hubConfig.then(function(data){52 console.log(data);53});54hubConfig.catch(function(err){55 console.log(err);56});
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!!