Skip to content

Commit

Permalink
Add wait-for-shutdown API
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Jan 16, 2019
1 parent 6d3e2fb commit faa22f6
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions webextensions/common/tst-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function log(...args) {

export const kREGISTER_SELF = 'register-self';
export const kUNREGISTER_SELF = 'unregister-self';
export const kWAIT_FOR_SHUTDOWN = 'wait-for-shutdown';
export const kPING = 'ping';
export const kNOTIFY_READY = 'ready';
export const kNOTIFY_SHUTDOWN = 'shutdown'; // defined but not notified for now.
Expand Down Expand Up @@ -253,6 +254,12 @@ browser.runtime.onMessage.addListener((message, _sender) => {
}
});

const mPromisedOnBeforeUnload = new Promise((resolve, _reject) => {
// If this promise doesn't do anything then there seems to be a timeout so it only works if TST is disabled within about 10 seconds after this promise is used as a response to a message. After that it will not throw an error for the waiting extension.
// If we use the following then the returned promise will be rejected when TST is disabled even for longer times:
window.addEventListener('beforeunload', () => resolve());
});

browser.runtime.onMessageExternal.addListener((message, sender) => {
if (!message ||
typeof message.type != 'string')
Expand Down Expand Up @@ -293,6 +300,9 @@ browser.runtime.onMessageExternal.addListener((message, sender) => {
configs.cachedExternalAddons = configs.cachedExternalAddons.filter(id => id != sender.id);
return true;
})();

case kWAIT_FOR_SHUTDOWN:
return mPromisedOnBeforeUnload;
}
break;

Expand Down

0 comments on commit faa22f6

Please sign in to comment.