UNPKG

when-exit

Version:

Execute a function right before the process, or the browser's tab, is about to exit.

28 lines (27 loc) 661 B
/* IMPORT */ /* MAIN */ class Interceptor { /* CONSTRUCTOR */ constructor() { /* VARIABLES */ this.callbacks = new Set(); /* API */ this.exit = () => { for (const callback of this.callbacks) { callback(); } }; this.hook = () => { window.addEventListener('beforeunload', this.exit); }; this.register = (callback) => { this.callbacks.add(callback); return () => { this.callbacks.delete(callback); }; }; this.hook(); } } /* EXPORT */ export default new Interceptor();