
let amtStore = {} // 动画存储库
let absTime = 0

// 生成注册卡 key
const diceKey = (num = 6) => {
  let arr = new Array(num).fill(null)
  arr.length = num
  arr = arr.map(() => {
    let x = 65 + Number((Math.random() * 52 + "").split(".")[0])
    if (x > 90 && x < 97) {
      x = x + 7
    }
    return String.fromCharCode(x)
  })
  return arr.join("")
}

const amtMount = (update) => {

  const key = diceKey()
  amtStore[key] = update
  return key

}

const amtRemove = (key) => {

  delete amtStore[key]

}

const amtStart = (e) => {

  const delay = (e - absTime) / 1000
  absTime = e

  Object.keys(amtStore).forEach(key => {
    amtStore[key](delay)
  })
  requestAnimationFrame(amtStart)

}

const amtStop = () => {
  for (const key in amtStore) {
    delete amtStore[key]
  }
  cancelAnimationFrame(amtStart)
}

const amtClean = () => {
  cancelAnimationFrame(amtStart)
  amtStore = {}
}

export {
  amtMount,
  amtRemove,
  amtStart,
  amtStop,
  amtClean
}