aboutsummaryrefslogtreecommitdiffstats
path: root/src/shared/observe-dom.ts
diff options
context:
space:
mode:
authorArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2023-03-27 21:38:43 +0530
committerArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2023-03-27 21:38:43 +0530
commitd178caee5c93ad9ed91dbd082b0f0b5a9b0507aa (patch)
treeccfdf4c6992a86955c80780de0518a2fea10ac39 /src/shared/observe-dom.ts
parent5955bd821303cffa35263c41286339fb28e7536b (diff)
downloadbanglar-bhumi-utils-d178caee5c93ad9ed91dbd082b0f0b5a9b0507aa.tar.gz
banglar-bhumi-utils-d178caee5c93ad9ed91dbd082b0f0b5a9b0507aa.zip
Refactoring code.
Only using hashes for output file names. Using singleton pattern to store the callbacks for observeDOM, and only one MutationObserver.
Diffstat (limited to 'src/shared/observe-dom.ts')
-rw-r--r--src/shared/observe-dom.ts31
1 files changed, 23 insertions, 8 deletions
diff --git a/src/shared/observe-dom.ts b/src/shared/observe-dom.ts
index 4a447a0..dc2cb45 100644
--- a/src/shared/observe-dom.ts
+++ b/src/shared/observe-dom.ts
@@ -1,12 +1,27 @@
-export const observeDOM = (callback: () => boolean) => {
- const observer = new MutationObserver(() => {
- if (callback()) {
+type ObserveDOMCallback = () => boolean;
+
+let callbacks: (ObserveDOMCallback | null)[] = [];
+let count = 0;
+
+const observer = new MutationObserver(() => {
+ for (const callback of callbacks) {
+ if (callback && callback()) {
+ count++;
+ }
+
+ if (count >= callbacks.length) {
observer.disconnect();
+
+ break;
}
- });
+ }
+});
+
+observer.observe(document, {
+ childList: true,
+ subtree: true
+});
- observer.observe(document, {
- childList: true,
- subtree: true
- });
+export const observeDOM = (callback: ObserveDOMCallback) => {
+ callbacks.push(callback);
};