| 298 | } |
| 299 | |
| 300 | _persistFreshContent() { |
| 301 | /** @typedef {{ items: Items, map: Content, loc: number }} PackItem */ |
| 302 | const itemsCount = this.freshContent.size; |
| 303 | if (itemsCount > 0) { |
| 304 | const packCount = Math.ceil(itemsCount / MAX_ITEMS_IN_FRESH_PACK); |
| 305 | const itemsPerPack = Math.ceil(itemsCount / packCount); |
| 306 | /** @type {PackItem[]} */ |
| 307 | const packs = []; |
| 308 | let i = 0; |
| 309 | let ignoreNextTimeTick = false; |
| 310 | const createNextPack = () => { |
| 311 | const loc = this._findLocation(); |
| 312 | this.content[loc] = /** @type {EXPECTED_ANY} */ (null); // reserve |
| 313 | /** @type {PackItem} */ |
| 314 | const pack = { |
| 315 | items: new Set(), |
| 316 | map: new Map(), |
| 317 | loc |
| 318 | }; |
| 319 | packs.push(pack); |
| 320 | return pack; |
| 321 | }; |
| 322 | let pack = createNextPack(); |
| 323 | if (this.requestsTimeout !== undefined) { |
| 324 | clearTimeout(this.requestsTimeout); |
| 325 | } |
| 326 | for (const identifier of this.requests) { |
| 327 | if (identifier === undefined) { |
| 328 | if (ignoreNextTimeTick) { |
| 329 | ignoreNextTimeTick = false; |
| 330 | } else if (pack.items.size >= MIN_ITEMS_IN_FRESH_PACK) { |
| 331 | i = 0; |
| 332 | pack = createNextPack(); |
| 333 | } |
| 334 | continue; |
| 335 | } |
| 336 | const info = this.freshContent.get(identifier); |
| 337 | if (info === undefined) continue; |
| 338 | pack.items.add(identifier); |
| 339 | pack.map.set(identifier, info.freshValue); |
| 340 | info.location = pack.loc; |
| 341 | info.freshValue = undefined; |
| 342 | this.freshContent.delete(identifier); |
| 343 | if (++i > itemsPerPack) { |
| 344 | i = 0; |
| 345 | pack = createNextPack(); |
| 346 | ignoreNextTimeTick = true; |
| 347 | } |
| 348 | } |
| 349 | this.requests.length = 0; |
| 350 | for (const pack of packs) { |
| 351 | this.content[pack.loc] = new PackContent( |
| 352 | pack.items, |
| 353 | new Set(pack.items), |
| 354 | new PackContentItems(pack.map) |
| 355 | ); |
| 356 | } |
| 357 | this.logger.log( |