Skip to content

Commit

Permalink
fix: clean up exported code (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw authored Feb 9, 2023
1 parent c977b03 commit eee64a2
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions packages/nuxt-delay-hydration/src/template/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ interface Handler { c: () => void; p: Promise<string | Event> }
// not supported
if (!('requestIdleCallback' in w) || !('requestAnimationFrame' in w))
return new Promise(resolve => resolve('not supported'))
const eventListeners = (): Handler => {
function eventListeners(): Handler {
const c = new AbortController()
const p = new Promise<Event>((resolve) => {
const hydrateOnEvents = '<%= options.hydrateOnEvents %>'.split(',') as (keyof WindowEventMap)[]
const handler = (e: Event) => {
function handler(e: Event) {
hydrateOnEvents.forEach(e => w.removeEventListener(e, handler))
// hydrate on animation frame
requestAnimationFrame(() => resolve(e))
Expand All @@ -25,7 +25,7 @@ const eventListeners = (): Handler => {
return { c: () => c.abort(), p }
}

const idleListener = (): Handler => {
function idleListener(): Handler {
let id: number
const p = new Promise<string>((resolve) => {
const isMobile = w.innerWidth < 640
Expand All @@ -39,10 +39,7 @@ const idleListener = (): Handler => {
return { c: () => window.cancelIdleCallback(id), p }
}
// return a promise which will never resolve if there is no hydration
const triggers = [
idleListener(),
eventListeners(),
]
const triggers = [idleListener(), eventListeners()];
const hydrationPromise = Promise.race<string | Event>(
triggers.map(t => t.p),
).finally(() => {
Expand Down

0 comments on commit eee64a2

Please sign in to comment.