forked from snxraven/peardock
Grey out the Swarm sidebar item with a help popover explaining how to enable swarm mode. Recheck the peer on each hover and re-enable the nav when LocalNodeState becomes active.
100 lines
2.5 KiB
JavaScript
100 lines
2.5 KiB
JavaScript
/**
|
|
* Host snapshot cache for dynamic UI auto-population.
|
|
*/
|
|
import { manager, Methods } from './manager.js'
|
|
|
|
const TTL_MS = 8000
|
|
/** @type {{ data: object, at: number, peerId: string }|null} */
|
|
let cache = null
|
|
/** @type {Promise<object>|null} */
|
|
let inflight = null
|
|
|
|
export function clearSnapshotCache() {
|
|
cache = null
|
|
inflight = null
|
|
}
|
|
|
|
/**
|
|
* @param {{ force?: boolean }} [opts]
|
|
*/
|
|
export async function getSnapshot(opts = {}) {
|
|
const peerId = manager.active?.id || ''
|
|
const now = Date.now()
|
|
if (
|
|
!opts.force &&
|
|
cache &&
|
|
cache.peerId === peerId &&
|
|
now - cache.at < TTL_MS
|
|
) {
|
|
return cache.data
|
|
}
|
|
if (inflight && !opts.force) return inflight
|
|
|
|
inflight = (async () => {
|
|
if (!manager.active?.connected) {
|
|
throw new Error('Not connected')
|
|
}
|
|
const data = await manager.request(Methods.getHostSnapshot, {})
|
|
cache = { data, at: Date.now(), peerId }
|
|
inflight = null
|
|
return data
|
|
})().catch((err) => {
|
|
inflight = null
|
|
throw err
|
|
})
|
|
|
|
return inflight
|
|
}
|
|
|
|
export async function suggestNetwork(opts = {}) {
|
|
if (!manager.active?.connected) throw new Error('Not connected')
|
|
return manager.request(Methods.suggestNetworkIPAM, opts)
|
|
}
|
|
|
|
export async function suggestName(kind, base) {
|
|
if (!manager.active?.connected) throw new Error('Not connected')
|
|
return manager.request(Methods.suggestResourceName, { kind, base })
|
|
}
|
|
|
|
export async function suggestFromImage(image) {
|
|
if (!manager.active?.connected) throw new Error('Not connected')
|
|
return manager.request(Methods.suggestFromImage, { image })
|
|
}
|
|
|
|
export async function suggestDefaults(kind, args = {}) {
|
|
if (!manager.active?.connected) throw new Error('Not connected')
|
|
return manager.request(Methods.suggestDefaults, { kind, ...args })
|
|
}
|
|
|
|
export async function listUsedHostPorts() {
|
|
if (!manager.active?.connected) throw new Error('Not connected')
|
|
return manager.request(Methods.listUsedHostPorts, {})
|
|
}
|
|
|
|
/** Warm cache after connect (best-effort). Notifies UI of engine/swarm state. */
|
|
export function warmSnapshot() {
|
|
return getSnapshot({ force: true })
|
|
.then((data) => {
|
|
try {
|
|
if (typeof window !== 'undefined' && typeof window.peardockOps?.onHostSnapshot === 'function') {
|
|
window.peardockOps.onHostSnapshot(data)
|
|
}
|
|
} catch {
|
|
// ignore UI hooks
|
|
}
|
|
return data
|
|
})
|
|
.catch(() => {})
|
|
}
|
|
|
|
export default {
|
|
getSnapshot,
|
|
clearSnapshotCache,
|
|
warmSnapshot,
|
|
suggestNetwork,
|
|
suggestName,
|
|
suggestFromImage,
|
|
suggestDefaults,
|
|
listUsedHostPorts,
|
|
}
|