1export interface IIndexMap { 2 getIndexMap: () => Record<string, Record<string, number>>; 3 getIndex: (mediaItem: ICommon.IMediaBase) => number; 4 has: (mediaItem: ICommon.IMediaBase) => boolean; 5} 6 7export function createMediaIndexMap( 8 mediaItems: ICommon.IMediaBase[], 9): IIndexMap { 10 const indexMap: Record<string, Record<string, number>> = {}; 11 12 mediaItems.forEach((item, index) => { 13 // 映射中不存在 14 if (!indexMap[item.platform]) { 15 indexMap[item.platform] = { 16 [item.id]: index, 17 }; 18 } else { 19 // 修改映射 20 indexMap[item.platform][item.id] = index; 21 } 22 }); 23 24 function getIndexMap() { 25 return indexMap; 26 } 27 28 function getIndex(mediaItem: ICommon.IMediaBase) { 29 if (!mediaItem) { 30 return -1; 31 } 32 return indexMap[mediaItem.platform]?.[mediaItem.id] ?? -1; 33 } 34 35 function has(mediaItem: ICommon.IMediaBase) { 36 if (!mediaItem) { 37 return false; 38 } 39 40 return indexMap[mediaItem.platform]?.[mediaItem.id] > -1; 41 } 42 43 return { 44 getIndexMap, 45 getIndex, 46 has, 47 }; 48} 49