46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
/**
|
||
* 设计器用的流程清单 + 分类清单:挑流程的地方都从这一份拿。
|
||
*
|
||
* 「只允许关联某个流程」「带出另一个流程的数据」这些面板一次挂好几个,每个自己发一次请求
|
||
* 既慢又容易各看各的。这里缓存一小会儿(60 秒):够盖住一次设计过程里的反复点开,
|
||
* 又不至于新建了流程还要刷新页面才看得见。
|
||
*/
|
||
import { designAPI } from '@/api'
|
||
import type { Category, ProcessDef } from '@/types'
|
||
|
||
export interface ProcessCatalog {
|
||
categories: Category[]
|
||
processes: ProcessDef[]
|
||
}
|
||
|
||
/** 没归到任何分类的流程单独占一档,不然它们永远选不到。 */
|
||
export const UNCATEGORISED = '__none__'
|
||
|
||
const TTL_MS = 60_000
|
||
|
||
let cache: { at: number; value: ProcessCatalog } | null = null
|
||
let inflight: Promise<ProcessCatalog> | null = null
|
||
|
||
const EMPTY: ProcessCatalog = { categories: [], processes: [] }
|
||
|
||
export function processCatalog(force = false): Promise<ProcessCatalog> {
|
||
if (!force && cache && Date.now() - cache.at < TTL_MS) return Promise.resolve(cache.value)
|
||
if (inflight) return inflight
|
||
inflight = Promise.all([designAPI.listCategories(), designAPI.listProcesses()])
|
||
.then(([categories, processes]) => {
|
||
const value = { categories, processes }
|
||
cache = { at: Date.now(), value }
|
||
return value
|
||
})
|
||
.catch(() => EMPTY)
|
||
.finally(() => {
|
||
inflight = null
|
||
})
|
||
return inflight
|
||
}
|
||
|
||
/** 这条流程属于哪一档(没分类的落到未分类那一档)。 */
|
||
export function categoryOf(item: ProcessDef): string {
|
||
return item.category || UNCATEGORISED
|
||
}
|