Line data Source code
1 : import { api } from '@/scripts/api'
2 :
3 : /**
4 : * `/folder_paths` answers with the directories ComfyUI resolved at startup, so
5 : * a given backend process keeps answering the same way. One in-flight request
6 : * is shared, and a reconnect invalidates it, since the backend on the other
7 : * side may be a different process with different paths.
8 : */
9 2355 : let pending: Promise<Record<string, string[]>> | undefined
10 2355 : let listening = false
11 :
12 0 : function invalidate(): void {
13 0 : pending = undefined
14 : }
15 :
16 0 : export function loadFolderPathsOnce(): Promise<Record<string, string[]>> {
17 0 : if (!listening) {
18 0 : listening = true
19 0 : api.addEventListener('reconnected', invalidate)
20 : }
21 0 : if (pending) return pending
22 :
23 0 : const request: Promise<Record<string, string[]>> = api
24 : .getFolderPaths()
25 : .catch((error: unknown) => {
26 : // A failure must not be cached, but only this request may clear it: a
27 : // reconnect, or a caller that asked after one, may already have put a
28 : // newer request in its place.
29 0 : if (pending === request) pending = undefined
30 0 : throw error
31 : })
32 0 : pending = request
33 0 : return request
34 : }
35 :
36 : /** Test seam. Production invalidation happens on reconnect. */
37 : export function resetFolderPathCache(): void {
38 : invalidate()
39 : }
|