Add a function to force update all instances of a component

This commit is contained in:
Samuel Elliott 2018-08-01 16:08:47 +01:00
parent 1a12430322
commit a20473d718
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
2 changed files with 30 additions and 12 deletions

View File

@ -166,10 +166,18 @@ class Helpers {
export { Helpers as ReactHelpers }; export { Helpers as ReactHelpers };
class ReactComponent { class ReactComponent {
constructor(id, component, retVal) { constructor(id, component, retVal, important) {
this.id = id; this.id = id;
this.component = component; this.component = component;
this.retVal = retVal; this.retVal = retVal;
this.important = important;
}
forceUpdateAll() {
if (!this.important || !this.important.selector) return;
for (let e of document.querySelectorAll(this.important.selector)) {
Reflection(e).forceUpdate(this);
}
} }
} }
@ -181,22 +189,28 @@ export class ReactComponents {
static get ReactComponent() { return ReactComponent } static get ReactComponent() { return ReactComponent }
static push(component, retVal) { static push(component, retVal, important) {
if (!(component instanceof Function)) return null; if (!(component instanceof Function)) return null;
const { displayName } = component; const { displayName } = component;
if (!displayName) { if (!displayName) {
return this.processUnknown(component, retVal); return this.processUnknown(component, retVal);
} }
const have = this.components.find(comp => comp.id === displayName); const have = this.components.find(comp => comp.id === displayName);
if (have) return component; if (have) {
const c = new ReactComponent(displayName, component, retVal); if (!have.important) have.important = important;
this.components.push(c); return component;
const listener = this.listeners.find(listener => listener.id === displayName);
if (!listener) return c;
for (const l of listener.listeners) {
l(c);
} }
this.listeners.splice(this.listeners.findIndex(listener => listener.id === displayName), 1);
const c = new ReactComponent(displayName, component, retVal, important);
this.components.push(c);
const listener = this.listeners.find(listener => listener.id === displayName);
if (listener) {
for (const l of listener.listeners) l(c);
Utils.removeFromArray(this.listeners, listener);
}
return c; return c;
} }
@ -210,6 +224,7 @@ export class ReactComponents {
static async getComponent(name, important, filter) { static async getComponent(name, important, filter) {
const have = this.components.find(c => c.id === name); const have = this.components.find(c => c.id === name);
if (have) return have; if (have) return have;
if (important) { if (important) {
const importantInterval = setInterval(() => { const importantInterval = setInterval(() => {
if (this.components.find(c => c.id === name)) { if (this.components.find(c => c.id === name)) {
@ -231,14 +246,17 @@ export class ReactComponents {
if (!component.displayName) component.displayName = name; if (!component.displayName) component.displayName = name;
Logger.info('ReactComponents', [`Found important component ${name} with reflection`, reflect]); Logger.info('ReactComponents', [`Found important component ${name} with reflection`, reflect]);
this.push(component); important.filter = filter;
this.push(component, undefined, important);
}, 50); }, 50);
} }
let listener = this.listeners.find(l => l.id === name); let listener = this.listeners.find(l => l.id === name);
if (!listener) this.listeners.push(listener = { if (!listener) this.listeners.push(listener = {
id: name, id: name,
listeners: [] listeners: []
}); });
return new Promise(resolve => { return new Promise(resolve => {
listener.listeners.push(resolve); listener.listeners.push(resolve);
}); });

View File

@ -107,7 +107,7 @@ class Reflection {
return stateNodes; return stateNodes;
} }
static findComponentStateNode(node, component) { static getComponentStateNode(node, component) {
if (component instanceof ReactComponents.ReactComponent) component = component.component; if (component instanceof ReactComponents.ReactComponent) component = component.component;
for (let stateNode of this.getStateNodes(node)) { for (let stateNode of this.getStateNodes(node)) {