refactor(infinite-scroll): migrate to the new DI architecture

Bug: twpowertools:226

Change-Id: I5f4156204327cc66e976821134cef667d57f7233
diff --git a/src/common/architecture/dependenciesProvider/DependenciesProvider.ts b/src/common/architecture/dependenciesProvider/DependenciesProvider.ts
index 4f01b82..61a7425 100644
--- a/src/common/architecture/dependenciesProvider/DependenciesProvider.ts
+++ b/src/common/architecture/dependenciesProvider/DependenciesProvider.ts
@@ -1,6 +1,6 @@
 import ExtraInfo from '../../../features/extraInfo/core';
 import AutoRefresh from '../../../features/autoRefresh/core/autoRefresh';
-import OptionsProviderAdapter from '../../options/OptionsProvider';
+import OptionsProviderAdapter from '../../../infrastructure/services/options/OptionsProvider.adapter';
 import WorkflowsImport from '../../../features/workflows/core/communityConsole/import';
 import Workflows from '../../../features/workflows/core/communityConsole/workflows';
 import StartupDataStorage from '../../../contentScripts/communityConsole/utils/StartupDataStorage';
diff --git a/src/common/architecture/scripts/stylesheet/StylesheetScript.ts b/src/common/architecture/scripts/stylesheet/StylesheetScript.ts
index 2ed9025..4899436 100644
--- a/src/common/architecture/scripts/stylesheet/StylesheetScript.ts
+++ b/src/common/architecture/scripts/stylesheet/StylesheetScript.ts
@@ -1,10 +1,10 @@
 import StylesheetManager from '../../../StylesheetManager';
 import { StylesheetAttributes } from '../../../contentScriptsUtils';
-import OptionsProviderAdapter from '../../../options/OptionsProvider';
 import DependenciesProviderSingleton, {
   OptionsProviderDependency,
 } from '../../dependenciesProvider/DependenciesProvider';
 import Script, { ScriptEnvironment, ScriptRunPhase } from '../Script';
+import { OptionsProviderPort } from '../../../../services/options/OptionsProvider';
 
 /**
  * Script which injects a stylesheet depending on a set condition. It
@@ -23,7 +23,7 @@
    */
   readonly attributes: StylesheetAttributes = {};
 
