diff --git a/src/flow.ts b/src/flow.ts index cdef66a..83ce7e9 100644 --- a/src/flow.ts +++ b/src/flow.ts @@ -1,8 +1,7 @@ -import { Apps } from './types.ts' +import { App } from './types.ts' class Flow { - apps: Apps = {} - + apps: App[] = [] appList = [ 'settings', 'music', @@ -20,7 +19,7 @@ class Flow { const app = new ImportedApp() window.preloader.setStatus(`importing default apps\n${appPath}`) - this.apps[app.pkg] = app + this.add(app) } window.wm.launcher.style.opacity = '0' @@ -29,16 +28,16 @@ class Flow { window.preloader.setStatus('adding apps to app launcher...') - for (const pkg in window.flow.apps) { - window.preloader.setStatus(`adding apps to app launcher\n${window.flow.apps[pkg].name}`) - const app = document.createElement('app') - app.onclick = async () => { - await window.flow.openApp(pkg) + this.apps.forEach((app) => { + window.preloader.setStatus(`adding apps to app launcher\n${app.name}`) + const appElement = document.createElement('app') + appElement.onclick = async () => { + await window.flow.openApp(app.pkg) window.wm.toggleLauncher() } - app.innerHTML = `
${window.flow.apps[pkg].name}
` - window.wm.launcher.querySelector('apps')?.appendChild(app) - } + appElement.innerHTML = `
${app.name}
` + window.wm.launcher.querySelector('apps')?.appendChild(appElement) + }) document.body.appendChild(window.wm.windowArea) document.body.appendChild(window.wm.launcher) @@ -46,9 +45,19 @@ class Flow { await window.preloader.setDone('apps') } + add (app: App): void { + if (this.apps.some(x => x.pkg === app.pkg)) { + console.error(`Unable to register app; ${app.pkg} is already registered.`) + return + } + + this.apps.push(app) + } + async openApp (pkg: string, data?: any): Promise { - const win = this.apps[pkg].open(data) - const event = new CustomEvent('app_opened', { detail: { app: this.apps[pkg], win: await win } }) + const app = this.apps.find(x => x.pkg === pkg) + const win = app?.open(data) + const event = new CustomEvent('app_opened', { detail: { app, win: await win } }) window.dispatchEvent(event) } }