Grid's update() doesn't clone grid correctly
flauwekeul opened this issue ยท 4 comments
Here's another grid bug that I just encountered:
test(`can mutate hexes inside update function`, () => {
const hexPrototype = createHexPrototype<TestHex>()
const hex = createHex(hexPrototype, { q: 0, r: 0 })
hex.test = 1
const newHex = cloneHex(hex)
newHex.test = 2
const callback = jest.fn((grid) => {
grid.store.set('0,0', newHex)
})
const grid = new Grid(hexPrototype, () => [hex])
const newGrid = grid.update(callback)
expect(newGrid.store.get('0,0')!.test).toBe(2)
expect(newGrid).not.toBe(grid)
newGrid
.each((hex) => {
expect(hex.test).toEqual(newHex.test)
})
.run()
})
I tried updating a hex as per the readme instructions, using grid.update()
and within the update()
method I called grid.store.set()
. This works, if you're calling grid.store.get()
to get the new hex. However, if you try using grid.each()
, you get the old hex returned.
Originally posted by @vfonic in #67 (comment)
I'll try to come back to this later today.
Thank you! I really appreciate it!
Take your time. I'm just messing around with the library on a small hobby project.
You found something I hadn't thought of yet (again), so that's very valuable ๐
I've changed update()
so that it always returns a grid that iterates over the hexes in its store. I think it's what people (like yourself) would expect.
This demonstrates the changed behaviour:
const sourceGrid = new Grid(hexPrototype, rectangle({ width: 2, height: 1 }))
const updatedGrid = sourceGrid.update((grid) => {
grid.store = new Map()
})
sourceGrid
.each((hex) => console.log(hex)) // Hex {q: 0, r: 0}, Hex {q: 1, r: 0}
.run()
updatedGrid
.each((hex) => console.log(hex)) // won't be called, because the store is empty
.run()
I'll release this in the next alpha.
๐ This issue has been resolved in version 4.0.0-beta.1 ๐
The release is available on:
Your semantic-release bot ๐ฆ๐