File
Blob: src/client/lib/request-guard.ts
| 1 | export interface RequestGuard { |
| 2 | isCurrent(): boolean; |
| 3 | cancel(): void; |
| 4 | } |
| 5 | |
| 6 | export function createRequestGuard(epochRef: { current: number }, activeRef: { current: boolean }): RequestGuard { |
| 7 | const epoch = ++epochRef.current; |
| 8 | let cancelled = false; |
| 9 | |
| 10 | return { |
| 11 | isCurrent() { |
| 12 | return !cancelled && activeRef.current && epoch === epochRef.current; |
| 13 | }, |
| 14 | cancel() { |
| 15 | cancelled = true; |
| 16 | }, |
| 17 | }; |
| 18 | } |