Skip to content

Commit

Permalink
Merge pull request #183 from ator-dev/highlighting-updated-listening-fix
Browse files Browse the repository at this point in the history
Use observer pattern for hi'ing-updated, fix Hi'te
  • Loading branch information
ator-dev authored Sep 9, 2024
2 parents f955dd8 + 0fdb7d1 commit 27f5dff
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 43 deletions.
6 changes: 4 additions & 2 deletions src/content.mts
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,10 @@ interface TermAppender<Async = true> {
diacritics: false,
},
};
const updateTermStatus = (term: MatchTerm) => getToolbarOrNull()?.updateTermStatus(term);
const highlighter: AbstractEngineManager = new EngineManager(updateTermStatus, termTokens, termPatterns);
const highlighter: AbstractEngineManager = new EngineManager(termTokens, termPatterns);
highlighter.addHighlightingUpdatedListener(() => {
getToolbarOrNull()?.updateStatuses();
});
const termSetterInternal: TermSetter<false> = {
setTerms: termsNew => {
if (itemsMatch(terms, termsNew, termEquals)) {
Expand Down
9 changes: 4 additions & 5 deletions src/modules/call-requester.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
* @param waitDuration Time to wait after the last request, before fulfilling it.
* @param reschedulingDelayMax Maximum total delay time between requests and fulfillment.
*/
const requestCallFn = function* (
const requestCallFn = (
call: () => void,
waitDuration: number,
reschedulingDelayMax: number,
) {
): (() => void) => {
const reschedulingRequestCountMargin = 1;
let timeRequestAcceptedLast = 0;
let requestCount = 0;
Expand All @@ -31,15 +31,14 @@ const requestCallFn = function* (
requestCount = 0;
call();
}, waitDuration + 20); // Arbitrary small amount added to account for lag (preventing lost updates).
while (true) {
return () => {
requestCount++;
const dateMs = Date.now();
if (dateMs > timeRequestAcceptedLast + waitDuration) {
timeRequestAcceptedLast = dateMs;
scheduleRefresh();
}
yield;
}
};
};

export { requestCallFn };
22 changes: 10 additions & 12 deletions src/modules/highlight/engine-manager.mts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { getContainerBlock } from "/dist/modules/highlight/container-blocks.mjs"
import type { Engine, PaintEngineMethod } from "/dist/modules/common.mjs";
import type { MatchTerm, TermTokens, TermPatterns } from "/dist/modules/match-term.mjs";
import { requestCallFn } from "/dist/modules/call-requester.mjs";
import type { UpdateTermStatus } from "/dist/content.mjs";
import { compatibility } from "/dist/modules/common.mjs";

interface AbstractEngineManager extends Highlighter, HighlighterCounterInterface, HighlighterWalkerInterface {
Expand Down Expand Up @@ -46,7 +45,7 @@ class EngineManager implements AbstractEngineManager {
readonly #termTokens: TermTokens;
readonly #termPatterns: TermPatterns;

readonly #updateTermStatus: UpdateTermStatus;
readonly #highlightingUpdatedListeners = new Set<() => void>();

#highlighting: {
terms: ReadonlyArray<MatchTerm>
Expand All @@ -58,13 +57,11 @@ class EngineManager implements AbstractEngineManager {
#specialEngine: AbstractSpecialEngine | null = null;

constructor (
updateTermStatus: UpdateTermStatus,
termTokens: TermTokens,
termPatterns: TermPatterns,
) {
this.#termTokens = termTokens;
this.#termPatterns = termPatterns;
this.#updateTermStatus = updateTermStatus;
}

getTermBackgroundStyle (colorA: string, colorB: string, cycle: number): string {
Expand Down Expand Up @@ -154,14 +151,11 @@ class EngineManager implements AbstractEngineManager {
break;
}}
}
engine.addHighlightingUpdatedListener(requestCallFn(
() => {
for (const term of terms.current) {
this.#updateTermStatus(term);
}
},
50, 500,
));
engine.addHighlightingUpdatedListener(() => {
for (const listener of this.#highlightingUpdatedListeners) {
listener();
}
});
return engineData;
}

Expand Down Expand Up @@ -264,6 +258,10 @@ class EngineManager implements AbstractEngineManager {
}
this.#specialEngine = null;
}

addHighlightingUpdatedListener (listener: () => void) {
this.#highlightingUpdatedListeners.add(listener);
}
}

export type { AbstractEngineManager };
Expand Down
4 changes: 2 additions & 2 deletions src/modules/highlight/engine.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ interface AbstractEngine extends Highlighter {

readonly deactivate: () => void

readonly addHighlightingUpdatedListener: (listener: Generator) => void

readonly getHighlightedElements: () => Iterable<HTMLElement>
}

Expand All @@ -44,6 +42,8 @@ interface HighlightingInterface {
) => void

readonly endHighlighting: () => void

readonly addHighlightingUpdatedListener: (listener: () => void) => void
}

export type {
Expand Down
16 changes: 8 additions & 8 deletions src/modules/highlight/engines/element.mts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class ElementEngine implements AbstractTreeEditEngine {

readonly #elementsJustHighlighted = new Set<HTMLElement>();

readonly #highlightingUpdatedListeners = new Set<() => void>();

readonly #styleManager = new StyleManager(new HTMLStylesheet(document.head));
readonly #termStyleManagerMap = new Map<MatchTerm, StyleManager<Record<never, never>>>();

Expand Down Expand Up @@ -106,8 +108,8 @@ ${HIGHLIGHT_TAG} {
highlightElementsThrottled();
}
//mutationUpdates.observe();
for (const listener of this.highlightingUpdatedListeners) {
listener.next();
for (const listener of this.#highlightingUpdatedListeners) {
listener();
}
});
this.#flowMutations = {
Expand Down Expand Up @@ -215,10 +217,8 @@ ${HIGHLIGHT_TAG} {
);
}

readonly highlightingUpdatedListeners = new Set<Generator>();

addHighlightingUpdatedListener (listener: Generator) {
this.highlightingUpdatedListeners.add(listener);
addHighlightingUpdatedListener (listener: () => void) {
this.#highlightingUpdatedListeners.add(listener);
}

/**
Expand Down Expand Up @@ -400,8 +400,8 @@ ${HIGHLIGHT_TAG} {
highlightInBlock(terms, nodeItems);
}
}
for (const listener of this.highlightingUpdatedListeners) {
listener.next();
for (const listener of this.#highlightingUpdatedListeners) {
listener();
}
};
})();
Expand Down
8 changes: 3 additions & 5 deletions src/modules/highlight/engines/highlight.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { AbstractTreeCacheEngine } from "/dist/modules/highlight/models/tre
import type { AbstractFlowTracker, Flow, Span } from "/dist/modules/highlight/models/tree-cache/flow-tracker.mjs";
import { FlowTracker } from "/dist/modules/highlight/models/tree-cache/flow-trackers/flow-tracker.mjs";
import * as TermCSS from "/dist/modules/highlight/term-css.mjs";
import { MatchTerm, type TermTokens, type TermPatterns } from "/dist/modules/match-term.mjs";
import type { MatchTerm, TermTokens, TermPatterns } from "/dist/modules/match-term.mjs";
import { StyleManager } from "/dist/modules/style-manager.mjs";
import { HTMLStylesheet } from "/dist/modules/stylesheets/html.mjs";
import { EleID, EleClass, createContainer, type AllReadonly } from "/dist/modules/common.mjs";
Expand All @@ -32,8 +32,6 @@ class HighlightEngine implements AbstractTreeCacheEngine {

readonly #highlights = new ExtendedHighlightRegistry();

readonly #highlightingUpdatedListeners = new Set<Generator>();

readonly #termStyleManagerMap = new Map<MatchTerm, StyleManager<Record<never, never>>>();

readonly terms = createContainer<ReadonlyArray<MatchTerm>>([]);
Expand Down Expand Up @@ -162,8 +160,8 @@ class HighlightEngine implements AbstractTreeCacheEngine {
return this.#elementFlowsMap.keys();
}

addHighlightingUpdatedListener (listener: Generator) {
this.#highlightingUpdatedListeners.add(listener);
addHighlightingUpdatedListener (listener: () => void) {
this.#flowTracker.addHighlightingUpdatedListener(listener);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/highlight/engines/paint.mts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ class PaintEngine implements AbstractTreeCacheEngine, HighlightingStyleObserver
return this.#elementHighlightingIdMap.keys();
}

addHighlightingUpdatedListener (listener: Generator) {
addHighlightingUpdatedListener (listener: () => void) {
this.#flowTracker.addHighlightingUpdatedListener(listener);
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/highlight/engines/paint/methods/element.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { AbstractMethod } from "/dist/modules/highlight/engines/paint/metho
import { getBoxesOwned } from "/dist/modules/highlight/engines/paint/boxes.mjs";
import type { HighlightingStyleObserver, Flow, Span, Box } from "/dist/modules/highlight/engines/paint.mjs";
import * as TermCSS from "/dist/modules/highlight/term-css.mjs";
import { MatchTerm, type TermTokens } from "/dist/modules/match-term.mjs";
import type { MatchTerm, TermTokens } from "/dist/modules/match-term.mjs";
import { StyleManager } from "/dist/modules/style-manager.mjs";
import { HTMLStylesheet } from "/dist/modules/stylesheets/html.mjs";
import type { AllReadonly } from "/dist/modules/common.mjs";
Expand Down
2 changes: 1 addition & 1 deletion src/modules/highlight/models/tree-cache/flow-tracker.mts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ interface AbstractFlowTracker extends FlowMutationObserver {
) => void

/** Adds a listener for changes in highlighting. */
readonly addHighlightingUpdatedListener: (listener: Generator) => void
readonly addHighlightingUpdatedListener: (listener: () => void) => void

/**
* Generates highlighting information for all text flows below the given element.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import type { Flow, Span, AbstractFlowTracker } from "/dist/modules/highlight/models/tree-cache/flow-tracker.mjs";
import { highlightTags } from "/dist/modules/highlight/highlight-tags.mjs";
import { matchInTextFlow } from "/dist/modules/highlight/matcher.mjs";
import { MatchTerm, type TermPatterns } from "/dist/modules/match-term.mjs";
import type { MatchTerm, TermPatterns } from "/dist/modules/match-term.mjs";
import type { RContainer, AllReadonly } from "/dist/modules/common.mjs";

class FlowTracker implements AbstractFlowTracker {
Expand All @@ -26,7 +26,7 @@ class FlowTracker implements AbstractFlowTracker {
#spansCreatedListener?: (flowOwner: HTMLElement, spansCreated: AllReadonly<Array<Span>>) => void;
#spansRemovedListener?: (flowOwner: HTMLElement, spansRemoved: AllReadonly<Array<Span>>) => void;
#nonSpanOwnerListener?: (flowOwner: HTMLElement) => void;
readonly #highlightingUpdatedListeners = new Set<Generator>();
readonly #highlightingUpdatedListeners = new Set<() => void>();

/**
*
Expand Down Expand Up @@ -146,7 +146,7 @@ class FlowTracker implements AbstractFlowTracker {
this.cacheFlowWithSpans(terms, textFlows[i]);
}
for (const listener of this.#highlightingUpdatedListeners) {
listener.next();
listener();
}
}

Expand Down Expand Up @@ -220,7 +220,7 @@ class FlowTracker implements AbstractFlowTracker {
this.#elementFlowsMap.clear();
}
for (const listener of this.#highlightingUpdatedListeners) {
listener.next();
listener();
}
}

Expand Down Expand Up @@ -332,7 +332,7 @@ class FlowTracker implements AbstractFlowTracker {
this.#nonSpanOwnerListener = listener;
}

addHighlightingUpdatedListener (listener: Generator) {
addHighlightingUpdatedListener (listener: () => void) {
this.#highlightingUpdatedListeners.add(listener);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/modules/highlight/tools/term-markers/tree-cache.mts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class TermMarker implements AbstractTermMarker {
hues: ReadonlyArray<number>,
highlightedElements: Iterable<HTMLElement>,
) {
this.setTermsStyle(terms, hues);
const termsSet = new Set(terms);
let markersHtml = "";
for (const element of highlightedElements) if (this.#elementFlowsMap.has(element)) {
Expand Down
8 changes: 7 additions & 1 deletion src/modules/interface/toolbar.mts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ interface AbstractToolbar {
readonly removeTerm: (term: MatchTerm | number) => void

/**
* Updates the look of the control to reflect whether or not its term currently occurs within the document.
* Updates the look of every term control, to reflect whether their terms currently occur within the document.
*/
readonly updateStatuses: () => void

/**
* Updates the look of a term control, to reflect whether its term currently occurs within the document.
* @param term The control's term.
*/
readonly updateTermStatus: (term: MatchTerm) => void

Expand Down
6 changes: 6 additions & 0 deletions src/modules/interface/toolbars/toolbar.mts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ class Toolbar implements AbstractToolbar, ToolbarTermControlInterface, ToolbarCo
this.refreshTermControls();
}

updateStatuses () {
for (const termControl of this.#termControls) {
termControl.updateStatus();
}
}

updateTermStatus (term: MatchTerm) {
const termToken = this.#termTokens.get(term);
this.#termControls.find(control => control.getTermToken() === termToken)?.updateStatus();
Expand Down

0 comments on commit 27f5dff

Please sign in to comment.