import request from './request'
import { queryUserOne } from '@/api/sysSetting'

const cacheModule = {
  cache: {},
  temporaryStorage: {},
  getUserInfo(id) {
    const funArray = this.temporaryStorage[id]
    request.get(queryUserOne, { id }, response => {
      delete this.temporaryStorage[id]
      if (response.success && response.data) {
        this.cache[id] = response.data
        for (const fun of funArray.values()) {
          fun(this.cache[id])
        }
      }
    })
  },
  Judge(id, callback) {
    if (this.cache[id]) {
      callback(this.cache[id])
    } else {
      if (this.temporaryStorage[id]) {
        this.temporaryStorage[id].push(callback)
      } else {
        this.temporaryStorage[id] = [callback]
        this.getUserInfo(id)
      }
    }
  }
}

export default cacheModule
