(startAt: InstanceId = 1)
| 21 | * Each call to allocate() returns the next sequential ID. |
| 22 | */ |
| 23 | export function createInstanceIdAllocator(startAt: InstanceId = 1): InstanceIdAllocator { |
| 24 | if (!Number.isInteger(startAt) || startAt < 0) { |
| 25 | throw new Error( |
| 26 | `createInstanceIdAllocator: startAt must be an integer >= 0 (got ${String(startAt)})`, |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | let next = startAt; |
| 31 | |
| 32 | return Object.freeze({ |
| 33 | allocate: () => { |
| 34 | const id = next; |
| 35 | next++; |
| 36 | return id; |
| 37 | }, |
| 38 | }); |
| 39 | } |
no outgoing calls