Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added cancel message on window close #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions packages/proton-web-sdk/src/links/protonWeb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,23 @@ export class ProtonWebLink {
chainId: string

public get childWindow() {
return _childWindow;
return _childWindow
}

public set childWindow(window: Window | null) {
_childWindow = window
if(_childWindow !== window) {
_childWindow = window
if(!_childWindow && this.closeCheckInterval) {
clearInterval(this.closeCheckInterval);
}
if(_childWindow) {
this.closeCheckInterval = setInterval(() => this.checkChildWindowClosed(), 500)
}
}
}

private closeCheckInterval: NodeJS.Timer | null = null

constructor(options: LinkOptions & { testUrl?: string }) {
this.scheme = options.scheme
this.client = typeof options.client === 'string' ? new JsonRpc(options.client) : options.client
Expand All @@ -54,7 +64,6 @@ export class ProtonWebLink {
this.transport = options.transport
this.chainId = options.chainId!.toString()

setInterval(() => this.closeChild(), 500)
window.addEventListener('message', (event) => this.onEvent(event), false)
}

Expand All @@ -67,15 +76,15 @@ export class ProtonWebLink {
return `${base}${path}`
}

closeChild(force = false) {
closeChild() {
if (this.childWindow) {
if (force) {
this.childWindow.close()
}

if (force || this.childWindow.closed) {
if(this.closeCheckInterval) {
clearInterval(this.closeCheckInterval);
}
if(!this.childWindow.closed) {
this.childWindow.close()
}
this.childWindow = null
}
}
}

Expand All @@ -85,7 +94,7 @@ export class ProtonWebLink {
chainId: this.chainId,
transact: async (args: TransactArgs, options?: TransactOptions): Promise<any> => {
if (this.deferredLogin) {
this.closeChild(true)
this.closeChild()
this.deferredLogin.reject('Trying to login')
this.deferredLogin = undefined
}
Expand Down Expand Up @@ -118,7 +127,7 @@ export class ProtonWebLink {

async login() {
if (this.deferredTransact) {
this.closeChild(true)
this.closeChild()
this.deferredTransact.deferral.reject('Trying to login')
this.deferredTransact = undefined
}
Expand Down Expand Up @@ -203,7 +212,7 @@ export class ProtonWebLink {
}
// Close child
else if (type === 'close') {
this.closeChild(true)
this.closeChild()

if (this.deferredTransact) {
this.deferredTransact.deferral.reject('Closed')
Expand All @@ -213,7 +222,7 @@ export class ProtonWebLink {
}
// TX Success
else if (type === 'transactionSuccess') {
this.closeChild(true)
this.closeChild()

if (this.deferredTransact) {
if (error) {
Expand All @@ -227,7 +236,7 @@ export class ProtonWebLink {
}
// Login success
else if (type === 'loginSuccess') {
this.closeChild(true)
this.closeChild()

if (this.deferredLogin) {
this.deferredLogin.resolve(data)
Expand All @@ -238,4 +247,17 @@ export class ProtonWebLink {
console.error(e)
}
}

private checkChildWindowClosed() {
if (this.childWindow && this.childWindow.closed) {

window.postMessage(
JSON.stringify({
type: 'close',
}),
'*',
)

}
}
}