(callback: (hasDarkTheme: boolean) => void, hints: DetectorHint[])
| 147 | } |
| 148 | |
| 149 | export function runDarkThemeDetector(callback: (hasDarkTheme: boolean) => void, hints: DetectorHint[]): void { |
| 150 | stopDarkThemeDetector(); |
| 151 | if (hints && hints.length > 0) { |
| 152 | const hint = hints[0]; |
| 153 | if (hint.noDarkTheme) { |
| 154 | callback(false); |
| 155 | return; |
| 156 | } |
| 157 | if (hint.systemTheme && isSystemDarkModeEnabled()) { |
| 158 | callback(true); |
| 159 | return; |
| 160 | } |
| 161 | detectUsingHint(hint, () => callback(true)); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | if (canCheckForStyle()) { |
| 166 | runCheck(callback); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | observer = new MutationObserver(() => { |
| 171 | if (canCheckForStyle()) { |
| 172 | stopDarkThemeDetector(); |
| 173 | runCheck(callback); |
| 174 | } |
| 175 | }); |
| 176 | observer.observe(document.documentElement, {childList: true}); |
| 177 | |
| 178 | if (document.readyState !== 'complete') { |
| 179 | readyStateListener = () => { |
| 180 | if (document.readyState === 'complete') { |
| 181 | stopDarkThemeDetector(); |
| 182 | runCheck(callback); |
| 183 | } |
| 184 | }; |
| 185 | // readystatechange event is not cancellable and does not bubble |
| 186 | document.addEventListener('readystatechange', readyStateListener); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | export function stopDarkThemeDetector(): void { |
| 191 | if (observer) { |
no test coverage detected