Skip to content

Commit

Permalink
Improved suggested fix for midi-mixer#13
Browse files Browse the repository at this point in the history
Fix should also apply to when plugin is first started before OBS.
  • Loading branch information
lwYeo authored Dec 8, 2022
1 parent 77795b9 commit ab9e062
Showing 1 changed file with 27 additions and 31 deletions.
58 changes: 27 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ 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 @@ -67,15 +65,7 @@ const registerListeners = () => {

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

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

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


function disconnect() {
console.log("Disconnecting");
wsConnected = false;
obs.disconnect();
for (let k in inputs) {
let s = inputs[k];
Expand All @@ -177,27 +165,35 @@ function disconnect() {

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

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

await connect();
registerListeners();
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);

let wsConnected = false;
while (!wsConnected) {
obs.disconnect();
inputs = {};
scenes = {};

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

await connect();
registerListeners();
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);

await delay(1000);
}
}
wsConnecting = false;
};

const delay = (delayMs: number) => {
return new Promise(resolve => setTimeout(resolve, delayMs));
}

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

init();

0 comments on commit ab9e062

Please sign in to comment.