Skip to content

Commit

Permalink
Return Promise for blocking current state
Browse files Browse the repository at this point in the history
  • Loading branch information
rexrainbow committed Dec 21, 2024
1 parent 9e7b205 commit 53413fc
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 13 deletions.
5 changes: 5 additions & 0 deletions plugins/utils/object/IsPromise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var IsPromise = function (obj) {
return obj && (typeof obj.then === 'function');
}

export default IsPromise;
49 changes: 41 additions & 8 deletions templates/bejeweled/states/MainState.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import MatchState from './MatchState.js';
// Actions
import SelectChess from '../actions/SelectChess.js';
import SwapChess from '../actions/SwapChess.js'
import IsPromise from '../../../plugins/utils/object/IsPromise.js';

const GetValue = Phaser.Utils.Objects.GetValue;

Expand Down Expand Up @@ -105,11 +106,19 @@ class State extends BaseState {
// SELECT1
enter_SELECT1() {
var board = this.board.board,
bejeweled = this.bejeweled,
chess = this.selectedChess1;

this.bejeweled.emit('select1', chess, board, this.bejeweled);
this.bejeweled.emit('select1', chess, board, bejeweled);

this.select1Action(chess, board, this.bejeweled);
var result = this.select1Action(chess, board, bejeweled);
if (IsPromise(result)) {
bejeweled.waitEvent(bejeweled, 'select1.complete');
result
.then(function () {
bejeweled.emit('select1.complete');
})
}

// To next state when all completed
this.next();
Expand Down Expand Up @@ -145,11 +154,19 @@ class State extends BaseState {
// SELECT2
enter_SELECT2() {
var board = this.board.board,
bejeweled = this.bejeweled,
chess = this.selectedChess2;

this.bejeweled.emit('select2', chess, board, this.bejeweled);
this.bejeweled.emit('select2', chess, board, bejeweled);

this.select2Action(chess, board, this.bejeweled);
var result = this.select2Action(chess, board, bejeweled);
if (IsPromise(result)) {
bejeweled.waitEvent(bejeweled, 'select2.complete');
result
.then(function () {
bejeweled.emit('select2.complete');
})
}

// To next state when all completed
this.next();
Expand All @@ -162,12 +179,20 @@ class State extends BaseState {
// SWAP
enter_SWAP() {
var board = this.board.board,
bejeweled = this.bejeweled,
chess1 = this.selectedChess1,
chess2 = this.selectedChess2;

this.bejeweled.emit('swap', chess1, chess2, board, this.bejeweled);
this.bejeweled.emit('swap', chess1, chess2, board, bejeweled);

this.swapAction(chess1, chess2, board, this.bejeweled);
var result = this.swapAction(chess1, chess2, board, bejeweled);
if (IsPromise(result)) {
bejeweled.waitEvent(bejeweled, 'swap.complete');
result
.then(function () {
bejeweled.emit('swap.complete');
})
}

// To next state when all completed
this.next();
Expand Down Expand Up @@ -197,12 +222,20 @@ class State extends BaseState {
// UNDO_SWAP
enter_UNDOSWAP() {
var board = this.board.board,
bejeweled = this.bejeweled,
chess1 = this.selectedChess1,
chess2 = this.selectedChess2;

this.bejeweled.emit('undo-swap', chess1, chess2, board, this.bejeweled);
this.bejeweled.emit('undo-swap', chess1, chess2, board, bejeweled);

this.undoSwapAction(chess1, chess2, board, this.bejeweled);
var result = this.undoSwapAction(chess1, chess2, board, bejeweled);
if (IsPromise(result)) {
bejeweled.waitEvent(bejeweled, 'undo-swap.complete');
result
.then(function () {
bejeweled.emit('undo-swap.complete');
})
}

// To next state when all completed
this.next();
Expand Down
28 changes: 23 additions & 5 deletions templates/bejeweled/states/MatchState.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import BaseState from './BaseState.js';
// Actions
import EliminateChess from '../actions/EliminateChess.js';
import FallingAllChess from '../actions/FallingAllChess.js';
import IsPromise from '../../../plugins/utils/object/IsPromise.js';

const GetValue = Phaser.Utils.Objects.GetValue;
const SetStruct = Phaser.Structs.Set;
Expand Down Expand Up @@ -96,11 +98,19 @@ class State extends BaseState {
// ELIMINATING
enter_ELIMINATING() {
var board = this.board.board,
bejeweled = this.bejeweled,
chessArray = this.eliminatedChessArray;

this.bejeweled.emit('eliminate', chessArray, board, this.bejeweled);
this.bejeweled.emit('eliminate', chessArray, board, bejeweled);

this.eliminatingAction(chessArray, board, this.bejeweled);
var result = this.eliminatingAction(chessArray, board, bejeweled);
if (IsPromise(result)) {
bejeweled.waitEvent(bejeweled, 'eliminate.complete');
result
.then(function () {
bejeweled.emit('eliminate.complete');
})
}

// Remove eliminated chess
chessArray.forEach(board.removeChess, board);
Expand All @@ -118,11 +128,19 @@ class State extends BaseState {

// FALLING
enter_FALLING() {
var board = this.board.board;
var board = this.board.board,
bejeweled = this.bejeweled;

this.bejeweled.emit('fall', board, this.bejeweled);
this.bejeweled.emit('fall', board, bejeweled);

this.fallingAction(board, this.bejeweled);
var result = this.fallingAction(board, bejeweled);
if (IsPromise(result)) {
bejeweled.waitEvent(bejeweled, 'fall.complete');
result
.then(function () {
bejeweled.emit('fall.complete');
})
}

// To next state when all completed
this.next();
Expand Down

0 comments on commit 53413fc

Please sign in to comment.