mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-04-02 15:13:46 +00:00
wip(app): global config
This commit is contained in:
@@ -5,6 +5,7 @@ import { State } from "./state"
|
||||
import { iife } from "@/util/iife"
|
||||
import { GlobalBus } from "@/bus/global"
|
||||
import { Filesystem } from "@/util/filesystem"
|
||||
import { withTimeout } from "@/util/timeout"
|
||||
|
||||
interface Context {
|
||||
directory: string
|
||||
@@ -14,6 +15,8 @@ interface Context {
|
||||
const context = Context.create<Context>("instance")
|
||||
const cache = new Map<string, Promise<Context>>()
|
||||
|
||||
const DISPOSE_TIMEOUT_MS = 10_000
|
||||
|
||||
export const Instance = {
|
||||
async provide<R>(input: { directory: string; init?: () => Promise<any>; fn: () => R }): Promise<R> {
|
||||
let existing = cache.get(input.directory)
|
||||
@@ -78,13 +81,18 @@ export const Instance = {
|
||||
},
|
||||
async disposeAll() {
|
||||
Log.Default.info("disposing all instances")
|
||||
for (const [_key, value] of cache) {
|
||||
const awaited = await value.catch(() => {})
|
||||
if (awaited) {
|
||||
await context.provide(await value, async () => {
|
||||
await Instance.dispose()
|
||||
})
|
||||
for (const [key, value] of cache) {
|
||||
const ctx = await withTimeout(value, DISPOSE_TIMEOUT_MS).catch((error) => {
|
||||
Log.Default.warn("instance dispose timed out", { key, error })
|
||||
return undefined
|
||||
})
|
||||
if (!ctx) {
|
||||
cache.delete(key)
|
||||
continue
|
||||
}
|
||||
await context.provide(ctx, async () => {
|
||||
await Instance.dispose()
|
||||
})
|
||||
}
|
||||
cache.clear()
|
||||
},
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Log } from "@/util/log"
|
||||
import { withTimeout } from "@/util/timeout"
|
||||
|
||||
export namespace State {
|
||||
interface Entry {
|
||||
@@ -7,6 +8,7 @@ export namespace State {
|
||||
}
|
||||
|
||||
const log = Log.create({ service: "state" })
|
||||
const DISPOSE_TIMEOUT_MS = 10_000
|
||||
const recordsByKey = new Map<string, Map<any, Entry>>()
|
||||
|
||||
export function create<S>(root: () => string, init: () => S, dispose?: (state: Awaited<S>) => Promise<void>) {
|
||||
@@ -46,14 +48,21 @@ export namespace State {
|
||||
}, 10000).unref()
|
||||
|
||||
const tasks: Promise<void>[] = []
|
||||
for (const entry of entries.values()) {
|
||||
for (const [init, entry] of entries) {
|
||||
if (!entry.dispose) continue
|
||||
|
||||
const task = Promise.resolve(entry.state)
|
||||
.then((state) => entry.dispose!(state))
|
||||
.catch((error) => {
|
||||
log.error("Error while disposing state:", { error, key })
|
||||
})
|
||||
const label = typeof init === "function" ? init.name : String(init)
|
||||
|
||||
const task = withTimeout(
|
||||
Promise.resolve(entry.state).then((state) => entry.dispose!(state)),
|
||||
DISPOSE_TIMEOUT_MS,
|
||||
).catch((error) => {
|
||||
if (error instanceof Error && error.message.includes("Operation timed out")) {
|
||||
log.warn("state disposal timed out", { key, init: label })
|
||||
return
|
||||
}
|
||||
log.error("Error while disposing state:", { error, key, init: label })
|
||||
})
|
||||
|
||||
tasks.push(task)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user