Merge pull request #942 from Stremio/refactor/shell-init
Some checks are pending
Build / build (push) Waiting to run

refactor(Shell): remove init logic
This commit is contained in:
Tim 2025-06-21 01:24:42 +02:00 committed by GitHub
commit ab7fa8748a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 64 additions and 96 deletions

View file

@ -11,21 +11,6 @@ function Shell() {
const events = new EventEmitter(); const events = new EventEmitter();
function onTransportInit() {
active = true;
error = null;
starting = false;
onStateChanged();
}
function onTransportInitError(err) {
console.error(err);
active = false;
error = new Error(err);
starting = false;
onStateChanged();
transport = null;
}
function onStateChanged() { function onStateChanged() {
events.emit('stateChanged'); events.emit('stateChanged');
} }
@ -68,9 +53,22 @@ function Shell() {
active = false; active = false;
starting = true; starting = true;
transport = new ShellTransport();
transport.on('init', onTransportInit); try {
transport.on('init-error', onTransportInitError); transport = new ShellTransport();
active = true;
error = null;
starting = false;
onStateChanged();
} catch (e) {
console.error(e);
active = false;
error = new Error(e);
starting = false;
onStateChanged();
transport = null;
}
onStateChanged(); onStateChanged();
}; };
this.stop = function() { this.stop = function() {

View file

@ -2,9 +2,6 @@
const EventEmitter = require('eventemitter3'); const EventEmitter = require('eventemitter3');
let shellAvailable = false;
const shellEvents = new EventEmitter();
const QtMsgTypes = { const QtMsgTypes = {
signal: 1, signal: 1,
propertyUpdate: 2, propertyUpdate: 2,
@ -19,27 +16,6 @@ const QtMsgTypes = {
}; };
const QtObjId = 'transport'; // the ID of our transport object const QtObjId = 'transport'; // the ID of our transport object
window.initShellComm = function () {
delete window.initShellComm;
shellEvents.emit('availabilityChanged');
};
const initialize = () => {
if(!window.qt) return Promise.reject('Qt API not found');
return new Promise((resolve) => {
function onShellAvailabilityChanged() {
shellEvents.off('availabilityChanged', onShellAvailabilityChanged);
shellAvailable = true;
resolve();
}
if (shellAvailable) {
onShellAvailabilityChanged();
} else {
shellEvents.on('availabilityChanged', onShellAvailabilityChanged);
}
});
};
function ShellTransport() { function ShellTransport() {
const events = new EventEmitter(); const events = new EventEmitter();
@ -47,66 +23,60 @@ function ShellTransport() {
// eslint-disable-next-line @typescript-eslint/no-this-alias // eslint-disable-next-line @typescript-eslint/no-this-alias
const shell = this; const shell = this;
initialize() const transport = window.qt && window.qt.webChannelTransport;
.then(() => { if (!transport) throw 'no viable transport found (qt.webChannelTransport)';
const transport = window.qt && window.qt.webChannelTransport;
if (!transport) throw 'no viable transport found (qt.webChannelTransport)';
let id = 0; let id = 0;
function send(msg) { function send(msg) {
msg.id = id++; msg.id = id++;
transport.send(JSON.stringify(msg)); transport.send(JSON.stringify(msg));
}
transport.onmessage = function (message) {
const msg = JSON.parse(message.data);
if (msg.id === 0) {
const obj = msg.data[QtObjId];
obj.properties.slice(1).forEach(function (prop) {
shell.props[prop[1]] = prop[3];
});
if (typeof shell.props.shellVersion === 'string') {
shell.shellVersionArr = (
shell.props.shellVersion.match(/(\d+)\.(\d+)\.(\d+)/) || []
)
.slice(1, 4)
.map(Number);
} }
events.emit('received-props', shell.props);
transport.onmessage = function (message) { obj.signals.forEach(function (sig) {
const msg = JSON.parse(message.data); send({
if (msg.id === 0) { type: QtMsgTypes.connectToSignal,
const obj = msg.data[QtObjId]; object: QtObjId,
signal: sig[1],
});
});
obj.properties.slice(1).forEach(function (prop) { const onEvent = obj.methods.filter(function (x) {
shell.props[prop[1]] = prop[3]; return x[0] === 'onEvent';
}); })[0];
if (typeof shell.props.shellVersion === 'string') {
shell.shellVersionArr = (
shell.props.shellVersion.match(/(\d+)\.(\d+)\.(\d+)/) || []
)
.slice(1, 4)
.map(Number);
}
events.emit('received-props', shell.props);
obj.signals.forEach(function (sig) { shell.send = function (ev, args) {
send({ send({
type: QtMsgTypes.connectToSignal, type: QtMsgTypes.invokeMethod,
object: QtObjId, object: QtObjId,
signal: sig[1], method: onEvent[1],
}); args: [ev, args || {}],
}); });
const onEvent = obj.methods.filter(function (x) {
return x[0] === 'onEvent';
})[0];
shell.send = function (ev, args) {
send({
type: QtMsgTypes.invokeMethod,
object: QtObjId,
method: onEvent[1],
args: [ev, args || {}],
});
};
shell.send('app-ready', {}); // signal that we're ready to take events
}
if (msg.object === QtObjId && msg.type === QtMsgTypes.signal)
events.emit(msg.args[0], msg.args[1]);
events.emit('init');
}; };
send({ type: QtMsgTypes.init });
}) .catch((error) => { shell.send('app-ready', {}); // signal that we're ready to take events
events.emit('init-error', error); }
});
if (msg.object === QtObjId && msg.type === QtMsgTypes.signal)
events.emit(msg.args[0], msg.args[1]);
};
send({ type: QtMsgTypes.init });
this.on = function(name, listener) { this.on = function(name, listener) {
events.on(name, listener); events.on(name, listener);