(proposedId: string)
| 35 | iteration <= 1 ? idBase : idBase + iteration.toString(); |
| 36 | |
| 37 | export const createId = (proposedId: string) => { |
| 38 | if (proposedId === undefined) { |
| 39 | // failsafe to avoid endless loops in error cases |
| 40 | proposedId = 'undefined'; |
| 41 | } |
| 42 | let state = prefixStates.get(proposedId); |
| 43 | if (state === undefined) { |
| 44 | state = { used: new Set<number>(), next: 0 }; |
| 45 | prefixStates.set(proposedId, state); |
| 46 | } |
| 47 | // Start from the smallest index that hasn't been allocated for this prefix. |
| 48 | // Holes left by removeId reset `next`, so released ids are reused. |
| 49 | let tries = state.next; |
| 50 | while (state.used.has(tries) || idSlots.has(makeId(proposedId, tries))) { |
| 51 | tries++; |
| 52 | } |
| 53 | const newId = makeId(proposedId, tries); |
| 54 | state.used.add(tries); |
| 55 | state.next = tries + 1; |
| 56 | idSlots.set(newId, { prefix: proposedId, index: tries }); |
| 57 | return newId; |
| 58 | }; |
| 59 | |
| 60 | export const removeId = (id: string) => { |
| 61 | const slot = idSlots.get(id); |
no test coverage detected
searching dependent graphs…