2024-08-16 07:15:42 -04:00
|
|
|
import { isDesktop } from './isDesktop'
|
2024-08-04 00:51:30 -04:00
|
|
|
import { components } from './machine-api'
|
2024-09-09 18:17:45 -04:00
|
|
|
import { reportRejection } from './trap'
|
|
|
|
import { toSync } from './utils'
|
2024-08-16 07:15:42 -04:00
|
|
|
|
2024-08-28 15:15:37 -04:00
|
|
|
export type MachinesListing = Array<
|
|
|
|
components['schemas']['MachineInfoResponse']
|
|
|
|
>
|
2024-08-04 00:51:30 -04:00
|
|
|
|
|
|
|
export class MachineManager {
|
2024-08-16 07:15:42 -04:00
|
|
|
private _isDesktop: boolean = isDesktop()
|
2024-08-28 15:15:37 -04:00
|
|
|
private _machines: MachinesListing = []
|
2024-08-04 00:51:30 -04:00
|
|
|
private _machineApiIp: string | null = null
|
2024-08-28 15:15:37 -04:00
|
|
|
private _currentMachine: components['schemas']['MachineInfoResponse'] | null =
|
|
|
|
null
|
2024-08-04 00:51:30 -04:00
|
|
|
|
|
|
|
constructor() {
|
2024-08-16 07:15:42 -04:00
|
|
|
if (!this._isDesktop) {
|
2024-08-04 00:51:30 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-09 18:17:45 -04:00
|
|
|
this.updateMachines().catch(reportRejection)
|
2024-08-04 00:51:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
2024-08-16 07:15:42 -04:00
|
|
|
if (!this._isDesktop) {
|
2024-08-04 00:51:30 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a background job to update the machines every ten seconds.
|
2024-08-16 07:15:42 -04:00
|
|
|
// If MDNS is already watching, this timeout will wait until it's done to trigger the
|
|
|
|
// finding again.
|
|
|
|
let timeoutId: ReturnType<typeof setTimeout> | undefined = undefined
|
|
|
|
const timeoutLoop = () => {
|
|
|
|
clearTimeout(timeoutId)
|
2024-09-09 18:17:45 -04:00
|
|
|
timeoutId = setTimeout(
|
|
|
|
toSync(async () => {
|
|
|
|
await this.updateMachineApiIp()
|
|
|
|
await this.updateMachines()
|
|
|
|
timeoutLoop()
|
|
|
|
}, reportRejection),
|
|
|
|
10000
|
|
|
|
)
|
2024-08-16 07:15:42 -04:00
|
|
|
}
|
|
|
|
timeoutLoop()
|
2024-08-04 00:51:30 -04:00
|
|
|
}
|
|
|
|
|
2024-08-16 07:15:42 -04:00
|
|
|
get machines(): MachinesListing {
|
2024-08-04 00:51:30 -04:00
|
|
|
return this._machines
|
|
|
|
}
|
|
|
|
|
|
|
|
machineCount(): number {
|
2024-08-28 15:15:37 -04:00
|
|
|
return this._machines.length
|
2024-08-04 00:51:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get machineApiIp(): string | null {
|
|
|
|
return this._machineApiIp
|
|
|
|
}
|
|
|
|
|
2024-08-19 15:57:31 -07:00
|
|
|
// Get the reason message for why there are no machines.
|
|
|
|
noMachinesReason(): string | undefined {
|
|
|
|
if (this.machineCount() > 0) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.machineApiIp === null) {
|
|
|
|
return 'Machine API server was not discovered'
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'Machine API server was discovered, but no machines are available'
|
|
|
|
}
|
|
|
|
|
2024-08-28 15:15:37 -04:00
|
|
|
get currentMachine(): components['schemas']['MachineInfoResponse'] | null {
|
2024-08-04 00:51:30 -04:00
|
|
|
return this._currentMachine
|
|
|
|
}
|
|
|
|
|
2024-08-28 15:15:37 -04:00
|
|
|
set currentMachine(
|
|
|
|
machine: components['schemas']['MachineInfoResponse'] | null
|
|
|
|
) {
|
2024-08-04 00:51:30 -04:00
|
|
|
this._currentMachine = machine
|
|
|
|
}
|
|
|
|
|
|
|
|
private async updateMachines(): Promise<void> {
|
2024-08-16 07:15:42 -04:00
|
|
|
if (!this._isDesktop) {
|
2024-08-04 00:51:30 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-17 15:30:46 -07:00
|
|
|
if (this._machineApiIp === null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this._machines = await window.electron.listMachines(this._machineApiIp)
|
2024-08-04 00:51:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private async updateMachineApiIp(): Promise<void> {
|
2024-08-16 07:15:42 -04:00
|
|
|
if (!this._isDesktop) {
|
2024-08-04 00:51:30 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-16 07:15:42 -04:00
|
|
|
this._machineApiIp = await window.electron.getMachineApiIp()
|
2024-08-04 00:51:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const machineManager = new MachineManager()
|
|
|
|
machineManager.start()
|