Skip to content

Commit

Permalink
Add stateTracker.queueCommand()
Browse files Browse the repository at this point in the history
  • Loading branch information
yishn committed Feb 20, 2020
1 parent 39acb98 commit d69544f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ controller of the engine that we're tracking the state of.

Returns a boolean whether the engine supports the given command.

#### `async stateTracker.queueCommand(command)`

- `command` [`<Command>`](#command)

Sends the given command to the engine after all ongoing syncs have finished.

#### `async stateTracker.sync(state)`

- `state` [`<EngineState>`](#enginestate)
Expand Down
11 changes: 11 additions & 0 deletions src/ControllerStateTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ class ControllerStateTracker {
return this._commands.includes(commandName)
}

async queueCommand(...args) {
if (this.syncing) {
await new Promise(r =>
this._syncFinishedEmitter.once('syncs-finished', r)
)
}

return this.controller.sendCommand(...args)
}

async _startProcessingSyncs() {
if (this.syncing) return

Expand All @@ -143,6 +153,7 @@ class ControllerStateTracker {
}

this.syncing = false
this._syncFinishedEmitter.emit('syncs-finished')
}

async _sync(state) {
Expand Down
25 changes: 25 additions & 0 deletions tests/ControllerStateTracker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,31 @@ t.test('sync history state', async t => {
t.strictDeepEquals(stateTracker.state.history, commands)
})

t.test('queueCommand will send command after syncs are done', async t => {
let {stateTracker} = t.context
let commands = [
{name: 'set_free_handicap', args: ['F4', 'G4', 'H4']},
{name: 'play', args: ['B', 'D4']},
{name: 'play', args: ['W', 'E4']}
]

let sentCommands = []

stateTracker.controller.on('command-sent', ({command}) => {
sentCommands.push(command)
})

let queuedCommand = {name: 'genmove', args: ['B']}

await Promise.all([
stateTracker.sync({history: commands}),
stateTracker.sync({history: commands}),
stateTracker.queueCommand(queuedCommand)
])

t.strictDeepEquals(sentCommands.slice(-1)[0], queuedCommand)
})

t.test('sync genmove commands', async t => {
let {stateTracker} = t.context
let history = []
Expand Down

0 comments on commit d69544f

Please sign in to comment.