2023-07-10 09:53:00 +00:00
|
|
|
import * as Main from './main.js';
|
2012-09-03 01:23:50 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
export class ComponentManager {
|
2017-10-31 01:19:44 +00:00
|
|
|
constructor() {
|
2012-09-03 01:23:50 +00:00
|
|
|
this._allComponents = {};
|
|
|
|
this._enabledComponents = [];
|
|
|
|
|
2023-07-10 09:34:17 +00:00
|
|
|
Main.sessionMode.connect('updated', () => {
|
|
|
|
this._sessionModeUpdated().catch(logError);
|
|
|
|
});
|
|
|
|
|
|
|
|
this._sessionModeUpdated().catch(logError);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-09-03 01:23:50 +00:00
|
|
|
|
2023-07-10 09:34:17 +00:00
|
|
|
async _sessionModeUpdated() {
|
2012-09-03 01:23:50 +00:00
|
|
|
let newEnabledComponents = Main.sessionMode.components;
|
|
|
|
|
2023-07-10 09:34:17 +00:00
|
|
|
await Promise.allSettled([...newEnabledComponents
|
2020-04-03 23:52:29 +00:00
|
|
|
.filter(name => !this._enabledComponents.includes(name))
|
2023-07-10 09:34:17 +00:00
|
|
|
.map(name => this._enableComponent(name))]);
|
2020-04-03 23:52:29 +00:00
|
|
|
|
|
|
|
this._enabledComponents
|
|
|
|
.filter(name => !newEnabledComponents.includes(name))
|
|
|
|
.forEach(name => this._disableComponent(name));
|
2012-09-03 01:23:50 +00:00
|
|
|
|
|
|
|
this._enabledComponents = newEnabledComponents;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-09-03 01:23:50 +00:00
|
|
|
|
2023-07-10 09:34:17 +00:00
|
|
|
async _importComponent(name) {
|
2023-07-10 09:53:00 +00:00
|
|
|
let module = await import(`./components/${name}.js`);
|
2012-09-03 01:23:50 +00:00
|
|
|
return module.Component;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-09-03 01:23:50 +00:00
|
|
|
|
2023-07-10 09:34:17 +00:00
|
|
|
async _ensureComponent(name) {
|
2012-09-03 01:23:50 +00:00
|
|
|
let component = this._allComponents[name];
|
|
|
|
if (component)
|
|
|
|
return component;
|
|
|
|
|
2019-01-29 20:18:46 +00:00
|
|
|
if (Main.sessionMode.isLocked)
|
|
|
|
return null;
|
2012-09-05 10:48:14 +00:00
|
|
|
|
2023-07-10 09:34:17 +00:00
|
|
|
let constructor = await this._importComponent(name);
|
2012-09-03 01:23:50 +00:00
|
|
|
component = new constructor();
|
|
|
|
this._allComponents[name] = component;
|
|
|
|
return component;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-09-03 01:23:50 +00:00
|
|
|
|
2023-07-10 09:34:17 +00:00
|
|
|
async _enableComponent(name) {
|
|
|
|
let component = await this._ensureComponent(name);
|
2019-01-29 20:18:46 +00:00
|
|
|
if (component)
|
2012-09-05 10:48:14 +00:00
|
|
|
component.enable();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-09-03 01:23:50 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_disableComponent(name) {
|
2012-09-03 01:23:50 +00:00
|
|
|
let component = this._allComponents[name];
|
|
|
|
if (component == null)
|
|
|
|
return;
|
|
|
|
component.disable();
|
|
|
|
}
|
2023-07-10 09:53:00 +00:00
|
|
|
}
|