New type of core messages. Better events

This commit is contained in:
Vladimir Borisov 2019-10-29 14:50:35 +02:00
parent 188e64dff6
commit 0c62d79312
No known key found for this signature in database
GPG key ID: F9A584BE4FCB6603

View file

@ -1,6 +1,34 @@
const EventEmitter = require('events');
// const EventEmitter = require('events');
const { default: init, ContainerService } = require('stremio-core-web');
// Slightly better event handling
class EventEmitter {
constructor() {
this._handlers = {};
}
_handlersFor(event) {
return this._handlers[event] || [];
}
on(event, handler) {
this._handlers[event] = this._handlersFor(event).concat(handler);
}
off(event, handler) {
console.log('Before off', this._handlersFor(event))
this._handlers[event] = this._handlersFor(event).filter(event => event !== handler);
console.log('After off', this._handlersFor(event))
}
emit(event, args) {
this._handlersFor(event).forEach(handler => {
try {
handler(args);
}
catch (e) {
console.log('Event error', event, args, e);
}
});
}
}
function Core() {
let active = false;
let error = null;
@ -52,12 +80,12 @@ function Core() {
function off(name, listener) {
events.off(name, listener);
}
function dispatch({ action, args } = {}) {
function dispatch(args, model = 'All') {
if (!active) {
return;
}
containerService.dispatch({ action, args });
containerService.dispatch({ model, args });
}
function getState() {
if (!active) {