1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-08-02 16:08:31 +00:00
FrankerFaceZ/src/utilities/compat/subpump.js
SirStendec 76b72b86c8 4.29.3
* Added: Setting to hide the "Discover Luna" link in the top navigation.
* Changed: Hide blocked categories from the directory's category list.
* Fixed: Stream up-times not appearing in the directory.
* Fixed: Error in error handler for PubSub.
* API Added: `Dialog` now supports prepending its element to its container, rather than appending.
* API Added: `tip.add_class` for adding additional classes to rich tooltips that may now have had their element created yet.
2021-11-05 18:01:28 -04:00

151 lines
No EOL
3.1 KiB
JavaScript

'use strict';
// ============================================================================
// Subpump
// It controls Twitch PubSub.
// ============================================================================
import Module from 'utilities/module';
import { FFZEvent } from 'utilities/events';
export class PubSubEvent extends FFZEvent {
constructor(data) {
super(data);
this._obj = undefined;
this._changed = false;
}
markChanged() {
this._changed = true;
}
get topic() {
return this.event.topic;
}
get message() {
if ( this._obj === undefined )
this._obj = JSON.parse(this.event.message);
return this._obj;
}
set message(val) {
this._obj = val;
this._changed = true;
}
}
export default class Subpump extends Module {
constructor(...args) {
super(...args);
this.instance = null;
}
onEnable(tries = 0) {
const instances = window.__Twitch__pubsubInstances;
if ( ! instances ) {
if ( tries > 10 )
this.log.warn('Unable to find PubSub.');
else
new Promise(r => setTimeout(r, 50)).then(() => this.onEnable(tries + 1));
return;
}
for(const val of Object.values(instances))
if ( val?._client ) {
if ( this.instance ) {
this.log.warn('Multiple PubSub instances detected. Things might act weird.');
continue;
}
this.instance = val;
this.hookClient(val._client);
}
if ( ! this.instance )
this.log.warn('Unable to find a PubSub instance.');
}
hookClient(client) {
const t = this,
orig_message = client._onMessage;
client._unbindPrimary(client._primarySocket);
client._onMessage = function(e) {
try {
if ( e.type === 'MESSAGE' && e.data?.topic ) {
const raw_topic = e.data.topic,
idx = raw_topic.indexOf('.'),
prefix = idx === -1 ? raw_topic : raw_topic.slice(0, idx),
trail = idx === -1 ? '' : raw_topic.slice(idx + 1);
const event = new PubSubEvent({
prefix,
trail,
event: e.data
});
t.emit(':pubsub-message', event);
if ( event.defaultPrevented )
return;
if ( event._changed )
e.data.message = JSON.stringify(event._obj);
}
} catch(err) {
t.log.error('Error processing PubSub event.', err);
}
return orig_message.call(this, e);
};
client._bindPrimary(client._primarySocket);
const listener = client._listens,
orig_on = listener.on,
orig_off = listener.off;
listener.on = function(topic, fn, ctx) {
const has_topic = !! listener._events?.[topic],
out = orig_on.call(this, topic, fn, ctx);
if ( ! has_topic )
t.emit(':add-topic', topic)
return out;
}
listener.off = function(topic, fn) {
const has_topic = !! listener._events?.[topic],
out = orig_off.call(this, topic, fn);
if ( has_topic && ! listener._events?.[topic] )
t.emit(':remove-topic', topic);
return out;
}
}
inject(topic, message) {
const listens = this.instance?._client?._listens;
if ( ! listens )
throw new Error('No PubSub instance available');
listens._trigger(topic, JSON.stringify(message));
}
get topics() {
const events = this.instance?._client?._listens._events;
if ( ! events )
return [];
return Object.keys(events);
}
}