mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-03-11 21:27:05 +00:00
StremioCore service implemented
This commit is contained in:
parent
33a13d6c44
commit
e4e6b663b3
3 changed files with 104 additions and 2 deletions
98
src/services/StremioCore/StremioCore.js
Normal file
98
src/services/StremioCore/StremioCore.js
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
const EventEmitter = require('events');
|
||||
const { default: init, ContainerService } = require('stremio-core-web');
|
||||
|
||||
function StremioCore() {
|
||||
let active = false;
|
||||
let error = null;
|
||||
let starting = false;
|
||||
let containerService = null;
|
||||
let events = new EventEmitter();
|
||||
|
||||
function onStateChanged() {
|
||||
events.emit('stateChanged');
|
||||
}
|
||||
function start() {
|
||||
if (active || error !== null || starting) {
|
||||
return;
|
||||
}
|
||||
|
||||
starting = true;
|
||||
events.on('error', () => { });
|
||||
init()
|
||||
.then(() => {
|
||||
if (starting) {
|
||||
containerService = new ContainerService(({ name, args } = {}) => {
|
||||
if (active) {
|
||||
events.emit(name, args);
|
||||
}
|
||||
});
|
||||
active = true;
|
||||
onStateChanged();
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
error = new Error('Unable to init stremio-core-web');
|
||||
error.error = e;
|
||||
onStateChanged();
|
||||
})
|
||||
.then(() => {
|
||||
starting = false;
|
||||
});
|
||||
}
|
||||
function stop() {
|
||||
active = false;
|
||||
error = null;
|
||||
containerService = null;
|
||||
onStateChanged();
|
||||
}
|
||||
function on(name, listener) {
|
||||
events.on(name, listener);
|
||||
}
|
||||
function off(name, listener) {
|
||||
events.off(name, listener);
|
||||
}
|
||||
function dispatch({ action, args } = {}) {
|
||||
if (!active) {
|
||||
return;
|
||||
}
|
||||
|
||||
containerService.dispatch({ action, args });
|
||||
}
|
||||
function getState() {
|
||||
if (!active) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return containerService.get_state();
|
||||
}
|
||||
|
||||
Object.defineProperties(this, {
|
||||
active: {
|
||||
configurable: false,
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return active;
|
||||
}
|
||||
},
|
||||
error: {
|
||||
configurable: false,
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.start = start;
|
||||
this.stop = stop;
|
||||
this.on = on;
|
||||
this.off = off;
|
||||
this.dispatch = dispatch;
|
||||
this.getState = getState;
|
||||
|
||||
Object.freeze(this);
|
||||
};
|
||||
|
||||
Object.freeze(StremioCore);
|
||||
|
||||
module.exports = StremioCore;
|
||||
|
|
@ -1 +1,3 @@
|
|||
//TODO
|
||||
const StremioCore = require('./StremioCore');
|
||||
|
||||
module.exports = StremioCore;
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
const KeyboardNavigation = require('./KeyboardNavigation');
|
||||
const { ServicesProvider, useServices } = require('./ServicesContext');
|
||||
const StremioCore = require('./StremioCore');
|
||||
|
||||
module.exports = {
|
||||
KeyboardNavigation,
|
||||
ServicesProvider,
|
||||
useServices
|
||||
useServices,
|
||||
StremioCore
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue