Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 5051b86

Browse files
committed
Debounce scanned file storage
1 parent 605b000 commit 5051b86

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

src/utils/main/ipc/scanner.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,25 @@ export class ScannerChannel implements IpcChannelInterface {
125125
} as Song
126126
}
127127

128+
private async saveToDb(songs: Song[]) {
129+
await getSongDB().store(...songs)
130+
}
131+
132+
private songList: Song[] = new Proxy<Song[]>([], {
133+
set: (target, property, value) => {
134+
target[property as unknown as number] = value
135+
if (target.length >= 50) {
136+
this.saveToDb(target.splice(0, target.length))
137+
}
138+
return true
139+
}
140+
})
141+
128142
private async storeSong(data: SongWithLen) {
129143
this.totalScanFiles = data.size
130144
this.reportProgress(data.current)
131145

132-
await getSongDB().store(this.parseScannedSong(data.song))
146+
this.songList.push(this.parseScannedSong(data.song))
133147
}
134148

135149
private parseScannedPlaylist(data: ScanPlaylist): Playlist {
@@ -181,7 +195,12 @@ export class ScannerChannel implements IpcChannelInterface {
181195
}
182196
}
183197
},
184-
() => Promise.allSettled(promises).then(() => resolve(lastValue))
198+
() =>
199+
Promise.allSettled(promises).then(() => {
200+
if (store) this.saveToDb(this.songList)
201+
this.songList = []
202+
return resolve(lastValue)
203+
})
185204
)
186205
})
187206
}

0 commit comments

Comments
 (0)