* 获取一个可用的ID * * 返回一个32位ID,高16位为世代版本,低16位为索引。 * * @returns 新分配的实体ID * @throws {Error} 当达到索引限制时抛出错误
()
| 104 | * @throws {Error} 当达到索引限制时抛出错误 |
| 105 | */ |
| 106 | public checkOut(): number { |
| 107 | // 处理延迟回收队列 |
| 108 | this._processDelayedRecycle(); |
| 109 | |
| 110 | let index: number; |
| 111 | |
| 112 | if (this._freeIndices.length > 0) { |
| 113 | // 重用回收的索引 |
| 114 | index = this._freeIndices.pop()!; |
| 115 | } else { |
| 116 | // 分配新索引 |
| 117 | if (this._nextAvailableIndex > IdentifierPool.MAX_INDEX) { |
| 118 | throw new Error( |
| 119 | `实体索引已达到框架设计限制 (${IdentifierPool.MAX_INDEX})。` + |
| 120 | '这意味着您已经分配了超过65535个不同的实体索引。' + |
| 121 | '这是16位索引设计的限制,考虑优化实体回收策略或升级到64位ID设计。' |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | index = this._nextAvailableIndex++; |
| 126 | |
| 127 | // 按需扩展世代存储 |
| 128 | this._ensureGenerationCapacity(index); |
| 129 | } |
| 130 | |
| 131 | const generation = this._generations.get(index) || 1; |
| 132 | this._stats.totalAllocated++; |
| 133 | this._stats.currentActive++; |
| 134 | |
| 135 | return this._packId(index, generation); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * 回收一个ID |
no test coverage detected