domoto
Version:
Librería para crear extensiones de Domoto CLI
44 lines (33 loc) • 888 B
JavaScript
const singleton = Symbol();
const singletonEnforcer = Symbol();
module.exports = class ViewManager {
constructor(enforcer) {
if (enforcer !== singletonEnforcer)
throw new Error('Cannot construct singleton');
this.visible = null;
this.views = new Set();
}
static get instance() {
if (!this[singleton])
this[singleton] = new ViewManager(singletonEnforcer);
return this[singleton];
}
setVisible(view) {
if (this.visible)
this.visible.hide();
this.visible = view;
view.show();
}
show(view) {
this.views.add(view);
if (!this.visible)
this.setVisible(view);
return this.setVisible.bind(this, view);
}
hide(...views) {
views.forEach(view => this.views.delete(view));
if (views.every(view => view !== this.visible))
return;
this.setVisible(this.views.values().next().value);
}
};