Skip to content

Commit

Permalink
fix(grid): fix bug where internal hexes were cleared after Grid run()…
Browse files Browse the repository at this point in the history
… was called

fix #67
  • Loading branch information
flauwekeul committed Apr 22, 2021
1 parent 970f562 commit cb6c65d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/grid/grid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,13 +390,15 @@ describe('run()', () => {
expect(runCallback.mock.calls).toEqual([[createHex(hexPrototype, { q: 3, r: 4 }), grid]])
})

test(`doesn't run iterators again once run`, () => {
test(`doesn't run iterators again once run, but doesn't clear internal hexes either`, () => {
const eachCallback = jest.fn()
const runGrid = new Grid(hexPrototype, at({ q: 1, r: 2 })).each(eachCallback).run()
const hex = createHex(hexPrototype, { q: 1, r: 2 })
const runGrid = new Grid(hexPrototype, () => [hex]).each(eachCallback).run()

runGrid.run()
const twiceRunGrid = runGrid.run()

expect(runGrid.hexes()).toEqual([])
expect(eachCallback).toBeCalledTimes(1)
expect(runGrid.hexes()).toEqual([hex])
expect(twiceRunGrid.hexes()).toEqual([hex])
})
})
5 changes: 3 additions & 2 deletions src/grid/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ export class Grid<T extends Hex> {
}

run(callback?: Callback<T, void>) {
this.hexes().forEach((hex) => callback && callback(hex, this))
return this._clone(() => [])
const hexes = this._getPrevHexes(this)
hexes.forEach((hex) => callback && callback(hex, this))
return this._clone(() => hexes)
}

private _clone(getHexState: GetHexesFn<T>) {
Expand Down

0 comments on commit cb6c65d

Please sign in to comment.