mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-08-16 15:44:49 +00:00
New type of core messages. Better events
This commit is contained in:
parent
188e64dff6
commit
0c62d79312
1 changed files with 31 additions and 3 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue