1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-06-28 23:37:41 +00:00

Add post-modifier stuff to Apollo. Add convenience methods to FineWrapper for dealing with things that are expected to only have one instance at any given time.

This commit is contained in:
SirStendec 2017-11-22 03:33:34 -05:00
parent 127184f997
commit b51306019a
2 changed files with 31 additions and 5 deletions

View file

@ -6,7 +6,7 @@
// ============================================================================ // ============================================================================
import Module from 'utilities/module'; import Module from 'utilities/module';
import {has, get} from 'utilities/object'; import {has, get, deep_copy} from 'utilities/object';
export default class Apollo extends Module { export default class Apollo extends Module {
constructor(...args) { constructor(...args) {
@ -27,6 +27,15 @@ export default class Apollo extends Module {
} }
}`); }`);
this.registerModifier('ChannelPage_ChannelInfoBar_User', data => {
const u = data && data.data && data.data.user;
if ( u ) {
const o = u.profileViewCount = new Number(u.profileViewCount || 0);
o.data = deep_copy(u);
}
}, false);
this.registerModifier('FollowedIndex_CurrentUser', `query { this.registerModifier('FollowedIndex_CurrentUser', `query {
currentUser { currentUser {
followedLiveUsers { followedLiveUsers {
@ -218,8 +227,11 @@ export default class Apollo extends Module {
} }
registerModifier(operation, modifier) { registerModifier(operation, modifier, pre=true) {
if ( typeof modifier !== 'function' ) { if ( typeof modifier !== 'function' ) {
if ( ! pre )
throw new Error('post modifiers must be functions');
let parsed; let parsed;
try { try {
parsed = this.graphql ? this.graphql.parse(modifier, {noLocation: true}) : null; parsed = this.graphql ? this.graphql.parse(modifier, {noLocation: true}) : null;
@ -231,15 +243,21 @@ export default class Apollo extends Module {
modifier = [modifier, parsed]; modifier = [modifier, parsed];
} }
const mods = this.modifiers[operation] = this.modifiers[operation] || []; const mods = pre ?
(this.modifiers[operation] = this.modifiers[operation] || []) :
(this.post_modifiers[operation] = this.post_modifiers[operation] || []);
mods.push(modifier); mods.push(modifier);
} }
unregisterModifier(operation, modifier) { unregisterModifier(operation, modifier, pre=true) {
const mods = this.modifiers[operation]; const mods = pre ? this.modifiers[operation] : this.post_modifiers[operation];
if ( ! mods ) if ( ! mods )
return; return;
if ( typeof modifier !== 'function' )
throw new Error('graphql modifiers cannot be removed');
for(let i=0; i < mods.length; i++) { for(let i=0; i < mods.length; i++) {
const mod = mods[i]; const mod = mods[i];
if ( typeof mod === 'function' ? mod === modifier : mod[0] === modifier ) { if ( typeof mod === 'function' ? mod === modifier : mod[0] === modifier ) {

View file

@ -337,6 +337,14 @@ export class FineWrapper extends EventEmitter {
this._class = null; this._class = null;
} }
get first() {
return this.toArray()[0];
}
toArray() {
return Array.from(this.instances);
}
ready(fn) { ready(fn) {
if ( this._class ) if ( this._class )
fn(this._class, this.instances); fn(this._class, this.instances);