so now we're on fly.io

This commit is contained in:
EGOIST
2022-05-16 14:13:44 +08:00
parent 358c6631cb
commit c3aa3fd967
116 changed files with 418 additions and 1828 deletions
+43
View File
@@ -0,0 +1,43 @@
// @ts-nocheck
globalThis._singletons = globalThis._singletons || {}
export const singleton = <T>(id: string, factory: () => T): T => {
if (!globalThis._singletons[id]) {
globalThis._singletons[id] = factory()
}
return globalThis._singletons[id]
}
export const singletonAsync = <T>(
id: string,
factory: () => Promise<T>,
enableSingleton = true
): { readonly value: T; wait: Promise<void> } => {
if (globalThis._singletons[id] && enableSingleton) {
return {
wait: Promise.resolve(),
get value() {
return globalThis._singletons[id]
},
}
}
let ready = false
const wait = factory().then((value) => {
ready = true
globalThis._singletons[id] = value
})
return {
wait,
get value() {
if (!ready) {
throw new Error(`please await .wait before using the value of '${id}'`)
}
return globalThis._singletons[id]
},
}
}