Fix WebGL page cache and flip sequencing

This commit is contained in:
2026-06-08 23:08:13 +02:00
parent a73dc5725f
commit 419691000c
7 changed files with 364 additions and 60 deletions
+36 -8
View File
@@ -54,6 +54,7 @@ class BookPaginationModule extends BaseModule {
'countLineWords',
'getLineGeometry',
'getSpread',
'findSpreadIndexForBlock',
'getCurrentSpread',
'setCurrentSpread',
'handlePageCountChanged',
@@ -99,11 +100,17 @@ class BookPaginationModule extends BaseModule {
const token = ++this.refreshToken;
const detail = event?.detail || {};
const gameId = detail.gameId || this.storyHistory?.currentGameId || null;
const latestRenderedBlockId = Math.max(
0,
Number(detail.latestRenderedBlockId || this.storyHistory?.latestRenderedBlockId || 0)
);
const latestBlockId = Math.max(
0,
Number(detail.latestRenderedBlockId || detail.latestBlockId || this.storyHistory?.latestRenderedBlockId || (this.storyHistory?.nextBlockId || 1) - 1)
Number(detail.latestBlockId || (this.storyHistory?.nextBlockId || 1) - 1 || latestRenderedBlockId)
);
if (!gameId || latestBlockId <= 0 || typeof this.storyHistory?.getBlocksRange !== 'function') {
const continuationBlockId = this.getContinuationBlockId(latestBlockId, latestRenderedBlockId);
const paginationEndBlockId = Math.max(latestRenderedBlockId, continuationBlockId);
if (!gameId || paginationEndBlockId <= 0 || typeof this.storyHistory?.getBlocksRange !== 'function') {
this.pages = this.buildPages([]);
this.spreads = this.buildSpreadsFromPages(this.pages);
this.latestBlockId = 0;
@@ -114,20 +121,31 @@ class BookPaginationModule extends BaseModule {
return;
}
const blocks = await this.storyHistory.getBlocksRange(gameId, 1, latestBlockId);
const blocks = await this.storyHistory.getBlocksRange(gameId, 1, paginationEndBlockId);
if (token !== this.refreshToken) return;
this.latestBlockId = latestBlockId;
this.latestRenderedBlockId = Math.max(
0,
Number(detail.latestRenderedBlockId || this.storyHistory?.latestRenderedBlockId || 0)
);
this.latestRenderedBlockId = latestRenderedBlockId;
this.pages = this.buildPages(blocks);
this.spreads = this.buildSpreadsFromPages(this.pages);
this.persistPaginationMetrics(this.pages);
this.currentSpreadIndex = Math.max(0, Math.min(this.currentSpreadIndex, Math.max(0, this.spreads.length - 1)));
const continuationSpreadIndex = this.findSpreadIndexForBlock(continuationBlockId);
const renderedSpreadIndex = this.findSpreadIndexForBlock(latestRenderedBlockId);
this.currentSpreadIndex = continuationSpreadIndex >= 0
? continuationSpreadIndex
: renderedSpreadIndex >= 0
? renderedSpreadIndex
: Math.max(0, Math.min(this.currentSpreadIndex, Math.max(0, this.spreads.length - 1)));
this.publish();
}
getContinuationBlockId(latestBlockId = 0, latestRenderedBlockId = 0) {
const latest = Math.max(0, Number(latestBlockId || 0));
const rendered = Math.max(0, Number(latestRenderedBlockId || 0));
if (latest <= 0) return 0;
if (rendered <= 0) return 1;
return rendered < latest ? rendered + 1 : latest;
}
async preparePendingBlock(block = {}, options = {}) {
const token = options.activate === false ? this.refreshToken : ++this.refreshToken;
const gameId = block.gameId || block.metadata?.gameId || this.storyHistory?.currentGameId || null;
@@ -857,6 +875,16 @@ class BookPaginationModule extends BaseModule {
return this.spreads[Math.max(0, Number(index || 0))] || { index: 0, left: [], right: [] };
}
findSpreadIndexForBlock(blockId) {
const id = Math.max(0, Number(blockId || 0));
if (id <= 0) return -1;
const spread = this.spreads.find(entry => ['left', 'right'].some((side) => {
const lines = Array.isArray(entry?.[side]) ? entry[side] : [];
return lines.some(line => Number(line?.blockId || 0) === id);
}));
return Number.isFinite(Number(spread?.index)) ? Math.max(0, Math.round(Number(spread.index))) : -1;
}
getCurrentSpread() {
return this.getSpread(this.currentSpreadIndex);
}