Skip to content

Commit

Permalink
delay launchPlaybackDolphin to make sure we never invoke more often t…
Browse files Browse the repository at this point in the history
…han every 1s

current Ishii sys/stat behavior only has second precision for file mod time, so comms file modifications won't be picked up if they happen in the same epoch second
  • Loading branch information
jmlee337 committed Mar 12, 2024
1 parent 87e1d81 commit b5880c0
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions src/remote/remote.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default class RemoteServer {
private dolphinManager: DolphinManager;
private settingsManager: SettingsManager;
private authToken: string;
private dolphinLaunchTimes: Map<string, number>;

private spectateWorker: SpectateWorker | null;
private httpServer: http.Server | null;
Expand All @@ -30,6 +31,7 @@ export default class RemoteServer {

constructor(dolphinManager: DolphinManager, settingsManager: SettingsManager) {
this.authToken = "";
this.dolphinLaunchTimes = new Map();
this.spectateWorker = null;
this.httpServer = null;
this.remoteServer = null;
Expand All @@ -56,6 +58,17 @@ export default class RemoteServer {
});
}

private async launchPlaybackDolphin(playbackId: string, filePath: string, replayComm: ReplayCommunication) {
try {
await this.dolphinManager.launchPlaybackDolphin(playbackId, replayComm);
if (this.connection && filePath) {
this.connection.sendUTF(JSON.stringify({ op: "new-file-event", dolphinId: playbackId, filePath }));
}
} catch (e) {
log.error(e);
}
}

private async createSpectateWorker() {
this.spectateWorker = await createSpectateWorker();
this.spectateWorker.getBroadcastListObservable().subscribe((data: BroadcasterItem[]) => {
Expand All @@ -69,14 +82,32 @@ export default class RemoteServer {
replay: filePath,
gameStation: broadcasterName,
};
try {
await this.dolphinManager.launchPlaybackDolphin(playbackId, replayComm);
if (this.connection && filePath) {
this.connection.sendUTF(JSON.stringify({ op: "new-file-event", dolphinId: playbackId, filePath }));
await new Promise<void>((resolve, reject) => {
const dolphinLaunchTime = this.dolphinLaunchTimes.get(playbackId);
if (dolphinLaunchTime !== undefined) {
const diff = Date.now() - dolphinLaunchTime;
if (diff < 1000) {
setTimeout(async () => {
try {
await this.launchPlaybackDolphin(playbackId, filePath, replayComm);
this.dolphinLaunchTimes.set(playbackId, Date.now());
resolve();
} catch (e) {
reject(e);
}
}, 1000 - diff);
return;
}
}
} catch (e) {
log.error(e);
}
this.launchPlaybackDolphin(playbackId, filePath, replayComm)
.then(() => {
this.dolphinLaunchTimes.set(playbackId, Date.now());
resolve();
})
.catch((e) => {
reject(e);
});
});
});
this.spectateWorker.getGameEndObservable().subscribe((dolphinId: string) => {
if (this.connection) {
Expand Down

0 comments on commit b5880c0

Please sign in to comment.