export { generateRequestHash } from './utils' type CancelRecord = Record> // 用来存储取消函数 export const canceller: CancelRecord = {} function addRecord(record: CancelRecord, cancelKey: string, hash: string, cancelFn: any) { const cancelObj = record[cancelKey] || {} cancelObj[hash] = cancelFn record[cancelKey] = cancelObj } function deleteRecordByHash(record: CancelRecord, cancelKey: string, hash: string) { const cancelObj = record[cancelKey] || {} if (!cancelObj[hash]) return Reflect.deleteProperty(cancelObj, hash) if (Object.keys(cancelObj).length === 0) { Reflect.deleteProperty(record, cancelKey) return } record[cancelKey] = cancelObj } export function addCanceller(cancelKey: string, hash: string, cancelFn: any) { addRecord(canceller, cancelKey, hash, cancelFn) } export function deleteCancellerByHash(cancelKey: string, hash: string) { deleteRecordByHash(canceller, cancelKey, hash) } export function hasCancelers(cancelKey: string) { const cancelObj = canceller[cancelKey] if (!cancelObj || Object.keys(cancelObj).length === 0) return false return true } export function callCancellerByCancelKey(cancelKey: string) { if (!hasCancelers(cancelKey)) return Object.entries(canceller[cancelKey]).forEach(([hash, fn]) => { fn.call() deleteCancellerByHash(cancelKey, hash) }) } // 用来存储已经被调用的取消函数名称 export const cancellerCalled: CancelRecord = {} export function addCalledCanceller(cancelKey: string, hash: string, cancelFn: any) { addRecord(cancellerCalled, cancelKey, hash, cancelFn) } export function deleteCalledCancellerByHash(cancelKey: string, hash: string) { deleteRecordByHash(cancellerCalled, cancelKey, hash) } export function hasCalledCancelerRecord(cancelKey: string, hash: string) { return cancellerCalled[cancelKey] && cancellerCalled[cancelKey][hash] }