mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-01-11 22:40:31 +00:00
initialization code of core updated
This commit is contained in:
parent
edf8dbf66c
commit
b86ebe1482
3 changed files with 46 additions and 54 deletions
|
|
@ -24,10 +24,9 @@ const App = () => {
|
|||
const [shellInitialized, setShellInitialized] = React.useState(false);
|
||||
React.useEffect(() => {
|
||||
const onCoreStateChanged = () => {
|
||||
setCoreInitialized(services.core.active);
|
||||
if (services.core.error) {
|
||||
alert(services.core.error);
|
||||
} else if (services.core.active) {
|
||||
setCoreInitialized(services.core.active);
|
||||
}
|
||||
};
|
||||
const onShellStateChanged = () => {
|
||||
|
|
|
|||
|
|
@ -1,56 +1,26 @@
|
|||
// Copyright (C) 2017-2020 Smart code 203358507
|
||||
|
||||
const EventEmitter = require('eventemitter3');
|
||||
const { default: initialize_api, initialize_runtime } = require('@stremio/stremio-core-web');
|
||||
const CoreTransport = require('./CoreTransport');
|
||||
|
||||
let transport = null;
|
||||
let apiInitialized = null;
|
||||
const apiEvents = new EventEmitter();
|
||||
initialize_api(require('@stremio/stremio-core-web/stremio_core_web_bg.wasm'))
|
||||
.then(() => {
|
||||
const transportEvents = new EventEmitter();
|
||||
return initialize_runtime(({ name, args }) => {
|
||||
try {
|
||||
transportEvents.emit(name, args);
|
||||
} catch (error) {
|
||||
console.error('Core', error);
|
||||
}
|
||||
}).then(() => {
|
||||
transport = new CoreTransport(transportEvents);
|
||||
});
|
||||
})
|
||||
.then(() => {
|
||||
apiInitialized = true;
|
||||
apiEvents.emit('initialized');
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Core', error);
|
||||
apiInitialized = false;
|
||||
apiEvents.emit('initialized');
|
||||
});
|
||||
|
||||
function Core() {
|
||||
let active = false;
|
||||
let error = null;
|
||||
let starting = false;
|
||||
let _transport = null;
|
||||
let transport = null;
|
||||
|
||||
const events = new EventEmitter();
|
||||
|
||||
function onAPIInitialized() {
|
||||
if (apiInitialized) {
|
||||
active = true;
|
||||
error = null;
|
||||
starting = false;
|
||||
_transport = transport;
|
||||
} else {
|
||||
active = false;
|
||||
error = new Error('Stremio Core API initialization failed');
|
||||
starting = false;
|
||||
_transport = null;
|
||||
}
|
||||
|
||||
function onTransportInit() {
|
||||
active = true;
|
||||
error = null;
|
||||
starting = false;
|
||||
onStateChanged();
|
||||
}
|
||||
function onTransportError(args) {
|
||||
active = false;
|
||||
error = new Error(`Stremio Core Transport initialization failed: ${args}`);
|
||||
starting = false;
|
||||
onStateChanged();
|
||||
}
|
||||
function onStateChanged() {
|
||||
|
|
@ -83,7 +53,7 @@ function Core() {
|
|||
configurable: false,
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return _transport;
|
||||
return transport;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
@ -94,20 +64,21 @@ function Core() {
|
|||
}
|
||||
|
||||
starting = true;
|
||||
if (apiInitialized !== null) {
|
||||
onAPIInitialized();
|
||||
} else {
|
||||
apiEvents.on('initialized', onAPIInitialized);
|
||||
onStateChanged();
|
||||
}
|
||||
transport = new CoreTransport();
|
||||
transport.on('init', onTransportInit);
|
||||
transport.on('error', onTransportError);
|
||||
onStateChanged();
|
||||
};
|
||||
this.stop = function() {
|
||||
apiEvents.off('initialized', onAPIInitialized);
|
||||
active = false;
|
||||
error = null;
|
||||
starting = false;
|
||||
onStateChanged();
|
||||
_transport = null;
|
||||
if (transport !== null) {
|
||||
transport.off('init', onTransportInit);
|
||||
transport.off('error', onTransportError);
|
||||
transport = null;
|
||||
}
|
||||
};
|
||||
this.on = function(name, listener) {
|
||||
events.on(name, listener);
|
||||
|
|
|
|||
|
|
@ -1,8 +1,30 @@
|
|||
// Copyright (C) 2017-2020 Smart code 203358507
|
||||
|
||||
const { get_state, dispatch } = require('@stremio/stremio-core-web');
|
||||
const EventEmitter = require('eventemitter3');
|
||||
const { default: initialize_api, initialize_runtime, get_state, dispatch } = require('@stremio/stremio-core-web');
|
||||
|
||||
function CoreTransport() {
|
||||
const events = new EventEmitter();
|
||||
|
||||
initialize_api(require('@stremio/stremio-core-web/stremio_core_web_bg.wasm'))
|
||||
.then(() => initialize_runtime(({ name, args }) => {
|
||||
try {
|
||||
events.emit(name, args);
|
||||
} catch (error) {
|
||||
console.error('CoreTransport', error);
|
||||
}
|
||||
}))
|
||||
.then(() => {
|
||||
try {
|
||||
events.emit('init');
|
||||
} catch (error) {
|
||||
console.error('CoreTransport', error);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
events.emit('error', error);
|
||||
});
|
||||
|
||||
function CoreTransport(events) {
|
||||
this.on = function(name, listener) {
|
||||
events.on(name, listener);
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue