Skip to content

Commit

Permalink
AG-16874 fix EventTracker
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 3ec9b65dd598735bf695db05ecedb0f82ea68b94
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Tue Oct 11 15:17:07 2022 +0300

    no extra type definition for applyRulesCallbackListener

commit 1ea678ea52366f63ee0de086de8d40dc58dc3cc4
Merge: 94dd26b e8286b1
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Tue Oct 11 12:06:58 2022 +0300

    Merge branch 'epic/AG-3532' into fix/AG-16874

commit 94dd26b7f3be35088e4de6bbb76c17676c9d6112
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Mon Oct 10 15:46:14 2022 +0300

    remove event listener while ExtendedCss.dispose()

commit fa99b5c7d90c20b1606128adc712c966b35295fb
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Mon Oct 10 15:38:02 2022 +0300

    remove eventTracker listeners on main document observer disconnect

commit 1ca59ee9795a4a2bea6a6aa9d6278eded5c31545
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Fri Oct 7 15:33:01 2022 +0300

    remove unused private lastEvent
  • Loading branch information
slavaleleka committed Oct 11, 2022
1 parent e8286b1 commit 4bd79b2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/extended-css/extended-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export class ExtendedCss {

private applyRulesScheduler: AsyncWrapper;

private applyRulesCallbackListener: () => void;

// Instance of ExtCssDocument is needed for using selector-ast cache
extCssDocument: ExtCssDocument;

Expand Down Expand Up @@ -128,6 +130,10 @@ export class ExtendedCss {
if (this.context.beforeStyleApplied && typeof this.context.beforeStyleApplied !== 'function') {
throw new Error(`Invalid configuration. Type of 'beforeStyleApplied' should be a function, received: '${typeof this.context.beforeStyleApplied}'`); // eslint-disable-line max-len
}

this.applyRulesCallbackListener = () => {
applyRules(this.context);
};
}

/**
Expand All @@ -139,7 +145,7 @@ export class ExtendedCss {
if (document.readyState !== 'complete') {
document.addEventListener(
'DOMContentLoaded',
() => { applyRules(this.context); },
this.applyRulesCallbackListener,
false,
);
}
Expand All @@ -153,6 +159,11 @@ export class ExtendedCss {
this.context.affectedElements.forEach((el) => {
revertStyle(el);
});
document.removeEventListener(
'DOMContentLoaded',
this.applyRulesCallbackListener,
false,
);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/extended-css/helpers/document-observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const observeDocument = (context: Context, callback: MainCallback): void => {
if (eventTracker.isIgnoredEventType() && shouldIgnoreMutations(mutations)) {
return;
}
// save instance of EventTracker to context
// for removing its event listeners on disconnectDocument() while mainDisconnect()
context.eventTracker = eventTracker;
callback();
}));
context.domMutationObserver.observe(document, {
Expand All @@ -45,6 +48,8 @@ const disconnectDocument = (context: Context, callback: MainCallback): void => {
document.removeEventListener('DOMNodeRemoved', callback, false);
document.removeEventListener('DOMAttrModified', callback, false);
}
// clean up event listeners
context.eventTracker?.stopTracking();
};

export const mainObserve = (context: Context, mainCallback: MainCallback): void => {
Expand Down
9 changes: 6 additions & 3 deletions src/extended-css/helpers/event-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ const SAFARI_PROBLEMATIC_EVENTS = ['wheel'];
export class EventTracker {
private trackedEvents: string[];

private lastEvent?: Event;

private lastEventType?: string;

private lastEventTime?: number;
Expand All @@ -42,7 +40,6 @@ export class EventTracker {
}

private trackEvent(event: Event): void {
this.lastEvent = event;
this.lastEventType = event.type;
this.lastEventTime = Date.now();
}
Expand All @@ -64,4 +61,10 @@ export class EventTracker {
&& !!sinceLastEventTime
&& sinceLastEventTime < LAST_EVENT_TIMEOUT_MS;
}

stopTracking(): void {
this.trackedEvents.forEach((eventName) => {
document.documentElement.removeEventListener(eventName, this.trackEvent, true);
});
}
}
6 changes: 6 additions & 0 deletions src/extended-css/helpers/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CssStyleMap, ExtCssRuleData } from '../../stylesheet';
import { EventTracker } from './event-tracker';
import { ExtMutationObserver } from './mutation-observer';

export type MainCallback = () => void;
Expand Down Expand Up @@ -78,6 +79,11 @@ export interface Context {
*/
isDomObserved: boolean;

/**
* Instance of EventTracker for document observing
*/
eventTracker?: EventTracker;

/**
* Main document mutation observer
*/
Expand Down

0 comments on commit 4bd79b2

Please sign in to comment.