mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-03-11 21:27:05 +00:00
92 lines
2.2 KiB
JavaScript
92 lines
2.2 KiB
JavaScript
// Copyright (C) 2017-2023 Smart code 203358507
|
|
|
|
const EventEmitter = require('eventemitter3');
|
|
const CoreTransport = require('./CoreTransport');
|
|
|
|
function Core(args) {
|
|
let active = false;
|
|
let error = null;
|
|
let starting = false;
|
|
let transport = null;
|
|
|
|
const events = new EventEmitter();
|
|
|
|
function onTransportInit() {
|
|
active = true;
|
|
error = null;
|
|
starting = false;
|
|
onStateChanged();
|
|
}
|
|
function onTransportError(args) {
|
|
console.error(args);
|
|
active = false;
|
|
error = new Error('Stremio Core Transport initialization failed', { cause: args });
|
|
starting = false;
|
|
onStateChanged();
|
|
transport = null;
|
|
}
|
|
function onStateChanged() {
|
|
events.emit('stateChanged');
|
|
}
|
|
|
|
Object.defineProperties(this, {
|
|
active: {
|
|
configurable: false,
|
|
enumerable: true,
|
|
get: function() {
|
|
return active;
|
|
}
|
|
},
|
|
error: {
|
|
configurable: false,
|
|
enumerable: true,
|
|
get: function() {
|
|
return error;
|
|
}
|
|
},
|
|
starting: {
|
|
configurable: false,
|
|
enumerable: true,
|
|
get: function() {
|
|
return starting;
|
|
}
|
|
},
|
|
transport: {
|
|
configurable: false,
|
|
enumerable: true,
|
|
get: function() {
|
|
return transport;
|
|
}
|
|
}
|
|
});
|
|
|
|
this.start = function() {
|
|
if (active || error instanceof Error || starting) {
|
|
return;
|
|
}
|
|
|
|
starting = true;
|
|
transport = new CoreTransport(args);
|
|
transport.on('init', onTransportInit);
|
|
transport.on('error', onTransportError);
|
|
onStateChanged();
|
|
};
|
|
this.stop = function() {
|
|
active = false;
|
|
error = null;
|
|
starting = false;
|
|
onStateChanged();
|
|
if (transport !== null) {
|
|
transport.removeAllListeners();
|
|
transport = null;
|
|
}
|
|
};
|
|
this.on = function(name, listener) {
|
|
events.on(name, listener);
|
|
};
|
|
this.off = function(name, listener) {
|
|
events.off(name, listener);
|
|
};
|
|
}
|
|
|
|
module.exports = Core;
|