Skip to content

Commit

Permalink
Suggested fix for midi-mixer#13
Browse files Browse the repository at this point in the history
  • Loading branch information
lwYeo authored Dec 7, 2022
1 parent 62761d3 commit 77795b9
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ let inputs: Record<string, Assignment> = {};
let scenes: Record<string, ButtonType> = {};
const settingsP: Promise<Settings> = $MM.getSettings();
let settings: Settings;
let wsConnected = false;
let wsConnecting = false;

const connect = async () => {
settings = await settingsP;
Expand Down Expand Up @@ -65,7 +67,15 @@ const registerListeners = () => {

obs.on("ExitStarted", () => {
disconnect();
init();

(async () => {
while (!wsConnected) {
await new Promise(f => setTimeout(f, 1000));
if (!wsConnecting) {
init();
}
}
})();
})
};

Expand Down Expand Up @@ -149,8 +159,10 @@ const mapScenes = async () => {
});
};


function disconnect() {
console.log("Disconnecting");
wsConnected = false;
obs.disconnect();
for (let k in inputs) {
let s = inputs[k];
Expand All @@ -163,13 +175,13 @@ function disconnect() {
}
}


const init = async () => {
console.log("Initializing");
obs.disconnect();
inputs = {};
scenes = {};


wsConnecting = true;
try {
$MM.setSettingsStatus("status", "Connecting...");

Expand All @@ -178,10 +190,12 @@ const init = async () => {
await Promise.all([mapSources(), mapScenes()]);

$MM.setSettingsStatus("status", "Connected");
wsConnected = true;
} catch (err: any) {
console.warn("OBS error:", err);
$MM.setSettingsStatus("status", err.description || err.message || err);
}
wsConnecting = false;
};

$MM.onSettingsButtonPress("reconnect", init);
Expand Down

0 comments on commit 77795b9

Please sign in to comment.