Best JavaScript code snippet using ava
game.js
Source:game.js
...44 "draws_remaining": number,45 "terminal": false46 };47 }48 this.emitStateChange();49 }50 this.drawPlayerCard = function(player) {51 var card = this.situation.player_cards_draw.shift();52 eventSink.emit({53 "event_type": "draw_player_card",54 "player": player,55 "card": card56 });57 this.situation.state.draws_remaining--;58 if (card.type === "epidemic") {59 return this.handleEpidemic();60 } else {61 this.findPlayer(player).hand.push(card);62 if (this.findPlayer(player).hand.length > this.situation.max_player_cards) {63 return this.handleHandLimitExceeded(player);64 }65 return true;66 }67 }68 this.handleEpidemic = function() {69 this.situation.infection_rate_index++;70 eventSink.emit({"event_type": "infection_rate_increased"});71 if (!this.drawInfection(3, true)) {72 return false;73 }74 this.situation.state = {75 "name": "epidemic",76 "player": this.situation.state.player,77 "parent": this.situation.state,78 "terminal": false79 };80 this.emitStateChange();81 return true;82 };83 this.handleHandLimitExceeded = function(player) {84 this.situation.state = {85 "name": "hand_limit_exceeded",86 "player": player,87 "parent": this.situation.state,88 "terminal": false89 };90 this.emitStateChange();91 return true;92 };93 this.infect = function(loc, dis, num) {94 var max_infections = 3;95 var self = this;96 function _infect(locs, dis, out) {97 var disease = self.findDisease(dis);98 if (disease.status === "eradicated") return true;99 if (_.isEmpty(locs)) return true;100 var loc = _.first(locs);101 var location = self.findLocation(loc);102 // If an outbreak already occurred here, skip103 if (_.contains(out, loc)) return _infect(_.rest(locs), dis, out);104 // Outbreak105 if (location.infections[dis] === max_infections) {106 eventSink.emit({107 "event_type": "outbreak",108 "location": loc,109 "disease": dis110 });111 self.situation.outbreak_count++;112 if (self.situation.outbreak_count > self.situation.max_outbreaks) {113 self.situation.state = { "name": "defeat_too_many_outbreaks", "terminal": true };114 self.emitStateChange();115 return false;116 }117 return _infect(_.rest(locs).concat(location.adjacent), dis, out.concat([loc]));118 }119 // Out of cubes120 if (disease.cubes === 0) {121 self.situation.state = {122 "name": "defeat_too_many_infections",123 "disease": dis,124 "terminal": true };125 self.emitStateChange();126 return false;127 }128 // Infection129 location.infections[dis]++;130 disease.cubes--;131 eventSink.emit({132 "event_type": "infect",133 "location": loc,134 "disease": dis135 });136 return _infect(_.rest(locs), dis, out);137 }138 return _infect(_.times(num, function() { return loc; }), dis, []);139 };140 this.drawInfection = function(n, last) {141 var card;142 if (last) {143 card = this.situation.infection_cards_draw.pop();144 } else {145 card = this.situation.infection_cards_draw.shift();146 }147 this.situation.infection_cards_discard.unshift(card);148 eventSink.emit({149 "event_type": "draw_and_discard_infection_card",150 "card": card151 });152 var location = this.findLocation(card.location);153 return this.infect(location.name, location.disease, n);154 };155 this.startInfectionPhase = function(player) {156 var rate = this.situation.infection_rate_levels[this.situation.infection_rate_index].rate;157 this.situation.state = {158 "name": "draw_infection_cards",159 "player": player,160 "draws_remaining": rate,161 "terminal": false162 };163 this.emitStateChange();164 }165 this.resume = function(situation) {166 if (!_.isNull(this.situation)) {167 throw "Game already initialized!";168 }169 this.situation = clone(situation);170 }171 this.setup = function(gameDef, players, settings) {172 if (!_.isNull(this.situation)) {173 throw "Game already initialized!";174 }175 var initialState = _.extend(clone(gameDef), settings);176 // assign roles177 var roles = _.map(gameDef.roles, function(role) { return role.name; });178 roles = randy.sample(roles, players.length);179 initialState.players = _.map(_.zip(players, roles),180 function(arr) {181 var player = _.object(["id", "role"], arr);182 player.location = gameDef.starting_location;183 player.hand = [];184 return player;185 });186 // create initial research center187 initialState.research_centers.push({ "location": gameDef.starting_location });188 initialState.research_centers_available--;189 // shuffle infection cards190 initialState.infection_cards_draw = randy.shuffle(gameDef.infection_cards_draw);191 // shuffle player cards and insert epidemic cards192 function setupPlayerCards() {193 var cards = randy.shuffle(gameDef.player_cards_draw);194 var nEpidemics = settings.number_of_epidemics;195 if (nEpidemics > 0) {196 var initialDeal = gameDef.initial_player_cards[players.length];197 var nReserved = initialDeal * players.length;198 var nCards = gameDef.player_cards_draw.length;199 var n = nCards - nReserved;200 var chunkSize = Math.floor(n / nEpidemics);201 var larger = n - (nEpidemics * chunkSize);202 var counts = _.times(nEpidemics,203 function(index) {204 return chunkSize + (index < larger ? 1 : 0);205 });206 var chunks = _.map(counts,207 function(count) { 208 var chunk = [this.index, this.index + count];209 this.index += count;210 return chunk;211 },212 { "index": nReserved });213 return _.reduce(chunks, function(memo, chunk, index) {214 var where = randy.randInt(chunk[0], chunk[1]);215 return memo216 .concat(cards.slice(chunk[0], where))217 .concat([{ "type": "epidemic", "number": index }])218 .concat(cards.slice(where, chunk[1]));219 }, cards.slice(0, nReserved));220 } else {221 return cards;222 }223 }224 initialState.player_cards_draw = setupPlayerCards();225 initialState.state = { "name": "setup", "terminal": false };226 // Make the initial state known227 eventSink.emit({ "event_type": "initial_situation", "situation": initialState });228 this.situation = clone(initialState);229 var self = this;230 // Initial infections231 _.each(initialState.initial_infections, function(n) {232 self.drawInfection(n);233 });234 // Initial draws235 var nDraw = gameDef.initial_player_cards[players.length];236 _.each(_.range(nDraw), function(idx) {237 _.each(self.situation.players, function(player) {238 self.drawPlayerCard(player.id);239 });240 });241 // Give turn to first player242 this.situation.state = player_actions_state(self.situation.players[0].id);243 this.emitStateChange();244 };245 this.resumeDrawPlayerCards = function() {246 var parent = this.situation.state.parent;247 if (parent.name !== "draw_player_cards") {248 throw "invalid state";249 }250 if (parent.draws_remaining > 0) {251 this.enterDrawState(parent.player, parent.draws_remaining);252 } else {253 this.startInfectionPhase(parent.player);254 }255 };256 this.resumePlayerActions = function() {257 if (this.situation.state.parent.name !== "player_actions") {258 throw "invalid state";259 }260 this.situation.state = this.situation.state.parent;261 this.situation.state.actions_remaining--;262 if (this.situation.state.actions_remaining === 0) {263 this.enterDrawState(player, 2);264 } else {265 this.emitStateChange();266 }267 };268 this.discardPlayerCard = function(player, card) {269 var thePlayer = this.findPlayer(player);270 eventSink.emit({271 "event_type": "discard_player_card",272 "player": player,273 "card": card274 });275 thePlayer.hand.splice(_.indexOf(thePlayer.hand, card), 1);276 this.situation.player_cards_discard.unshift(card);277 };278 this.requestApproval = function(player, other, action) {279 this.situation.state = {280 "name": "approve_action",281 "player": player,282 "approve_player": other,283 "approve_action": action,284 "parent": this.situation.state,285 "terminal": false286 };287 this.emitStateChange();288 };289 this.act = function(player, action) {290 if (action.name === "refuse_action") {291 if (this.situation.state.name !== "approve_action") {292 return false;293 }294 if (this.situation.state.approve_player !== player) {295 return false;296 }297 this.situation.state = this.situation.state.parent;298 this.emitStateChange();299 return true;300 }301 var approved = false;302 if (action.name === "approve_action") {303 if (this.situation.state.name !== "approve_action") {304 return false;305 }306 if (this.situation.state.approve_player !== player) {307 return false;308 }309 action = this.situation.state.approve_action;310 player = this.situation.state.player;311 this.situation.state = this.situation.state.parent;312 approved = true;313 }314 if (action.name.match(/^action_/)) {315 if (this.situation.state.name !== "player_actions") {316 return false;317 }318 if (player !== this.situation.state.player) {319 return false;320 }321 if (action.name === "action_pass") {322 } else if (action.name === "action_drive") {323 var thePlayer = this.findPlayer(player);324 if (player !== action.player && thePlayer.role !== "Dispatcher") {325 return false;326 }327 var other = action.player;328 var theOther = this.findPlayer(other);329 if (!theOther) {330 console.log("Invalid player ", other);331 return false;332 }333 var source = this.findLocation(theOther.location);334 if (!_.contains(source.adjacent, action.location)) {335 return false;336 }337 if (!approved && other !== player) {338 this.requestApproval(player, other, action);339 return true;340 } else {341 theOther.location = action.location;342 eventSink.emit({343 "event_type": "move_pawn",344 "player": other,345 "location": action.location346 });347 }348 } else if (action.name === "action_charter_flight") {349 var thePlayer = this.findPlayer(player);350 if (player !== action.player && thePlayer.role !== "Dispatcher") {351 return false;352 }353 var other = action.player;354 var theOther = this.findPlayer(other);355 if (theOther.location === action.location) {356 return false;357 }358 var card = _.find(thePlayer.hand, function(card) {359 return card.location === thePlayer.location; })360 if (!card) {361 return false;362 }363 if (!approved && other !== player) {364 this.requestApproval(player, other, action);365 return true;366 } else {367 this.discardPlayerCard(player, card);368 theOther.location = action.location;369 eventSink.emit({370 "event_type": "move_pawn",371 "player": other,372 "location": action.location373 });374 }375 } else if (action.name === "action_direct_flight") {376 var thePlayer = this.findPlayer(player);377 if (player !== action.player && thePlayer.role !== "Dispatcher") {378 return false;379 }380 var other = action.player;381 var theOther = this.findPlayer(other);382 if (theOther.location === action.location) {383 return false;384 }385 var card = _.find(thePlayer.hand, function(card) {386 return card.location === action.location; })387 if (!card) {388 return false;389 }390 if (!approved && other !== player) {391 this.requestApproval(player, other, action);392 return true;393 } else {394 this.discardPlayerCard(player, card);395 theOther.location = action.location;396 eventSink.emit({397 "event_type": "move_pawn",398 "player": other,399 "location": action.location400 });401 }402 } else if (action.name === "action_shuttle_flight") {403 var thePlayer = this.findPlayer(player);404 if (player !== action.player && thePlayer.role !== "Dispatcher") {405 return false;406 }407 var other = action.player;408 var theOther = this.findPlayer(other);409 var origin = theOther.location;410 var destination = action.location;411 if (origin === destination) {412 return false;413 }414 if (!_.find(this.situation.research_centers, function(center) {415 return center.location === origin;416 })) {417 return false;418 }419 if (!_.find(this.situation.research_centers, function(center) {420 return center.location === destination;421 })) {422 return false;423 }424 if (!approved && other !== player) {425 this.requestApproval(player, other, action);426 return true;427 } else {428 theOther.location = action.location;429 eventSink.emit({430 "event_type": "move_pawn",431 "player": other,432 "location": action.location433 });434 }435 } else if (action.name === "action_converge") {436 var thePlayer = this.findPlayer(player);437 if (thePlayer.role !== "Dispatcher") {438 return false;439 }440 var other = action.player;441 var theOther = this.findPlayer(other);442 if (!theOther) {443 console.log("Invalid player ", other);444 return false;445 }446 if (theOther.location === action.location) {447 return false;448 }449 if (!_.find(this.situation.players, function(player) {450 return player.location === action.location;451 })) {452 return false;453 }454 if (!approved && other !== player) {455 this.requestApproval(player, other, action);456 return true;457 } else {458 theOther.location = action.location;459 eventSink.emit({460 "event_type": "move_pawn",461 "player": other,462 "location": action.location463 });464 }465 } else if (action.name === "action_treat_disease") {466 var thePlayer = this.findPlayer(player);467 var location = this.findLocation(thePlayer.location);468 var disease = this.findDisease(action.disease);469 if (location.infections[disease.name] === 0) {470 return false;471 }472 var number = 1;473 if (disease.status === "cure_discovered" || thePlayer.role === "Medic") {474 number = location.infections[disease.name];475 }476 location.infections[disease.name] -= number;477 disease.cubes += number;478 eventSink.emit({479 "event_type": "treat_disease",480 "location": location.name,481 "disease": disease.name,482 "number": number483 });484 } else if (action.name === "action_build_research_center") {485 var thePlayer = this.findPlayer(player);486 if (this.situation.research_centers_available === 0) {487 return false;488 }489 if (_.find(this.situation.research_centers, function(center) {490 return center.location === thePlayer.location; })) {491 return false;492 };493 if (thePlayer.role !== "Operations Expert") {494 var card = _.find(thePlayer.hand, function(card) {495 return card.location === thePlayer.location;496 });497 if (!card) {498 return false;499 }500 this.discardPlayerCard(player, card);501 }502 eventSink.emit({503 "event_type": "build_research_center",504 "location": thePlayer.location505 });506 this.situation.research_centers.push({ "location": thePlayer.location });507 this.situation.research_centers_available--;508 } else if (action.name === "action_discover_cure") {509 var self = this;510 var thePlayer = this.findPlayer(player);511 if (thePlayer.role === "Scientist") {512 if (action.cards.length !== 4) return false;513 } else {514 if (action.cards.length !== 5) return false;515 }516 var cards = _.map(action.cards, function(card) {517 return _.find(thePlayer.hand, function(handCard) {518 return _.isEqual(handCard, card);519 });520 });521 if (_.some(cards, _.isUndefined)) {522 return false;523 }524 var disease = self.findDiseaseByLocation(cards[0].location);525 if (disease.status !== "no_cure") {526 return false;527 }528 if (!_.every(cards, function(card) {529 return self.findDiseaseByLocation(card.location) === disease; })) {530 return false;531 }532 _.each(cards, function(card) {533 self.discardPlayerCard(player, card);534 });535 disease.status = "cure_discovered"536 eventSink.emit({537 "event_type": "discover_cure",538 "disease": disease.name 539 });540 } else if (action.name === "action_share_knowledge") {541 var from = this.findPlayer(action.from_player);542 var to = this.findPlayer(action.to_player);543 if (!from || !to || from.id == to.id) {544 return false;545 }546 var card = _.find(from.hand, function(card) {547 return card.location === action.location; })548 if (!card) {549 return false;550 }551 if (from.location !== to.location) {552 return false;553 }554 if (from.role !== "Researcher" && from.location !== action.location) {555 return false;556 }557 if (player === to.id || player === from.id) {558 var other = player === to.id ? from.id : to.id;559 if (!approved) {560 this.situation.state = {561 "name": "approve_action",562 "player": player,563 "approve_player": other,564 "approve_action": action,565 "parent": this.situation.state,566 "terminal": false567 };568 this.emitStateChange();569 return true;570 } else {571 from.hand.splice(_.indexOf(from.hand, card), 1);572 to.hand.push(card);573 eventSink.emit({574 "event_type": "transfer_player_card",575 "from_player": from.id,576 "to_player": to.id,577 "card": card578 });579 if (to.hand.length > this.situation.max_player_cards) {580 return this.handleHandLimitExceeded(to.id);581 }582 }583 } else {584 return false;585 }586 } else {587 return false;588 }589 this.situation.state.actions_remaining--;590 if (this.situation.state.actions_remaining === 0) {591 this.enterDrawState(player, 2);592 } else {593 this.emitStateChange();594 }595 } else if (action.name === "special_airlift") {596 if (this.situation.state.name === "epidemic") {597 return false;598 }599 var thePlayer = this.findPlayer(player);600 var other = action.player;601 var theOther = this.findPlayer(other);602 if (!theOther) { 603 return false;604 }605 if (theOther.location === action.location) {606 return false;607 }608 var card = _.find(thePlayer.hand, function(card) {609 return card.special === "special_airlift";610 });611 if (!card) {612 return false;613 }614 if (!approved && other !== player) {615 this.requestApproval(player, other, action);616 return true;617 } else {618 this.discardPlayerCard(player, card);619 theOther.location = action.location;620 eventSink.emit({621 "event_type": "move_pawn",622 "player": other,623 "location": action.location624 });625 }626 } else if (action.name === "special_government_grant") {627 if (this.situation.state.name === "epidemic") {628 return false;629 }630 var thePlayer = this.findPlayer(player);631 var card = _.find(thePlayer.hand, function(card) {632 return card.special === action.name;633 });634 if (!card) {635 return false;636 }637 if (this.situation.research_centers_available === 0) {638 return false;639 }640 if (_.find(this.situation.research_centers, function(center) {641 return center.location === action.location; })) {642 return false;643 };644 this.discardPlayerCard(player, card);645 eventSink.emit({646 "event_type": "build_research_center",647 "location": action.location648 });649 this.situation.research_centers.push({ "location": thePlayer.location });650 this.situation.research_centers_available--;651 } else if (action.name === "draw_player_card") {652 if (this.situation.state.name !== "draw_player_cards") {653 return false;654 }655 if (player !== this.situation.state.player) {656 return false;657 }658 if (!this.drawPlayerCard(player)) { // Defeat659 return true;660 }661 if (this.situation.state.draws_remaining === 0) {662 this.startInfectionPhase(player);663 } else if (this.situation.state.name === "draw_player_cards") {664 this.enterDrawState(player, this.situation.state.draws_remaining);665 } else {666 this.emitStateChange();667 }668 } else if (action.name === "discard_player_card") {669 var thePlayer = this.findPlayer(player);670 var card = _.find(thePlayer.hand, function(card) {671 return _.isEqual(card, action.card);672 });673 if (!card) {674 return false;675 }676 this.discardPlayerCard(player, card);677 if (this.situation.state.name === "hand_limit_exceeded" &&678 this.situation.state.player === player &&679 thePlayer.hand.length <= this.situation.max_player_cards) {680 if (this.situation.state.parent.name === "player_actions") {681 this.resumePlayerActions();682 } else {683 this.resumeDrawPlayerCards();684 }685 }686 } else if (action.name === "increase_infection_intensity") {687 if (this.situation.state.name !== "epidemic") {688 return false;689 }690 if (player !== this.situation.state.player) {691 return false;692 }693 var cards = randy.shuffle(this.situation.infection_cards_discard);694 eventSink.emit({695 "event_type": "infection_cards_restack",696 "cards": cards697 });698 this.situation.infection_cards_discard = [];699 this.situation.infection_cards_draw = cards.concat(this.situation.infection_cards_draw);700 this.resumeDrawPlayerCards();701 } else if (action.name === "draw_infection_card") {702 if (this.situation.state.name !== "draw_infection_cards") {703 return false;704 }705 if (player !== this.situation.state.player) {706 return false;707 }708 if (!this.drawInfection(1)) { // Defeat709 return true;710 }711 this.situation.state.draws_remaining--;712 if (this.situation.state.draws_remaining === 0) {713 var players = this.situation.players;714 var index = _.indexOf(players, _.find(players, function(p) {715 return p.id === player;716 }));717 var nextPlayer = index + 1 === players.length ? players[0] : players[index + 1];718 this.situation.state = {719 "name": "player_actions",720 "player": nextPlayer.id,721 "actions_remaining": 4,722 "terminal": false723 };724 }725 this.emitStateChange();726 } else {727 return false;728 }729 var medic = _.find(this.situation.players, function(player) { return player.role === "Medic"; });730 if (medic) {731 var location = this.findLocation(medic.location);732 var cured = _.filter(this.situation.diseases, function(disease) { return disease.status === "cure_discovered"; });733 _.each(cured, function(disease) {734 var number = location.infections[disease.name];735 if (number > 0) {736 location.infections[disease.name] -= number;737 disease.cubes += number;738 eventSink.emit({739 "event_type": "treat_disease",740 "location": location.name,741 "disease": disease.name,742 "number": number743 });744 }745 });746 }747 var eradicated = _.filter(this.situation.diseases, function(disease) {748 return disease.status === "cure_discovered" && disease.cubes === disease.cubes_total;749 });750 for (i in eradicated) {751 var disease = eradicated[i];752 disease.status = "eradicated";753 eventSink.emit({754 "event_type": "eradicate_disease",755 "disease": disease.name756 });757 }758 var active = _.find(this.situation.diseases, function(disease) {759 return disease.status !== "eradicated";760 });761 if (!active) {762 this.situation.state = { "name": "victory", "terminal": true };763 this.emitStateChange();764 }765 return true;766 };767 return this;768}...
states.js
Source:states.js
...62 throw new Error(`micosmo:component:states:update: 'dispersePattern' is required.`);63 this.fDisperseMethod = methodNameBuilder(this.data.dispersePattern, /%A/, /%S/);64 },65 chain(state, fromCtxt, toCtxt) {66 Threadlet.DefaultPriority.run(() => { emitStateChange(this, this.intState.currentState, state, fromCtxt, toCtxt, 'chain') });67 },68 call(state, fromCtxt, toCtxt) {69 Threadlet.DefaultPriority.run(() => { callAndEmit(this, state, fromCtxt, toCtxt) });70 },71 return(state, fromCtxt, toCtxt) {72 Threadlet.DefaultPriority.run(() => { returnAndEmit(this, state, fromCtxt, toCtxt) });73 },74 syncChain(state, fromCtxt, toCtxt) { emitStateChange(this, this.intState.currentState, state, fromCtxt, toCtxt, 'chain') },75 syncCall(state, fromCtxt, toCtxt) { callAndEmit(this, state, fromCtxt, toCtxt) },76 syncReturn(state, fromCtxt, toCtxt) { returnAndEmit(this, state, fromCtxt, toCtxt) },77});78function callAndEmit(states, state, fromCtxt, toCtxt) {79 const curState = states.intState.currentState;80 states.callStack.push(curState);81 emitStateChange(states, curState, state, fromCtxt, toCtxt, 'call');82}83function returnAndEmit(states, state, fromCtxt, toCtxt) {84 if (states.callStack.length === 0)85 throw new Error(`micosmo:component:states:returnAndEmit: Call stack is empty.`);86 const oldState = states.callStack.pop();87 emitStateChange(states, states.intState.currentState, state || oldState, fromCtxt, toCtxt, 'return');88}89function emitStateChange(states, fromState, toState, fromCtxt, toCtxt, op) {90 if (!states.data.list.includes(toState))91 throw new Error(`micosmo:component:states:emitStateChange: State '${toState}' is not defined`);92 const evtDetail = requestObject();93 evtDetail.disperseEvent = disperseEvent; evtDetail.states = states; evtDetail.op = op;94 evtDetail.from = createContextObject(fromCtxt, fromState, 'exit');95 evtDetail.to = createContextObject(toCtxt, toState, 'enter');96 states.intState.currentState = toState;97 states.el.emit(states.data.event, evtDetail, false);98 returnObject(evtDetail); // Will automatically cleanup from/to context objects99}100function createContextObject(ctxt, state, defAction) {101 const oCtxt = requestObject();102 oCtxt.state = state || '<nos>';103 if (ctxt) typeof ctxt === 'string' ? parseNameValues(ctxt, oCtxt) : copyValues(ctxt, oCtxt);...
fork.js
Source:fork.js
...71 env: {NODE_ENV: 'test', ...process.env, ...options.environmentVariables, AVA_PATH},72 execArgv73 });74 subprocess.stdout.on('data', chunk => {75 emitStateChange({type: 'worker-stdout', chunk});76 });77 subprocess.stderr.on('data', chunk => {78 emitStateChange({type: 'worker-stderr', chunk});79 });80 const bufferedSend = controlFlow(subprocess);81 let forcedExit = false;82 const send = evt => {83 if (!finished && !forcedExit) {84 bufferedSend({ava: evt});85 }86 };87 const promise = new Promise(resolve => {88 const finish = () => {89 finished = true;90 resolve();91 };92 subprocess.on('message', message => {93 if (!message.ava) {94 return;95 }96 switch (message.ava.type) {97 case 'ready-for-options':98 send({type: 'options', options});99 break;100 case 'shared-worker-connect': {101 const channel = new SharedWorkerChannel(message.ava, send);102 sharedWorkerChannels.set(channel.id, channel);103 emitter.emit('connectSharedWorker', channel);104 break;105 }106 case 'shared-worker-message':107 sharedWorkerChannels.get(message.ava.channelId).emitMessage(message.ava);108 break;109 case 'ping':110 send({type: 'pong'});111 break;112 default:113 emitStateChange(message.ava);114 }115 });116 subprocess.on('error', error => {117 emitStateChange({type: 'worker-failed', err: error});118 finish();119 });120 subprocess.on('exit', (code, signal) => {121 if (forcedExit) {122 emitStateChange({type: 'worker-finished', forcedExit});123 } else if (code > 0) {124 emitStateChange({type: 'worker-failed', nonZeroExitCode: code});125 } else if (code === null && signal) {126 emitStateChange({type: 'worker-failed', signal});127 } else {128 emitStateChange({type: 'worker-finished', forcedExit});129 }130 finish();131 });132 });133 return {134 file,135 forkId,136 promise,137 exit() {138 forcedExit = true;139 subprocess.kill();140 },141 notifyOfPeerFailure() {142 send({type: 'peer-failed'});...
Using AI Code Generation
1const availableStateChangeNotifier = require('./availableStateChangeNotifier');2availableStateChangeNotifier.emitStateChange('on');3availableStateChangeNotifier.emitStateChange('off');4const EventEmitter = require('events');5class AvailableStateChangeNotifier extends EventEmitter { }6const availableStateChangeNotifier = new AvailableStateChangeNotifier();7module.exports = availableStateChangeNotifier;8const availableStateChangeNotifier = require('./availableStateChangeNotifier');9availableStateChangeNotifier.on('stateChange', (state) => {10 console.log(`State changed to ${state}`);11});12const availableStateChangeNotifier = require('./availableStateChangeNotifier');13availableStateChangeNotifier.on('stateChange', (state) => {14 console.log(`State changed to ${state}`);15});16availableStateChangeNotifier.emitStateChange('on');17availableStateChangeNotifier.emitStateChange('off');18const EventEmitter = require('events');19class AvailableStateChangeNotifier extends EventEmitter { }20const availableStateChangeNotifier = new AvailableStateChangeNotifier();21availableStateChangeNotifier.on('stateChange', (state) => {22 console.log(`State changed to ${state}`);23});24module.exports = availableStateChangeNotifier;
Using AI Code Generation
1var availabilityService = require('dw/svc/AvailabilityService');2availabilityService.emitStateChange('availabilityService', 'available');3var availabilityService = require('dw/svc/AvailabilityService');4availabilityService.emitStateChange('availabilityService', 'available');5var availabilityService = require('dw/svc/AvailabilityService');6availabilityService.emitStateChange('availabilityService', 'available');7var availabilityService = require('dw/svc/AvailabilityService');8availabilityService.emitStateChange('availabilityService', 'available');9var availabilityService = require('dw/svc/AvailabilityService');10availabilityService.emitStateChange('availabilityService', 'available');11var availabilityService = require('dw/svc/AvailabilityService');12availabilityService.emitStateChange('availabilityService', 'available');13var availabilityService = require('dw/svc/AvailabilityService');14availabilityService.emitStateChange('availabilityService', 'available');15var availabilityService = require('dw/svc/AvailabilityService');16availabilityService.emitStateChange('availabilityService', 'available');17var availabilityService = require('dw/svc/AvailabilityService');18availabilityService.emitStateChange('availabilityService', 'available');19var availabilityService = require('dw/svc/AvailabilityService');20availabilityService.emitStateChange('availabilityService', 'available');21var availabilityService = require('dw/svc/AvailabilityService');22availabilityService.emitStateChange('availabilityService', 'available');23var availabilityService = require('dw/svc/AvailabilityService');24availabilityService.emitStateChange('availabilityService', 'available');25var availabilityService = require('dw/svc/AvailabilityService');
Using AI Code Generation
1var avatar = AvatarList.getAvatar("example");2avatar.emitStateChange("exampleState", "exampleValue");3MyAvatar.emitStateChange("exampleState", "exampleValue");4AvatarManager.emitStateChange("exampleState", "exampleValue");5AvatarManager.stateChanged.connect(function(avatarID, stateName, stateValue) {6 print("Avatar with ID: " + avatarID + " changed state: " + stateName + " to value: " + stateValue);7});8var avatar = AvatarList.getAvatar("example");9avatar.stateChanged.connect(function(stateName, stateValue) {10 print("Avatar with ID: " + avatar.sessionUUID + " changed state: " + stateName + " to value: " + stateValue);11});12MyAvatar.stateChanged.connect(function(stateName, stateValue) {13 print("Avatar with ID: " + MyAvatar.sessionUUID + " changed state: " + stateName + " to value: " + stateValue);14});15AvatarList.stateChanged.connect(function(avatarID, stateName, stateValue) {16 print("Avatar with ID: " + avatarID + " changed state: " + stateName + " to value: " + stateValue);17});18var avatar = AvatarList.getAvatar("example");19avatar.stateChanged.connect(function(stateName, stateValue) {20 print("Avatar with ID: " + avatar.sessionUUID + " changed state: " + stateName + " to value: " + stateValue);21});22MyAvatar.stateChanged.connect(function(stateName, stateValue) {23 print("Avatar with ID: " + MyAvatar.sessionUUID + " changed state: " + stateName + " to value: " + stateValue);24});
Using AI Code Generation
1availableState.emitStateChange("New State");2availableState.onStateChange((state) => {3 console.log(state);4});5availableState.onStateChange((state) => {6 console.log(state);7});8const { availableState } = require("electron-ipc-state");9availableState.onStateChange((state) => {10 console.log(state);11});12availableState.emitStateChange("New State");13availableState.onStateChange((state) => {14 console.log(state);15});16availableState.onStateChange((state) => {17 console.log(state);18});19const { availableState } = require("electron-ipc-state");20availableState.onStateChange((state) => {21 console.log(state);22});23availableState.onStateChange((state) => {24 console.log(state);25});26availableState.onStateChange((state) => {27 console.log(state);28});29const { availableState } = require("electron-ipc-state");30availableState.onStateChange((state) => {
Using AI Code Generation
1var service = require('service');2var serviceInstance = service.createInstance();3serviceInstance.emitStateChange('stateChangeData');4var EventEmitter = require('events').EventEmitter;5var util = require('util');6var Service = function() {7 EventEmitter.call(this);8};9util.inherits(Service, EventEmitter);10Service.prototype.emitStateChange = function(data) {11 this.emit('stateChange', data);12};13Service.prototype.onStateChange = function(callback) {14 this.on('stateChange', callback);15};16module.exports = {17 createInstance: function() {18 return new Service();19 }20};21var service = require('service');22var serviceInstance = service.createInstance();23serviceInstance.onStateChange(function(data) {24 console.log('state changed', data);25});26var EventEmitter = require('events').EventEmitter;27var util = require('util');28var Service = function() {29 EventEmitter.call(this);30};31util.inherits(Service, EventEmitter);32Service.prototype.emitStateChange = function(data) {33 this.emit('stateChange', data);34};35Service.prototype.onStateChange = function(callback) {36 this.on('stateChange', callback);37};38module.exports = {39 createInstance: function() {40 return new Service();41 }42};43var EventEmitter = require('events').EventEmitter;44var util = require('util');45var MyEventEmitter = function() {46 EventEmitter.call(this);47};48util.inherits(MyEventEmitter, EventEmitter);49MyEventEmitter.prototype.emitStateChange = function(data) {50 this.emit('stateChange', data);51};52MyEventEmitter.prototype.onStateChange = function(callback) {53 this.on('stateChange', callback);54};55var emitter = new MyEventEmitter();56emitter.onStateChange(function(data) {57 console.log('state changed', data);58});59emitter.emitStateChange('stateChangeData');60var EventEmitter = require('events').EventEmitter;61var util = require('util');62var MyEventEmitter = function() {
Using AI Code Generation
1var state = require('state');2state.emitStateChange('test', 'state1');3var state = require('state');4state.emitStateChange('test', 'state1');5var state = require('state');6state.emitStateChange('test', 'state1');7var state = require('state');8state.emitStateChange('test', 'state1');9var state = require('state');10state.emitStateChange('test', 'state1');11var state = require('state');12state.emitStateChange('test', 'state1');13var state = require('state');14state.emitStateChange('test', 'state1');15var state = require('state');16state.emitStateChange('test', 'state1');17var state = require('state');18state.emitStateChange('test', 'state1');19var state = require('state');20state.emitStateChange('test', 'state1');21var state = require('state');22state.emitStateChange('test', 'state1');
Using AI Code Generation
1const injector = require('available-injector');2injector.emitStateChange({ state: 'running' });3const injector = require('available-injector');4injector.subscribeToStateChange((state) => {5 console.log(state);6});7injector.emitStateChange({ state: 'running' });8injector.subscribeToStateChange((state) => {9 console.log(state);10});11injector.getState();12injector.reset();13injector.getState();14injector.getInitialState();15injector.setInitialState({ state: 'running' });16injector.getAvailableModules();17 {18 },19 {20 },21 {22 },23];24injector.setAvailableModules(modules);25injector.getModuleState('module1');26injector.setModuleState('module1', 'running');27injector.getModuleDependencies('module1');28injector.setModuleDependencies('module1', ['module
Using AI Code Generation
1const availableServices = require('availableServices');2const service = availableServices.getAvailableService('service1');3service.emitStateChange('state1', 'state2');4### 10. Using availableServices.getAvailableService('serviceName').getStates()5const availableServices = require('availableServices');6const service = availableServices.getAvailableService('service1');7console.log(service.getStates());8### 11. Using availableServices.getAvailableService('serviceName').getStates('stateName')9const availableServices = require('availableServices');10const service = availableServices.getAvailableService('service1');11console.log(service.getStates('state1'));12### 12. Using availableServices.getAvailableService('serviceName').getStates('stateName').getValue()13const availableServices = require('availableServices');14const service = availableServices.getAvailableService('service1');15console.log(service.getStates('state1').getValue());16### 13. Using availableServices.getAvailableService('serviceName').getStates('stateName').setValue('stateValue')17const availableServices = require('availableServices');18const service = availableServices.getAvailableService('service1');19service.getStates('state1').setValue('stateValue');20### 14. Using availableServices.getAvailableService('serviceName').getStates('stateName').emitStateChange('oldValue', 'newValue')21const availableServices = require('availableServices');22const service = availableServices.getAvailableService('service1');23service.getStates('state1').emitStateChange('oldValue', 'newValue');24### 15. Using availableServices.getAvailableService('serviceName').getStates('stateName').onStateChange(callback)
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!!