40 lines
1.3 KiB
JavaScript
40 lines
1.3 KiB
JavaScript
// Host-side implementations of the capability interfaces components may import.
|
|
// This is where a native host would talk to the OS; here it is in-memory so the
|
|
// desktop is deterministic and testable.
|
|
|
|
/** @param {{now?: () => Date, apps: any[], tokens: any}} opts */
|
|
export function createPlatform({ now = () => new Date(), apps, tokens }) {
|
|
const p = {
|
|
tokens,
|
|
apps,
|
|
launches: [],
|
|
now,
|
|
launchable: new Set(apps.map((a) => a.id)),
|
|
};
|
|
/** Capability name -> WIT interface implementation (jco import shape). */
|
|
p.interfaces = {
|
|
theme: () => ({ getTokens: () => p.tokens }),
|
|
clock: () => ({
|
|
now() {
|
|
const d = p.now();
|
|
return {
|
|
year: d.getFullYear(), month: d.getMonth() + 1, day: d.getDate(),
|
|
hour: d.getHours(), minute: d.getMinutes(), second: d.getSeconds(), weekday: d.getDay(),
|
|
};
|
|
},
|
|
}),
|
|
'app-catalog': () => ({ listApps: () => p.apps }),
|
|
'app-launch': () => ({
|
|
launch(id) {
|
|
if (!p.launchable.has(id)) return { tag: 'err', val: `unknown application: ${id}` };
|
|
p.launches.push(id);
|
|
console.log(`[host] launch ${id}`);
|
|
return { tag: 'ok', val: undefined };
|
|
},
|
|
}),
|
|
};
|
|
return p;
|
|
}
|
|
|
|
export const CAPABILITIES = ['theme', 'clock', 'app-catalog', 'app-launch'];
|