-  protected optionsProvider: OptionsProviderAdapter;
+  protected optionsProvider: OptionsProviderPort;
   private stylesheetManager: StylesheetManager;
 
   constructor() {
diff --git a/src/entryPoints/communityConsole/contentScripts/main.ts b/src/entryPoints/communityConsole/contentScripts/main.ts
index fb6fa17..bb7fe6a 100644
--- a/src/entryPoints/communityConsole/contentScripts/main.ts
+++ b/src/entryPoints/communityConsole/contentScripts/main.ts
@@ -1,3 +1,6 @@
+// Run legacy Javascript entry point
+import '../../../contentScripts/communityConsole/main';
+
 import DependenciesProviderSingleton, {
   AutoRefreshDependency,
   OptionsProviderDependency,
@@ -11,7 +14,12 @@
 import AutoRefreshThreadListHideHandler from '../../../features/autoRefresh/presentation/nodeWatcherHandlers/threadListHide.handler';
 import AutoRefreshThreadListSetUpHandler from '../../../features/autoRefresh/presentation/nodeWatcherHandlers/threadListSetUp.handler';
 import AutoRefreshStylesScript from '../../../features/autoRefresh/presentation/scripts/styles.script';
+import ReportDialogColorThemeFix from '../../../features/ccDarkTheme/core/logic/reportDialog';
+import CCDarkThemeEcAppHandler from '../../../features/ccDarkTheme/nodeWatcherHandlers/ecApp.handler';
+import CCDarkThemeReportDialogHandler from '../../../features/ccDarkTheme/nodeWatcherHandlers/reportDialog.handler';
+import CCDarkThemeUnifiedProfilesIframeHandler from '../../../features/ccDarkTheme/nodeWatcherHandlers/unifiedProfilesIframe.handler';
 import Features from '../../../features/Features';
+import CCInfiniteScroll from '../../../features/infiniteScroll/core/ccInfiniteScroll';
 import { NodeWatcherAdapter } from '../../../infrastructure/presentation/nodeWatcher/NodeWatcher.adapter';
 import NodeWatcherScriptAdapter from '../../../infrastructure/presentation/scripts/NodeWatcherScript.adapter';
 import ScriptRunner from '../../../infrastructure/presentation/scripts/ScriptRunner';
@@ -19,13 +27,9 @@
 import { SortedScriptsProviderAdapter } from '../../../infrastructure/presentation/scripts/SortedScriptsProvider.adapter';
 import { NodeWatcherHandler } from '../../../presentation/nodeWatcher/NodeWatcherHandler';
 import StandaloneScripts from '../../../scripts/Scripts';
-
-// Run legacy Javascript entry point
-import '../../../contentScripts/communityConsole/main';
-import CCDarkThemeEcAppHandler from '../../../features/ccDarkTheme/nodeWatcherHandlers/ecApp.handler';
-import CCDarkThemeReportDialogHandler from '../../../features/ccDarkTheme/nodeWatcherHandlers/reportDialog.handler';
-import CCDarkThemeUnifiedProfilesIframeHandler from '../../../features/ccDarkTheme/nodeWatcherHandlers/unifiedProfilesIframe.handler';
-import ReportDialogColorThemeFix from '../../../features/ccDarkTheme/core/logic/reportDialog';
+import CCInfiniteScrollSetUpHandler from '../../../features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollSetUp.handler';
+import CCInfiniteScrollLoadMoreBarHandler from '../../../features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollLoadMoreBar.handler';
+import CCInfiniteScrollLoadMoreBtnHandler from '../../../features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollLoadMoreBtn.handler';
 
 const scriptRunner = createScriptRunner();
 scriptRunner.run();
@@ -37,6 +41,8 @@
     OptionsProviderDependency,
   );
 
+  const ccInfiniteScroll = new CCInfiniteScroll();
+
   const context: Context = {
     page: ScriptPage.CommunityConsole,
     environment: ScriptEnvironment.ContentScript,
@@ -70,6 +76,18 @@
               'ccDarkThemeUnifiedProfilesIframe',
               new CCDarkThemeUnifiedProfilesIframeHandler(optionsProvider),
             ],
+            [
+              'ccInfiniteScrollSetUp',
+              new CCInfiniteScrollSetUpHandler(ccInfiniteScroll),
+            ],
+            [
+              'ccInfiniteScrollLoadMoreBar',
+              new CCInfiniteScrollLoadMoreBarHandler(ccInfiniteScroll),
+            ],
+            [
+              'ccInfiniteScrollLoadMoreBtn',
+              new CCInfiniteScrollLoadMoreBtnHandler(ccInfiniteScroll),
+            ],
           ]),
         ),
 
diff --git a/src/features/Features.ts b/src/features/Features.ts
index 6bd74b0..d0861c5 100644
--- a/src/features/Features.ts
+++ b/src/features/Features.ts
@@ -1,5 +1,4 @@
 import Feature from '../common/architecture/features/Feature';
-import InfiniteScrollFeature from './infiniteScroll/infiniteScroll.feature';
 import ScriptFilterListProvider from '../common/architecture/scripts/ScriptFilterListProvider';
 import ExtraInfoFeature from './extraInfo/extraInfo.feature';
 import WorkflowsFeature from './workflows/workflows.feature';
