refactor(node-watcher): adapt to new DI architecture
This commit moves the node watcher code to the presentation layer and
adapts it to be ready to use DI. Thus, the node watcher is separated
into a port and adapter.
Bug: twpowertools:226
Change-Id: Id36d5407ff478006eb8c057db1dcad05fd30b7d6
diff --git a/src/common/architecture/scripts/nodeWatcher/LegacyNodeWatcherScript.ts b/src/common/architecture/scripts/nodeWatcher/LegacyNodeWatcherScript.ts
index b4c07a5..211210d 100644
--- a/src/common/architecture/scripts/nodeWatcher/LegacyNodeWatcherScript.ts
+++ b/src/common/architecture/scripts/nodeWatcher/LegacyNodeWatcherScript.ts
@@ -1,4 +1,4 @@
-import NodeWatcherSingleton from '../../../nodeWatcher/NodeWatcher';
+import NodeWatcherSingleton, { NodeWatcherAdapter } from '../../../../infrastructure/presentation/nodeWatcher/NodeWatcher.adapter';
import { ConcreteNodeWatcherScriptHandler } from './handlers/NodeWatcherScriptHandler';
import Script from '../Script';
@@ -11,22 +11,28 @@
ConcreteNodeWatcherScriptHandler<Options>
>;
+ private nodeWatcher: NodeWatcherAdapter;
+
+ constructor() {
+ super();
+
+ // TODO(https://iavm.xyz/b/226): Retrieve this via constructor injection.
+ this.nodeWatcher = NodeWatcherSingleton.getInstance();
+ }
+
/**
* Resolves to the options when the script is executed.
*
- * This is so we can defer retrieving dependencies until the script is
- * executed, to prevent loading unnecessary things if they aren't needed
- * after all.
*/
protected abstract optionsFactory(): Options;
execute() {
- const nodeWatcher = NodeWatcherSingleton.getInstance();
const options = this.optionsFactory();
+ this.nodeWatcher.start();
for (const [key, handlerClass] of this.handlers) {
const handler = new handlerClass(options);
- nodeWatcher.setHandler(key, handler);
+ this.nodeWatcher.setHandler(key, handler);
}
}
}
diff --git a/src/common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler.ts b/src/common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler.ts
index b5c3698..8756b72 100644
--- a/src/common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler.ts
+++ b/src/common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler.ts
@@ -1,5 +1,5 @@
import { NodeWatcherScriptHandler } from './NodeWatcherScriptHandler';
-import { NodeMutation, NodeMutationType } from '../../../../nodeWatcher/NodeWatcherHandler';
+import { NodeMutation, NodeMutationType } from '../../../../../presentation/nodeWatcher/NodeWatcherHandler';
/**
* @deprecated
diff --git a/src/common/architecture/scripts/nodeWatcher/handlers/NodeWatcherScriptHandler.ts b/src/common/architecture/scripts/nodeWatcher/handlers/NodeWatcherScriptHandler.ts
index 5b0fd42..712f533 100644
--- a/src/common/architecture/scripts/nodeWatcher/handlers/NodeWatcherScriptHandler.ts
+++ b/src/common/architecture/scripts/nodeWatcher/handlers/NodeWatcherScriptHandler.ts
@@ -1,4 +1,4 @@
-import { NodeMutation, NodeWatcherHandler } from "../../../../nodeWatcher/NodeWatcherHandler";
+import { NodeMutation, NodeWatcherHandler } from "../../../../../presentation/nodeWatcher/NodeWatcherHandler";
export abstract class NodeWatcherScriptHandler<Options>
implements NodeWatcherHandler
diff --git a/src/features/autoRefresh/nodeWatcherHandlers/threadListHide.handler.ts b/src/features/autoRefresh/nodeWatcherHandlers/threadListHide.handler.ts
index d0a41de..9712c00 100644
--- a/src/features/autoRefresh/nodeWatcherHandlers/threadListHide.handler.ts
+++ b/src/features/autoRefresh/nodeWatcherHandlers/threadListHide.handler.ts
@@ -1,5 +1,5 @@
import CssSelectorNodeWatcherScriptHandler from '../../../common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler';
-import { NodeMutationType } from '../../../common/nodeWatcher/NodeWatcherHandler';
+import { NodeMutationType } from '../../../presentation/nodeWatcher/NodeWatcherHandler';
import { AutoRefreshNodeWatcherDependencies } from '../scripts/nodeWatcher.script';
/**
diff --git a/src/features/ccDarkTheme/nodeWatcherHandlers/ecApp.handler.ts b/src/features/ccDarkTheme/nodeWatcherHandlers/ecApp.handler.ts
index 12d50bb..fee1ffd 100644
--- a/src/features/ccDarkTheme/nodeWatcherHandlers/ecApp.handler.ts
+++ b/src/features/ccDarkTheme/nodeWatcherHandlers/ecApp.handler.ts
@@ -1,5 +1,5 @@
import CssSelectorNodeWatcherScriptHandler from '../../../common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler';
-import { NodeMutation } from '../../../common/nodeWatcher/NodeWatcherHandler';
+import { NodeMutation } from '../../../presentation/nodeWatcher/NodeWatcherHandler';
import { injectDarkThemeButton } from '../core/logic/darkTheme';
import { CCDarkThemeNodeWatcherDependencies } from '../scripts/nodeWatcher.script';
diff --git a/src/features/ccDarkTheme/nodeWatcherHandlers/reportDialog.handler.ts b/src/features/ccDarkTheme/nodeWatcherHandlers/reportDialog.handler.ts
index 83942e5..42c7a59 100644
--- a/src/features/ccDarkTheme/nodeWatcherHandlers/reportDialog.handler.ts
+++ b/src/features/ccDarkTheme/nodeWatcherHandlers/reportDialog.handler.ts
@@ -1,5 +1,5 @@
import CssSelectorNodeWatcherScriptHandler from '../../../common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler';
-import { NodeMutation } from '../../../common/nodeWatcher/NodeWatcherHandler';
+import { NodeMutation } from '../../../presentation/nodeWatcher/NodeWatcherHandler';
import { CCDarkThemeNodeWatcherDependencies } from '../scripts/nodeWatcher.script';
/**
diff --git a/src/features/ccDarkTheme/nodeWatcherHandlers/unifiedProfilesIframe.handler.ts b/src/features/ccDarkTheme/nodeWatcherHandlers/unifiedProfilesIframe.handler.ts
index b71b9a7..6343834 100644
--- a/src/features/ccDarkTheme/nodeWatcherHandlers/unifiedProfilesIframe.handler.ts
+++ b/src/features/ccDarkTheme/nodeWatcherHandlers/unifiedProfilesIframe.handler.ts
@@ -1,5 +1,5 @@
import CssSelectorNodeWatcherScriptHandler from '../../../common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler';
-import { NodeMutation } from '../../../common/nodeWatcher/NodeWatcherHandler';
+import { NodeMutation } from '../../../presentation/nodeWatcher/NodeWatcherHandler';
import { isDarkThemeOn } from '../core/logic/darkTheme';
import { unifiedProfilesFix } from '../core/logic/unifiedProfiles';
import { CCDarkThemeNodeWatcherDependencies } from '../scripts/nodeWatcher.script';
diff --git a/src/features/extraInfo/nodeWatcherHandlers/profile/ccExtraInfoProfileAbuseChips.handler.ts b/src/features/extraInfo/nodeWatcherHandlers/profile/ccExtraInfoProfileAbuseChips.handler.ts
index 7cc895a..482a456 100644
--- a/src/features/extraInfo/nodeWatcherHandlers/profile/ccExtraInfoProfileAbuseChips.handler.ts
+++ b/src/features/extraInfo/nodeWatcherHandlers/profile/ccExtraInfoProfileAbuseChips.handler.ts
@@ -1,5 +1,5 @@
import CssSelectorNodeWatcherScriptHandler from '../../../../common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler';
-import { NodeMutation } from '../../../../common/nodeWatcher/NodeWatcherHandler';
+import { NodeMutation } from '../../../../presentation/nodeWatcher/NodeWatcherHandler';
import { CCExtraInfoMainOptions } from '../../scripts/ccExtraInfoMain.script';
export default class CCExtraInfoProfileAbuseChipsHandler extends CssSelectorNodeWatcherScriptHandler<CCExtraInfoMainOptions> {
diff --git a/src/features/extraInfo/nodeWatcherHandlers/profile/ccExtraInfoProfilePerForumStats.handler.ts b/src/features/extraInfo/nodeWatcherHandlers/profile/ccExtraInfoProfilePerForumStats.handler.ts
index d08db21..bd2bb1c 100644
--- a/src/features/extraInfo/nodeWatcherHandlers/profile/ccExtraInfoProfilePerForumStats.handler.ts
+++ b/src/features/extraInfo/nodeWatcherHandlers/profile/ccExtraInfoProfilePerForumStats.handler.ts
@@ -1,5 +1,5 @@
import CssSelectorNodeWatcherScriptHandler from '../../../../common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler';
-import { NodeMutation } from '../../../../common/nodeWatcher/NodeWatcherHandler';
+import { NodeMutation } from '../../../../presentation/nodeWatcher/NodeWatcherHandler';
import { CCExtraInfoMainOptions } from '../../scripts/ccExtraInfoMain.script';
export default class CCExtraInfoProfilePerForumStatsHandler extends CssSelectorNodeWatcherScriptHandler<CCExtraInfoMainOptions> {
diff --git a/src/features/extraInfo/nodeWatcherHandlers/thread/ccExtraInfoThreadComment.handler.ts b/src/features/extraInfo/nodeWatcherHandlers/thread/ccExtraInfoThreadComment.handler.ts
index 802dc51..5527e1a 100644
--- a/src/features/extraInfo/nodeWatcherHandlers/thread/ccExtraInfoThreadComment.handler.ts
+++ b/src/features/extraInfo/nodeWatcherHandlers/thread/ccExtraInfoThreadComment.handler.ts
@@ -1,5 +1,5 @@
import CssSelectorNodeWatcherScriptHandler from '../../../../common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler';
-import { NodeMutation } from '../../../../common/nodeWatcher/NodeWatcherHandler';
+import { NodeMutation } from '../../../../presentation/nodeWatcher/NodeWatcherHandler';
import { CCExtraInfoMainOptions } from '../../scripts/ccExtraInfoMain.script';
/**
diff --git a/src/features/extraInfo/nodeWatcherHandlers/thread/ccExtraInfoThreadQuestion.handler.ts b/src/features/extraInfo/nodeWatcherHandlers/thread/ccExtraInfoThreadQuestion.handler.ts
index 0d16772..028b9a3 100644
--- a/src/features/extraInfo/nodeWatcherHandlers/thread/ccExtraInfoThreadQuestion.handler.ts
+++ b/src/features/extraInfo/nodeWatcherHandlers/thread/ccExtraInfoThreadQuestion.handler.ts
@@ -1,5 +1,5 @@
import CssSelectorNodeWatcherScriptHandler from '../../../../common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler';
-import { NodeMutation } from '../../../../common/nodeWatcher/NodeWatcherHandler';
+import { NodeMutation } from '../../../../presentation/nodeWatcher/NodeWatcherHandler';
import { CCExtraInfoMainOptions } from '../../scripts/ccExtraInfoMain.script';
/**
diff --git a/src/features/extraInfo/nodeWatcherHandlers/thread/ccExtraInfoThreadReply.handler.ts b/src/features/extraInfo/nodeWatcherHandlers/thread/ccExtraInfoThreadReply.handler.ts
index 543ddb0..94ad753 100644
--- a/src/features/extraInfo/nodeWatcherHandlers/thread/ccExtraInfoThreadReply.handler.ts
+++ b/src/features/extraInfo/nodeWatcherHandlers/thread/ccExtraInfoThreadReply.handler.ts
@@ -1,5 +1,5 @@
import CssSelectorNodeWatcherScriptHandler from '../../../../common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler';
-import { NodeMutation } from '../../../../common/nodeWatcher/NodeWatcherHandler';
+import { NodeMutation } from '../../../../presentation/nodeWatcher/NodeWatcherHandler';
import { CCExtraInfoMainOptions } from '../../scripts/ccExtraInfoMain.script';
/**
diff --git a/src/features/extraInfo/nodeWatcherHandlers/threadList/ccExtraInfoThreadList.handler.ts b/src/features/extraInfo/nodeWatcherHandlers/threadList/ccExtraInfoThreadList.handler.ts
index 5d26037..d2e7008 100644
--- a/src/features/extraInfo/nodeWatcherHandlers/threadList/ccExtraInfoThreadList.handler.ts
+++ b/src/features/extraInfo/nodeWatcherHandlers/threadList/ccExtraInfoThreadList.handler.ts
@@ -1,5 +1,5 @@
import CssSelectorNodeWatcherScriptHandler from '../../../../common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler';
-import { NodeMutation } from '../../../../common/nodeWatcher/NodeWatcherHandler';
+import { NodeMutation } from '../../../../presentation/nodeWatcher/NodeWatcherHandler';
import { CCExtraInfoMainOptions } from '../../scripts/ccExtraInfoMain.script';
/**
diff --git a/src/features/extraInfo/nodeWatcherHandlers/threadList/ccExtraInfoThreadListToolbelt.handler.ts b/src/features/extraInfo/nodeWatcherHandlers/threadList/ccExtraInfoThreadListToolbelt.handler.ts
index b6ef4dc..d28f6f8 100644
--- a/src/features/extraInfo/nodeWatcherHandlers/threadList/ccExtraInfoThreadListToolbelt.handler.ts
+++ b/src/features/extraInfo/nodeWatcherHandlers/threadList/ccExtraInfoThreadListToolbelt.handler.ts
@@ -1,5 +1,5 @@
import CssSelectorNodeWatcherScriptHandler from '../../../../common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler';
-import { NodeMutation } from '../../../../common/nodeWatcher/NodeWatcherHandler';
+import { NodeMutation } from '../../../../presentation/nodeWatcher/NodeWatcherHandler';
import { CCExtraInfoMainOptions } from '../../scripts/ccExtraInfoMain.script';
/**
diff --git a/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollLoadMoreBar.handler.ts b/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollLoadMoreBar.handler.ts
index e9f04ff..5c2d308 100644
--- a/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollLoadMoreBar.handler.ts
+++ b/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollLoadMoreBar.handler.ts
@@ -1,4 +1,4 @@
-import { NodeMutation } from '../../../common/nodeWatcher/NodeWatcherHandler';
+import { NodeMutation } from '../../../presentation/nodeWatcher/NodeWatcherHandler';
import CssSelectorNodeWatcherScriptHandler from '../../../common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler';
import { InfiniteScrollNodeWatcherOptions } from '../scripts/ccInfiniteScroll.script';
diff --git a/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollLoadMoreBtn.handler.ts b/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollLoadMoreBtn.handler.ts
index 8bf6fd2..1ab8644 100644
--- a/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollLoadMoreBtn.handler.ts
+++ b/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollLoadMoreBtn.handler.ts
@@ -1,4 +1,4 @@
-import { NodeMutation } from '../../../common/nodeWatcher/NodeWatcherHandler';
+import { NodeMutation } from '../../../presentation/nodeWatcher/NodeWatcherHandler';
import CssSelectorNodeWatcherScriptHandler from '../../../common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler';
import { InfiniteScrollNodeWatcherOptions } from '../scripts/ccInfiniteScroll.script';
diff --git a/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollSetUp.handler.ts b/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollSetUp.handler.ts
index 60331a0..b36e0b9 100644
--- a/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollSetUp.handler.ts
+++ b/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollSetUp.handler.ts
@@ -1,4 +1,4 @@
-import { NodeMutation } from '../../../common/nodeWatcher/NodeWatcherHandler';
+import { NodeMutation } from '../../../presentation/nodeWatcher/NodeWatcherHandler';
import CssSelectorNodeWatcherScriptHandler from '../../../common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler';
import { InfiniteScrollNodeWatcherOptions } from '../scripts/ccInfiniteScroll.script';
diff --git a/src/features/workflows/nodeWatcherHandlers/crTags.handler.ts b/src/features/workflows/nodeWatcherHandlers/crTags.handler.ts
index fbaee19..040c5ed 100644
--- a/src/features/workflows/nodeWatcherHandlers/crTags.handler.ts
+++ b/src/features/workflows/nodeWatcherHandlers/crTags.handler.ts
@@ -1,5 +1,5 @@
import CssSelectorNodeWatcherScriptHandler from '../../../common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler';
-import { NodeMutation } from '../../../common/nodeWatcher/NodeWatcherHandler';
+import { NodeMutation } from '../../../presentation/nodeWatcher/NodeWatcherHandler';
import { WorkflowsNodeWatcherDependencies } from '../scripts/nodeWatcher.script';
/**
diff --git a/src/features/workflows/nodeWatcherHandlers/threadListActionBar.handler.ts b/src/features/workflows/nodeWatcherHandlers/threadListActionBar.handler.ts
index ff62487..ab23f11 100644
--- a/src/features/workflows/nodeWatcherHandlers/threadListActionBar.handler.ts
+++ b/src/features/workflows/nodeWatcherHandlers/threadListActionBar.handler.ts
@@ -1,5 +1,5 @@
import { NodeWatcherScriptHandler } from '../../../common/architecture/scripts/nodeWatcher/handlers/NodeWatcherScriptHandler';
-import { NodeMutation } from '../../../common/nodeWatcher/NodeWatcherHandler';
+import { NodeMutation } from '../../../presentation/nodeWatcher/NodeWatcherHandler';
import { WorkflowsNodeWatcherDependencies } from '../scripts/nodeWatcher.script';
/**
diff --git a/src/common/nodeWatcher/NodeWatcher.ts b/src/infrastructure/presentation/nodeWatcher/NodeWatcher.adapter.ts
similarity index 85%
rename from src/common/nodeWatcher/NodeWatcher.ts
rename to src/infrastructure/presentation/nodeWatcher/NodeWatcher.adapter.ts
index 73b9560..0d46e47 100644
--- a/src/common/nodeWatcher/NodeWatcher.ts
+++ b/src/infrastructure/presentation/nodeWatcher/NodeWatcher.adapter.ts
@@ -2,20 +2,20 @@
NodeWatcherHandler,
NodeMutation,
NodeMutationType,
-} from './NodeWatcherHandler';
+} from '../../../presentation/nodeWatcher/NodeWatcherHandler';
-class NodeWatcher {
+export class NodeWatcherAdapter {
+ private started = false;
private handlers: Map<string, NodeWatcherHandler> = new Map();
- private mutationObserver: MutationObserver;
+ private mutationObserver: MutationObserver | undefined;
- constructor() {
+ start(): void {
+ if (this.started) return;
+
+ this.started = true;
this.mutationObserver = new MutationObserver(
this.mutationCallback.bind(this),
);
- this.start();
- }
-
- start(): void {
this.mutationObserver.observe(document.body, {
childList: true,
subtree: true,
@@ -27,6 +27,7 @@
}
setHandler(key: string, handler: NodeWatcherHandler): void {
+ this.start();
this.handlers.set(key, handler);
this.performInitialDiscovery(handler);
}
@@ -97,16 +98,16 @@
}
export default class NodeWatcherSingleton {
- private static instance: NodeWatcher;
+ private static instance: NodeWatcherAdapter;
/**
* @see {@link NodeWatcherSingleton.getInstance}
*/
private constructor() {}
- public static getInstance(): NodeWatcher {
+ public static getInstance(): NodeWatcherAdapter {
if (!NodeWatcherSingleton.instance) {
- NodeWatcherSingleton.instance = new NodeWatcher();
+ NodeWatcherSingleton.instance = new NodeWatcherAdapter();
}
return NodeWatcherSingleton.instance;
}
diff --git a/src/presentation/nodeWatcher/NodeWatcher.port.ts b/src/presentation/nodeWatcher/NodeWatcher.port.ts
new file mode 100644
index 0000000..83372b6
--- /dev/null
+++ b/src/presentation/nodeWatcher/NodeWatcher.port.ts
@@ -0,0 +1,23 @@
+import { NodeWatcherHandler } from "./NodeWatcherHandler";
+
+export interface NodeWatcherPort {
+ /**
+ * Start watching mutations to nodes.
+ */
+ start(): void;
+
+ /**
+ * Pause watching mutations to nodes.
+ */
+ pause(): void;
+
+ /**
+ * Add a handler to watch certain mutations.
+ */
+ setHandler(key: string, handler: NodeWatcherHandler): void;
+
+ /**
+ * Remove a handler by key.
+ */
+ removeHandler(key: string): boolean;
+}
diff --git a/src/common/nodeWatcher/NodeWatcherHandler.ts b/src/presentation/nodeWatcher/NodeWatcherHandler.ts
similarity index 100%
rename from src/common/nodeWatcher/NodeWatcherHandler.ts
rename to src/presentation/nodeWatcher/NodeWatcherHandler.ts