All files / src canceller.ts

92.1% Statements 35/38
83.33% Branches 10/12
72.72% Functions 8/11
93.75% Lines 30/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 612x         2x     126x 126x 126x       342x 342x 84x 84x 60x 60x   24x       2x 126x     2x 342x     2x 98x 98x 96x     2x 48x 72x 72x 72x         2x   2x       2x       2x 149x  
export { generateRequestHash  } from "./utils"
 
type CancelRecord = Record<string, Record<string, any>>
 
// 用来存储取消函数
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) {
  Iif (!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]
}