@@ -11,7 +10,6 @@
 export default class Features extends ScriptFilterListProvider {
   private features: ConcreteFeatureClass[] = [
     ExtraInfoFeature,
-    InfiniteScrollFeature,
     InteropThreadPageFeature,
     LoadDraftsFeature,
     WorkflowsFeature,
diff --git a/src/features/infiniteScroll/infiniteScroll.feature.ts b/src/features/infiniteScroll/infiniteScroll.feature.ts
deleted file mode 100644
index b6dee6a..0000000
--- a/src/features/infiniteScroll/infiniteScroll.feature.ts
+++ /dev/null
@@ -1,11 +0,0 @@
-import Feature from '../../common/architecture/features/Feature';
-import { ConcreteScript } from '../../common/architecture/scripts/Script';
-import { OptionCodename } from '../../common/options/optionsPrototype';
-import CCInfiniteScrollScript from './scripts/ccInfiniteScroll.script';
-
-export default class InfiniteScrollFeature extends Feature {
-  public readonly scripts: ConcreteScript[] = [CCInfiniteScrollScript];
-
-  readonly codename = 'infiniteScroll';
-  readonly relatedOptions: OptionCodename[] = ['list', 'thread', 'threadall'];
-}
diff --git a/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollLoadMoreBar.handler.ts b/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollLoadMoreBar.handler.ts
index 5c2d308..d1068e3 100644
--- a/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollLoadMoreBar.handler.ts
+++ b/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollLoadMoreBar.handler.ts
@@ -1,10 +1,14 @@
 import { NodeMutation } from '../../../presentation/nodeWatcher/NodeWatcherHandler';
-import CssSelectorNodeWatcherScriptHandler from '../../../common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler';
-import { InfiniteScrollNodeWatcherOptions } from '../scripts/ccInfiniteScroll.script';
+import CCInfiniteScroll from '../core/ccInfiniteScroll';
+import CssSelectorNodeWatcherHandler from '../../../infrastructure/presentation/nodeWatcher/handlers/CssSelectorHandler.adapter';
 
-export default class CCInfiniteScrollLoadMoreBarHandler extends CssSelectorNodeWatcherScriptHandler<InfiniteScrollNodeWatcherOptions> {
+export default class CCInfiniteScrollLoadMoreBarHandler extends CssSelectorNodeWatcherHandler {
   cssSelector = '.load-more-bar';
 
+  constructor(private ccInfiniteScroll: CCInfiniteScroll) {
+    super();
+  }
+
   onMutatedNode({ node }: NodeMutation) {
     if (!(node instanceof Element)) {
       console.error(
@@ -13,6 +17,6 @@
       );
       return;
     }
-    this.options.ccInfiniteScroll.observeLoadMoreBar(node);
+    this.ccInfiniteScroll.observeLoadMoreBar(node);
   }
 }
diff --git a/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollLoadMoreBtn.handler.ts b/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollLoadMoreBtn.handler.ts
index 1ab8644..0dd8db6 100644
--- a/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollLoadMoreBtn.handler.ts
+++ b/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollLoadMoreBtn.handler.ts
@@ -1,11 +1,15 @@
 import { NodeMutation } from '../../../presentation/nodeWatcher/NodeWatcherHandler';
-import CssSelectorNodeWatcherScriptHandler from '../../../common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler';
-import { InfiniteScrollNodeWatcherOptions } from '../scripts/ccInfiniteScroll.script';
+import CssSelectorNodeWatcherHandler from '../../../infrastructure/presentation/nodeWatcher/handlers/CssSelectorHandler.adapter';
+import CCInfiniteScroll from '../core/ccInfiniteScroll';
 
-export default class CCInfiniteScrollLoadMoreBtnHandler extends CssSelectorNodeWatcherScriptHandler<InfiniteScrollNodeWatcherOptions> {
+export default class CCInfiniteScrollLoadMoreBtnHandler extends CssSelectorNodeWatcherHandler {
   cssSelector =
     '.scTailwindThreadMorebuttonbutton, .scTailwindThreadMessagegapbutton';
 
+  constructor(private ccInfiniteScroll: CCInfiniteScroll) {
+    super();
+  }
+
   onMutatedNode({ node }: NodeMutation) {
     if (!(node instanceof Element)) {
       console.error(
@@ -14,6 +18,6 @@
       );
       return;
     }
-    this.options.ccInfiniteScroll.observeLoadMoreInteropBtn(node);
+    this.ccInfiniteScroll.observeLoadMoreInteropBtn(node);
   }
 }
diff --git a/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollSetUp.handler.ts b/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollSetUp.handler.ts
index b36e0b9..0d77cf7 100644
--- a/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollSetUp.handler.ts
+++ b/src/features/infiniteScroll/nodeWatcherHandlers/ccInfiniteScrollSetUp.handler.ts
@@ -1,16 +1,17 @@
 import { NodeMutation } from '../../../presentation/nodeWatcher/NodeWatcherHandler';
-import CssSelectorNodeWatcherScriptHandler from '../../../common/architecture/scripts/nodeWatcher/handlers/CssSelectorNodeWatcherScriptHandler';
-import { InfiniteScrollNodeWatcherOptions } from '../scripts/ccInfiniteScroll.script';
+import CssSelectorNodeWatcherHandler from '../../../infrastructure/presentation/nodeWatcher/handlers/CssSelectorHandler.adapter';
+import CCInfiniteScroll from '../core/ccInfiniteScroll';
 
-export default class CCInfiniteScrollSetUpHandler extends CssSelectorNodeWatcherScriptHandler<InfiniteScrollNodeWatcherOptions> {
+export default class CCInfiniteScrollSetUpHandler extends CssSelectorNodeWatcherHandler {
   cssSelector = 'ec-app, .scrollable-content';
 
+  constructor(private ccInfiniteScroll: CCInfiniteScroll) {
+    super();
+  }
+
   onMutatedNode({ node }: NodeMutation) {
     if (!(node instanceof Element)) return;
     const isScrollableContent = node.classList.contains('scrollable-content');
-    this.options.ccInfiniteScroll.setUpIntersectionObserver(
-      node,
-      isScrollableContent,
-    );
+    this.ccInfiniteScroll.setUpIntersectionObserver(node, isScrollableContent);
   }
 }
diff --git a/src/features/infiniteScroll/scripts/ccInfiniteScroll.script.ts b/src/features/infiniteScroll/scripts/ccInfiniteScroll.script.ts
deleted file mode 100644
index e12a7c4..0000000
--- a/src/features/infiniteScroll/scripts/ccInfiniteScroll.script.ts
+++ /dev/null
@@ -1,31 +0,0 @@
-import {
-  ScriptEnvironment,
-  ScriptPage,
-  ScriptRunPhase,
-} from '../../../common/architecture/scripts/Script';
-import LegacyNodeWatcherScript from '../../../common/architecture/scripts/nodeWatcher/LegacyNodeWatcherScript';
-import CCInfiniteScroll from '../core/ccInfiniteScroll';
-import CCInfiniteScrollLoadMoreBarHandler from '../nodeWatcherHandlers/ccInfiniteScrollLoadMoreBar.handler';
-import CCInfiniteScrollLoadMoreBtnHandler from '../nodeWatcherHandlers/ccInfiniteScrollLoadMoreBtn.handler';
-import CCInfiniteScrollSetUpHandler from '../nodeWatcherHandlers/ccInfiniteScrollSetUp.handler';
-
-export interface InfiniteScrollNodeWatcherOptions {
-  ccInfiniteScroll: CCInfiniteScroll;
-}
-
-export default class CCInfiniteScrollScript extends LegacyNodeWatcherScript<InfiniteScrollNodeWatcherOptions> {
-  page = ScriptPage.CommunityConsole;
-  environment = ScriptEnvironment.ContentScript;
-  runPhase = ScriptRunPhase.Main;
-  handlers = new Map([
-    ['ccInfiniteScrollSetUp', CCInfiniteScrollSetUpHandler],
-    ['ccInfiniteScrollLoadMoreBar', CCInfiniteScrollLoadMoreBarHandler],
-    ['ccInfiniteScrollLoadMoreBtn', CCInfiniteScrollLoadMoreBtnHandler],
-  ]);
-
-  protected optionsFactory(): InfiniteScrollNodeWatcherOptions {
-    return {
-      ccInfiniteScroll: new CCInfiniteScroll(),
-    };
-  }
-}
diff --git a/src/common/options/OptionsProvider.ts b/src/infrastructure/services/options/OptionsProvider.adapter.ts
similarity index 90%
rename from src/common/options/OptionsProvider.ts
rename to src/infrastructure/services/options/OptionsProvider.adapter.ts
index 94a43de..e5570fe 100644
--- a/src/common/options/OptionsProvider.ts
+++ b/src/infrastructure/services/options/OptionsProvider.adapter.ts
@@ -1,12 +1,12 @@
 import { Mutex, MutexInterface, withTimeout } from 'async-mutex';
 
-import { getOptions } from './optionsUtils';
-import { OptionCodename, OptionsValues } from './optionsPrototype';
-import { OptionsConfiguration } from './OptionsConfiguration';
+import { getOptions } from '../../../common/options/optionsUtils';
+import { OptionCodename, OptionsValues } from '../../../common/options/optionsPrototype';
+import { OptionsConfiguration } from '../../../common/options/OptionsConfiguration';
 import {
   OptionsChangeListener,
   OptionsProviderPort,
-} from '../../services/options/OptionsProvider';
+} from '../../../services/options/OptionsProvider';
 
 // Prioritize reads before writes.
 const kReadPriority = 10;