Skip to content

Commit

Permalink
window close detection
Browse files Browse the repository at this point in the history
  • Loading branch information
waa-test authored and porkchop committed Mar 28, 2023
1 parent b2ebe7b commit fb3e0fa
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/WaxEventSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export class WaxEventSource {

public async openPopup(url: string): Promise<Window> {
const win = await window.open(url, "WaxPopup", "height=800,width=600");

if (win) {
return win;
}
Expand Down Expand Up @@ -60,6 +59,7 @@ export class WaxEventSource {
type?: string
): Promise<T> {
return new Promise((resolve, reject) => {
let resolved = false;
window.addEventListener(
"message",
async function onEvent(event) {
Expand All @@ -79,15 +79,23 @@ export class WaxEventSource {
}

try {
resolved =true;
resolve(await action(event));
} catch (e) {
resolved = true;
reject(e);
}

window.removeEventListener("message", onEvent, false);
},
false
);
const interval = setInterval(()=> {
if(source.closed && !resolved){
clearInterval(interval);
reject("user closed the window");
}
},1000)
});
}

Expand Down

0 comments on commit fb3e0fa

Please sign in to comment.