Fix: readData must have an atomic part.

This commit is contained in:
John Livingston 2023-09-11 09:30:20 +02:00
parent c900d2d1d4
commit ea1c008ded
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC

View File

@ -87,9 +87,6 @@ class RoomChannel {
public async readData (): Promise<boolean> {
// Reading the data file (see https://livingston.frama.io/peertube-plugin-livechat/fr/technical/data/)
this.room2Channel.clear()
this.channel2Rooms.clear()
let content: string
try {
content = (await fs.promises.readFile(this.dataFilePath)).toString()
@ -107,6 +104,18 @@ class RoomChannel {
return false
}
// This part must be done atomicly:
return this._readData(data)
}
/**
* _readData is the atomic part of readData:
* once the date are read from disk, object data must be emptied and filled atomicly.
*/
protected _readData (data: any): boolean {
this.room2Channel.clear()
this.channel2Rooms.clear()
if (typeof data !== 'object') {
this.logger.error('Invalid room-channel data file content')
return false