Skip to content

Commit

Permalink
Use UUIDv4 for item IDs to prevent collisions
Browse files Browse the repository at this point in the history
The previous solution of using an incrementing integer would result in
collisions between items stored in state and beams, since beam IDs are
generated on the fly and not stored in state, and the unique ID was only
incremented when the ID was not stored in state. This makes item IDs a
little harder to read, but ensure they remain unique across all items.
  • Loading branch information
kflorence committed Jun 5, 2024
1 parent c52ed27 commit 78052ae
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/components/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Item extends Stateful {

constructor (parent, state, configuration) {
// Retain ID from state if it exists, otherwise generate a new one
state.id ??= Item.uniqueId++
state.id ??= Item.uniqueId()

super(state)

Expand Down Expand Up @@ -102,7 +102,7 @@ export class Item extends Stateful {
'wall'
].map((type) => [type, capitalize(type)])))

// This should be stable per puzzle as state refers to it
// Note that IDs will change if the puzzle configuration changes
static uniqueId = 0
static uniqueId () {
return crypto.randomUUID().split('-')[0]
}
}
3 changes: 0 additions & 3 deletions src/components/puzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,6 @@ export class Puzzle {
}

#setup () {
// Reset the item IDs, so they are unique per-puzzle
Item.uniqueId = 0

const { layout, message, solution } = this.#state.getCurrent()

this.layout = new Layout(layout)
Expand Down

0 comments on commit 78052ae

Please sign in to comment.