Initial force update through reflection

This commit is contained in:
Jiiks 2018-03-17 18:35:09 -03:00
parent 8740c286a0
commit 5ed34c149a
3 changed files with 40 additions and 9 deletions

View File

@ -9,7 +9,7 @@
*/
import { FileUtils, ClientLogger as Logger } from 'common';
import { Events, Globals, WebpackModules, ReactComponents, MonkeyPatch } from 'modules';
import { DOM, VueInjector } from 'ui';
import { DOM, VueInjector, Reflection } from 'ui';
import EmoteComponent from './EmoteComponent.vue';
let emotes = null;
const emotesEnabled = true;
@ -105,12 +105,16 @@ export default class {
Logger.err('EmoteModule', err);
}
});
this.unpatchMount = MonkeyPatch('BD:EmoteModule', Message.component.prototype).after('componentDidMount', (component, args) => {
for (const message of document.querySelectorAll('.message')) {
Reflection(message).forceUpdate();
}
this.injectAll();
this.unpatchMount = MonkeyPatch('BD:EmoteModule', Message.component.prototype).after('componentDidMount', component => {
const element = this.ReactDOM.findDOMNode(component);
if (!element) return;
this.injectEmotes(element);
});
this.unpatchUpdate = MonkeyPatch('BD:EmoteModule', Message.component.prototype).after('componentDidUpdate', (component, args) => {
this.unpatchUpdate = MonkeyPatch('BD:EmoteModule', Message.component.prototype).after('componentDidUpdate', component => {
const element = this.ReactDOM.findDOMNode(component);
if (!element) return;
this.injectEmotes(element);

View File

@ -272,12 +272,13 @@ export class ReactAutoPatcher {
}
static async patchComponents() {
this.patchMessage();
this.patchMessageGroup();
this.patchChannelMember();
this.patchGuild();
this.patchChannel();
this.patchChannelList();
await this.patchMessage();
await this.patchMessageGroup();
await this.patchChannelMember();
await this.patchGuild();
await this.patchChannel();
await this.patchChannelList();
this.forceUpdate();
}
static async patchMessage() {
@ -344,4 +345,10 @@ export class ReactAutoPatcher {
retVal.props['data-channel-name'] = channel.name;
});
}
static forceUpdate() {
for (const e of document.querySelectorAll('.message,.message-group,.guild,.containerDefault-7RImuF,.channel-members .member')) {
Reflection(e).forceUpdate();
}
}
}

View File

@ -89,6 +89,14 @@ class Reflection {
}
}
static getStateNode(node) {
try {
return this.reactInternalInstance(node).return.stateNode;
} catch (err) {
return null;
}
}
static getComponent(node, first = true) {
// IMPORTANT TODO Currently only checks the first found component. For example channel-member will not return the correct component
try {
@ -121,12 +129,24 @@ export default function (node) {
get state() {
return Reflection.getState(this.node);
}
get stateNode() {
return Reflection.getStateNode(this.node);
}
get reactInternalInstance() {
return Reflection.reactInternalInstance(this.node);
}
get component() {
return Reflection.getComponent(this.node);
}
forceUpdate() {
try {
const stateNode = Reflection.getStateNode(this.node);
if (!stateNode || !stateNode.forceUpdate) return;
stateNode.forceUpdate();
} catch (err) {
Logger.err('Reflection', err);
}
}
prop(propName) {
const split = propName.split('.');
const first = Reflection.findProp(this.node, split[0]);