utils.ts
1 import { v4 as uuidv4 } from 'uuid' 2 3 export type anyDict = Record<string, any> 4 export type strDict = Record<string, string> 5 const textCache: strDict = {} 6 7 export const getCachedText = (id: string): string => { 8 const prompt = textCache[id] 9 delete textCache[id] 10 return prompt 11 } 12 13 export const putCachedText = (text: string): string => { 14 Object.keys(textCache).forEach((key) => { 15 delete textCache[key] 16 }) 17 18 const id = uuidv4() 19 textCache[id] = text 20 return id 21 } 22 23 export const wait = async (millis = 200) => { 24 if (process.env.DEBUG && process.platform !== 'darwin') { 25 // for an unknown reason, the promise code started to fail when debugging on Windows/Linux 26 const waitTill = new Date().getTime() + millis 27 while (waitTill > new Date().getTime()) { 28 // do nothing 29 } 30 } else { 31 await new Promise((resolve) => setTimeout(resolve, millis)) 32 } 33 }