Skip to content

Commit

Permalink
EPUB: Fix freakout when search changes while running
Browse files Browse the repository at this point in the history
  • Loading branch information
AbeJellinek committed Aug 27, 2024
1 parent b15799a commit 931ad0d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/dom/epub/epub-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,7 @@ class EPUBView extends DOMView<EPUBViewState, EPUBViewData> {
|| previousState.entireWord !== state.entireWord
|| previousState.active !== state.active) {
console.log('Initiating new search', state);
this._find?.cancel();
this._find = new EPUBFindProcessor({
view: this,
findState: { ...state },
Expand Down
9 changes: 9 additions & 0 deletions src/dom/epub/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export class EPUBFindProcessor implements FindProcessor {

private _totalResults = 0;

private _cancelled = false;

private readonly _onSetFindState?: (state?: FindState) => void;

constructor(options: {
Expand All @@ -34,6 +36,7 @@ export class EPUBFindProcessor implements FindProcessor {
}

async run(startRange?: Range | PersistentRange, onFirstResult?: () => void) {
this._cancelled = false;
let startIndex = this.view.flow.startView
? this.view.views.indexOf(this.view.flow.startView)
: 0;
Expand All @@ -43,12 +46,17 @@ export class EPUBFindProcessor implements FindProcessor {
view,
this._selectedProcessor ? undefined : startRange
);
if (this._cancelled) return;
if (this._selectedProcessor === processor) {
onFirstResult?.();
}
}
}

cancel() {
this._cancelled = true;
}

async prev(): Promise<FindResult | null> {
if (this._selectedProcessor) {
this._selectedProcessor.prev(false);
Expand Down Expand Up @@ -150,6 +158,7 @@ export class EPUBFindProcessor implements FindProcessor {
}

private _setFindState() {
if (this._cancelled) return;
if (this._onSetFindState) {
let index = 0;
let foundSelected = false;
Expand Down

0 comments on commit 931ad0d

Please sign in to comment.