Use an observer to detect new React components

This commit is contained in:
Samuel Elliott 2018-08-01 20:48:09 +01:00
parent 5b419cb8ab
commit 8826a7b984
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
1 changed files with 8 additions and 5 deletions

View File

@ -10,7 +10,7 @@
* LICENSE file in the root directory of this source tree.
*/
import { Reflection } from 'ui';
import { DOM, Reflection } from 'ui';
import { Utils, Filters, ClientLogger as Logger } from 'common';
import { MonkeyPatch } from './patcher';
import { WebpackModules } from './webpackmodules';
@ -226,17 +226,17 @@ export class ReactComponents {
if (have) return have;
if (important) {
const importantInterval = setInterval(() => {
const callback = () => {
if (this.components.find(c => c.id === name)) {
Logger.info('ReactComponents', `Important component ${name} already found`);
clearInterval(importantInterval);
DOM.observer.unsubscribe(observerSubscription);
return;
}
const element = document.querySelector(important.selector);
if (!element) return;
clearInterval(importantInterval);
DOM.observer.unsubscribe(observerSubscription);
const reflect = Reflection(element);
const component = filter ? reflect.components.find(filter) : reflect.component;
if (!component) {
@ -248,7 +248,10 @@ export class ReactComponents {
Logger.info('ReactComponents', [`Found important component ${name} with reflection`, reflect]);
important.filter = filter;
this.push(component, undefined, important);
}, 50);
};
const observerSubscription = DOM.observer.subscribeToQuerySelector(callback, important.selector);
setTimeout(callback, 0);
}
let listener = this.listeners.find(l => l.id === name);