Skip to content

Commit

Permalink
feat(My Library): Handle all beatmap md5 (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
yadPe authored Apr 18, 2021
1 parent 3b51374 commit eab8b4f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
36 changes: 27 additions & 9 deletions src/App/Providers/HistoryProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import { memoize } from 'underscore';
export const HistoryContext = createContext();
export const useDownloadHistory = () => useContext(HistoryContext);

let memoizeCacheTimeMs = 0;
const hashFn = () => {
const now = Date.now();
const deltaT = now - memoizeCacheTimeMs < 5000;
if (deltaT === false) {
memoizeCacheTimeMs = now;
}
return String(memoizeCacheTimeMs);
};

class HistoryProvider extends Component {
static historyFileMapper(beatmapSet) {
if (Array.isArray(beatmapSet)) {
Expand All @@ -22,7 +32,7 @@ class HistoryProvider extends Component {
artist: beatmapsSetData[2],
creator: beatmapsSetData[3],
isUnplayed: beatmapsSetData[4],
md5: beatmapsSetData[5],
mapsMd5: beatmapsSetData[5],
audioPath: beatmapsSetData[6],
previewOffset: beatmapsSetData[7],
};
Expand All @@ -36,7 +46,7 @@ class HistoryProvider extends Component {
beatmapSet.artist,
beatmapSet.creator,
beatmapSet.isUnplayed,
beatmapSet.md5,
beatmapSet.mapsMd5,
beatmapSet.audioPath,
beatmapSet.previewOffset,
],
Expand Down Expand Up @@ -71,12 +81,19 @@ class HistoryProvider extends Component {
this._writeHistory();
}

static getHistoryValuesList = memoize(
history => {
return Object.values(history);
},
input => Object.keys(input).length.toString(),
);
static getHistoryValuesList = memoize(history => {
return Object.values(history);
}, hashFn);

static getMD5BeatmapsetMap = memoize(history => {
const historyValues = HistoryProvider.getHistoryValuesList(history);
return historyValues.reduce((acc, item) => {
item.mapsMd5.forEach(mapMd5 => {
acc[mapMd5] = item.id;
});
return acc;
}, {});
}, hashFn);

set = ({ beatmaps, overallDuration, overallUnplayedCount }) => {
this.setState({ history: beatmaps, stats: { overallDuration, overallUnplayedCount } });
Expand All @@ -96,7 +113,8 @@ class HistoryProvider extends Component {

containsMD5 = md5 => {
const { history } = this.state;
return HistoryProvider.getHistoryValuesList(history).find(item => item.md5 === md5);
const md5toId = HistoryProvider.getMD5BeatmapsetMap(history);
return history[md5toId[md5]];
};

clear = () => {
Expand Down
7 changes: 5 additions & 2 deletions src/electron/threads/osuSongsScan.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ parentPort.on('message', osuDbPath => {
let overallUnplayedCount = 0;
re.beatmaps.forEach(beatmap => {
if (beatmap.beatmapset_id === -1) return;
if (beatmaps[beatmap.beatmapset_id]) return;
if (beatmaps[beatmap.beatmapset_id]) {
beatmaps[beatmap.beatmapset_id].mapsMd5.push(beatmap.md5);
return;
}
if (beatmap.unplayed) overallUnplayedCount += 1;
overallDuration += beatmap.total_time;
beatmaps[beatmap.beatmapset_id] = {
Expand All @@ -21,7 +24,7 @@ parentPort.on('message', osuDbPath => {
artist: beatmap.artist_name,
creator: beatmap.creator_name,
isUnplayed: beatmap.unplayed,
md5: beatmap.md5,
mapsMd5: [beatmap.md5],
audioPath: join(beatmap.folder_name, beatmap.audio_file_name),
previewOffset: beatmap.preview_offset,
};
Expand Down

0 comments on commit eab8b4f

Please sign in to comment.