pinafore/src/routes/_utils/lifecycle.js
2021-03-14 10:20:23 -07:00

22 lines
696 B
JavaScript

// the page-lifecycle package causes some problems (doesn't work in node),
// and plus it's not needed immediately, so lazy-load it
import { importPageLifecycle } from './asyncModules/importPageLifecycle.js'
function addEventListener (event, func) {
if (process.browser && !process.env.IS_SERVICE_WORKER) {
importPageLifecycle().then(lifecycle => {
lifecycle.addEventListener(event, func)
})
}
}
function removeEventListener (event, func) {
if (process.browser && !process.env.IS_SERVICE_WORKER) {
importPageLifecycle().then(lifecycle => {
lifecycle.removeEventListener(event, func)
})
}
}
export const lifecycle = { addEventListener, removeEventListener }