/** * 取消相同URL和method的上一个未结束请求,主要目的是防止页面切换时串数据,比如tab切换 * 且只针对get请求 */ // import hash from 'object-hash' export { generateRequestHash } from './utils' const cancellerCache: Record> = {} export function addRequestCanceler(url: string, hash: string, cancelFn: () => void) { const urlCache = cancellerCache[url] || {} urlCache[hash] = cancelFn cancellerCache[url] = urlCache } export function clearCancelerByHash(url: string, hash: string) { if (cancellerCache[url] && cancellerCache[url][hash]) { Reflect.deleteProperty(cancellerCache[url], hash) if (Object.keys(cancellerCache[url]).length === 0) { Reflect.deleteProperty(cancellerCache, url) } } } // export function checkIfHasURLCanceler(url: string): boolean { // if (!cancellerCache[url]) return false // if (Object.keys(cancellerCache[url]).length === 0) return false // return true // } export function execCanceler(url: string) { const cancelers = cancellerCache[url] || {} Object.entries(cancelers).forEach(([key, fn]) => { fn() Reflect.deleteProperty(cancelers, key) }) Reflect.deleteProperty(cancellerCache, url) } // export function getCachedCanceler(hashKey: string) { // return cancellerCache[hashKey] // }