Skip to content

Commit

Permalink
move defer comms file write logic to PlaybackDolphinInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
jmlee337 committed Apr 16, 2024
1 parent 85e87be commit c527bf8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 49 deletions.
25 changes: 23 additions & 2 deletions src/dolphin/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ export class DolphinInstance extends EventEmitter {

export class PlaybackDolphinInstance extends DolphinInstance {
private commPath: string;
private lastWriteMs: number;

constructor(execPath: string, isoPath?: string) {
super(execPath, isoPath);
this.commPath = generateTempCommunicationFile();
this.lastWriteMs = 0;

// Delete the comm file once Dolphin is closed
this.on("close", async () => {
Expand All @@ -118,8 +120,27 @@ export class PlaybackDolphinInstance extends DolphinInstance {
options.commandId = Math.random().toString(36).slice(2);
}

// Write out the comms file
await fs.writeFile(this.commPath, JSON.stringify(options));
// defer writing comms file if it's been less than 1 second since last write
// however this will only work if only one invocation comes 'early'
// if 2 or more invocations happen within a second after the last,
// they will all be deferred to approximately the same time.
// I don't think it's deterministic which one will go first (and therefore succeed).
const diff = Date.now() - this.lastWriteMs;
if (diff < 1000) {
await new Promise<void>((resolve, reject) => {
setTimeout(async () => {
try {
await fs.writeFile(this.commPath, JSON.stringify(options));
resolve();
} catch (e: any) {
reject(e);
}
}, 1000 - diff);
});
} else {
await fs.writeFile(this.commPath, JSON.stringify(options));
}
this.lastWriteMs = Date.now();

if (!this.process) {
const params: string[] = [];
Expand Down
53 changes: 6 additions & 47 deletions src/remote/remote_server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { BroadcasterItem, SpectateController, SpectateDolphinOptions } from "@broadcast/types";
import type { DolphinManager } from "@dolphin/manager";
import type { DolphinPlaybackClosedEvent, ReplayCommunication } from "@dolphin/types";
import type { DolphinPlaybackClosedEvent } from "@dolphin/types";
import { DolphinEventType, DolphinLaunchType } from "@dolphin/types";
import type { SettingsManager } from "@settings/settings_manager";
import electronLog from "electron-log";
Expand Down Expand Up @@ -55,59 +55,18 @@ export 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 setupSpectateController() {
this.spectateController = await this.getSpectateController();
this.spectateController.getBroadcastListObservable().subscribe((data: BroadcasterItem[]) => {
if (this.connection) {
this.connection.sendUTF(JSON.stringify({ op: "list-broadcasts-response", broadcasts: data }));
}
});
this.spectateController
.getSpectateDetailsObservable()
.subscribe(async ({ playbackId, filePath, broadcasterName }) => {
const replayComm: ReplayCommunication = {
mode: "mirror",
replay: filePath,
gameStation: broadcasterName,
};
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;
}
}
this.launchPlaybackDolphin(playbackId, filePath, replayComm)
.then(() => {
this.dolphinLaunchTimes.set(playbackId, Date.now());
resolve();
})
.catch((e) => {
reject(e);
});
});
});
this.spectateController.getSpectateDetailsObservable().subscribe(async ({ playbackId, filePath }) => {
if (this.connection && filePath) {
this.connection.sendUTF(JSON.stringify({ op: "new-file-event", dolphinId: playbackId, filePath }));
}
});
this.spectateController.getGameEndObservable().subscribe((dolphinId: string) => {
if (this.connection) {
this.connection.sendUTF(JSON.stringify({ op: "game-end-event", dolphinId }));
Expand Down

0 comments on commit c527bf8

Please sign in to comment.