Skip to content

Commit 05099d4

Browse files
perf: Third-party App Token Refresh Mechanism in the Assistant
1 parent f790f24 commit 05099d4

File tree

1 file changed

+56
-56
lines changed

1 file changed

+56
-56
lines changed

frontend/src/stores/assistant.ts

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface AssistantState {
2525
hostOrigin: string
2626
autoDs?: boolean
2727
requestPromiseMap: Map<string, PendingRequest[]>
28-
pedding: boolean
28+
peddingStatus: number //0: ready,1: pedding,2:finish
2929
certificateTime: number
3030
}
3131

@@ -44,7 +44,7 @@ export const AssistantStore = defineStore('assistant', {
4444
hostOrigin: '',
4545
autoDs: false,
4646
requestPromiseMap: new Map<string, PendingRequest[]>(),
47-
pedding: false,
47+
peddingStatus: 0,
4848
certificateTime: 0,
4949
}
5050
},
@@ -87,49 +87,32 @@ export const AssistantStore = defineStore('assistant', {
8787
},
8888
},
8989
actions: {
90-
refreshCertificate<T>(requestUrl?: string) {
91-
if (+new Date() > this.certificateTime + 5000) {
90+
refreshCertificate(requestUrl?: string) {
91+
/* if (+new Date() < this.certificateTime + 5000) {
9292
return
93-
}
94-
const timeout = 5000
95-
let peddingList = this.requestPromiseMap.get(this.id)
93+
} */
94+
95+
const timeout = 30000
96+
let peddingList = this.requestPromiseMap.get(this.id) as PendingRequest[]
9697
if (!peddingList) {
9798
this.requestPromiseMap.set(this.id, [])
98-
peddingList = this.requestPromiseMap.get(this.id)
99+
peddingList = this.requestPromiseMap.get(this.id) as PendingRequest[]
99100
}
100101

101-
const removeRequest = (requestId: string) => {
102-
if (!peddingList) return
103-
let len = peddingList.length
104-
while (len--) {
105-
const peddingRequest = peddingList[len]
106-
if (peddingRequest?.requestId === requestId) {
107-
peddingList.splice(len, 1)
108-
}
102+
if (this.peddingStatus === 2) {
103+
if (peddingList?.length) {
104+
return
105+
} else {
106+
this.peddingStatus = 0
109107
}
110108
}
111109

112-
const addRequest = (requestId: string, resolve: any, reject: any) => {
113-
const currentPeddingRequest = {
114-
requestId,
115-
resolve: (value: T) => {
116-
removeRequest(requestId)
117-
resolve(value)
118-
},
119-
reject: (reason: any) => {
120-
removeRequest(requestId)
121-
reject(reason)
122-
},
123-
} as PendingRequest
124-
peddingList?.push(currentPeddingRequest)
125-
}
126-
127110
return new Promise((resolve, reject) => {
128-
const currentRequestId = `${this.id}|${+new Date()}`
111+
const currentRequestId = `${this.id}|${requestUrl}|${+new Date()}`
129112
const timeoutId = setTimeout(() => {
130-
removeRequest(currentRequestId)
131113
console.error(`Request ${currentRequestId}[${requestUrl}] timed out after ${timeout}ms`)
132114
resolve(null)
115+
removeRequest(currentRequestId, peddingList)
133116
if (timeoutId) {
134117
clearTimeout(timeoutId)
135118
}
@@ -141,53 +124,41 @@ export const AssistantStore = defineStore('assistant', {
141124
clearTimeout(timeoutId)
142125
}
143126
resolve(value)
127+
removeRequest(currentRequestId, peddingList)
144128
}
145129

146130
const cleanupAndReject = (reason: any) => {
147131
if (timeoutId) {
148132
clearTimeout(timeoutId)
149133
}
134+
removeRequest(currentRequestId, peddingList)
150135
reject(reason)
151136
}
152137

153-
addRequest(currentRequestId, cleanupAndResolve, cleanupAndReject)
154-
if (!this.pedding) {
138+
addRequest(currentRequestId, cleanupAndResolve, cleanupAndReject, peddingList)
139+
if (this.peddingStatus !== 1) {
140+
this.peddingStatus = 1
155141
const readyData = {
156142
eventName: this.pageEmbedded ? 'sqlbot_embedded_event' : 'sqlbot_assistant_event',
157143
busi: 'ready',
158144
ready: true,
159145
messageId: this.id,
160146
}
161147
window.parent.postMessage(readyData, '*')
162-
this.pedding = true
163148
}
164149
})
165150
},
166151
resolveCertificate(data?: any) {
167152
const peddingRequestList = this.requestPromiseMap.get(this.id)
153+
168154
if (peddingRequestList?.length) {
169-
peddingRequestList.forEach((peddingRequest: PendingRequest) => {
155+
let len = peddingRequestList?.length
156+
while (len--) {
157+
const peddingRequest: PendingRequest = peddingRequestList[len]
170158
peddingRequest.resolve(data)
171-
})
172-
}
173-
this.pedding = false
174-
/* const resolvePromiseList = [] as Promise<void>[]
175-
if (peddingRequestList?.length) {
176-
peddingRequestList.forEach((peddingRequest: PendingRequest) => {
177-
const resolvePromise = new Promise((r: any) => {
178-
peddingRequest.resolve(data)
179-
r()
180-
})
181-
resolvePromiseList.push(resolvePromise as Promise<void>)
182-
})
159+
}
183160
}
184-
if (resolvePromiseList?.length) {
185-
Promise.all(resolvePromiseList).then(() => {
186-
this.pedding = false
187-
})
188-
} else {
189-
this.pedding = false
190-
} */
161+
this.peddingStatus = 2
191162
},
192163
setId(id: string) {
193164
this.id = id
@@ -243,6 +214,35 @@ export const AssistantStore = defineStore('assistant', {
243214
},
244215
})
245216

217+
const removeRequest = (requestId: string, peddingList: PendingRequest[]) => {
218+
if (!peddingList) return
219+
let len = peddingList.length
220+
while (len--) {
221+
const peddingRequest = peddingList[len]
222+
if (peddingRequest?.requestId === requestId) {
223+
peddingList.splice(len, 1)
224+
}
225+
}
226+
}
227+
228+
const addRequest = (
229+
requestId: string,
230+
resolve: any,
231+
reject: any,
232+
peddingList: PendingRequest[]
233+
) => {
234+
const currentPeddingRequest = {
235+
requestId,
236+
resolve: (value: any) => {
237+
resolve(value)
238+
},
239+
reject: (reason: any) => {
240+
reject(reason)
241+
},
242+
} as PendingRequest
243+
peddingList?.push(currentPeddingRequest)
244+
}
245+
246246
export const useAssistantStore = () => {
247247
return AssistantStore(store)
248248
}

0 commit comments

Comments
 (0)