import { getAssetUrl } from '@/api/asset' import { getProgram as programApi } from '@/api/program' import { getSchedule as scheduleApi } from '@/api/calendar' import { ScheduleType } from '@/constant' const cacheMap = {} function getProgramInst (id) { return programApi(id, { custom: true, background: true }).then(({ data }) => data) } function getScheduleInst (id) { return scheduleApi(id, { custom: true, background: true }) } function getInstName ({ name }) { return name } function getProgramImage ({ img }) { return getAssetUrl(img) } function getScheduleImage ({ events }) { return getImage(ScheduleType.CALENDAR, events[0].programId) } function getCache (type) { if (!cacheMap[type]) { cacheMap[type] = {} } return cacheMap[type] } function getPromise (type, id) { const cache = getCache() if (!cache[id]) { const promise = (type === ScheduleType.CALENDAR ? getProgramInst : getScheduleInst)(id) promise.catch(() => { cache[id] = null }) cache[id] = promise } return cache[id] } export function getName (type, id) { return getPromise(type, id).then(getInstName) } export function getImage (type, id) { return getPromise(type, id).then(type === ScheduleType.CALENDAR ? getProgramImage : getScheduleImage) }