-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
51 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1 @@ | ||
if (!('requestIdleCallback' in w) || !('requestAnimationFrame' in w)) | ||
return new Promise(resolve => resolve('not supported')) | ||
function eventListeners() { | ||
const c = new AbortController() | ||
const p = new Promise((resolve) => { | ||
const hydrateOnEvents = '<%= options.hydrateOnEvents %>'.split(',') | ||
function handler(e) { | ||
hydrateOnEvents.forEach(e => w.removeEventListener(e, handler)) | ||
requestAnimationFrame(() => resolve(e)) | ||
} | ||
hydrateOnEvents.forEach((e) => { | ||
w.addEventListener(e, handler, { | ||
capture: true, | ||
once: true, | ||
passive: true, | ||
signal: c.signal, | ||
}) | ||
}) | ||
}) | ||
|
||
return { c: () => c.abort(), p } | ||
} | ||
|
||
function idleListener() { | ||
let id | ||
const p = new Promise((resolve) => { | ||
const isMobile = w.innerWidth < 640 | ||
const timeout = isMobile ? Number.parseInt('<%= options.postIdleTimeout.mobile %>') : Number.parseInt('<%= options.postIdleTimeout.desktop %>') | ||
const timeoutDelay = () => setTimeout( | ||
() => requestAnimationFrame(() => resolve('timeout')), | ||
timeout, | ||
) | ||
id = w.requestIdleCallback(timeoutDelay, { timeout: Number.parseInt('<%= options.idleCallbackTimeout %>') }) | ||
}) | ||
return { c: () => window.cancelIdleCallback(id), p } | ||
} | ||
const triggers = [idleListener(), eventListeners()] | ||
const hydrationPromise = Promise.race( | ||
triggers.map(t => t.p), | ||
).finally(() => { | ||
triggers.forEach(t => t.c()) | ||
}) | ||
|
||
return hydrationPromise | ||
function eventListeners() { const c = new AbortController(); const p = new Promise((resolve) => { const hydrateOnEvents = '<%= options.hydrateOnEvents %>'.split(','); function handler(e) { hydrateOnEvents.forEach(e => w.removeEventListener(e, handler)); requestAnimationFrame(() => resolve(e)) }hydrateOnEvents.forEach((e) => { w.addEventListener(e, handler, { capture: true, once: true, passive: true, signal: c.signal }) }) }); return { c: () => c.abort(), p } } function idleListener() { let id; const p = new Promise((resolve) => { const isMobile = w.innerWidth < 640; const timeout = isMobile ? Number.parseInt('<%= options.postIdleTimeout.mobile %>') : Number.parseInt('<%= options.postIdleTimeout.desktop %>'); const timeoutDelay = () => setTimeout(() => requestAnimationFrame(() => resolve('timeout')), timeout); id = w.requestIdleCallback(timeoutDelay, { timeout: Number.parseInt('<%= options.idleCallbackTimeout %>') }) }); return { c: () => window.cancelIdleCallback(id), p } } const triggers = [idleListener(), eventListeners()]; const hydrationPromise = Promise.race(triggers.map(t => t.p)).finally(() => { triggers.forEach(t => t.c()) }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1 @@ | ||
(() => { | ||
w._$delayHydration.then((e) => { | ||
if (!(e instanceof PointerEvent) && !(e instanceof MouseEvent) && !(window.TouchEvent && e instanceof TouchEvent)) | ||
return | ||
|
||
if (e instanceof MouseEvent && e.type !== 'click') | ||
return | ||
setTimeout(() => | ||
w.requestIdleCallback( | ||
() => setTimeout(() => e.target?.click(), 500), | ||
), 50) | ||
}) | ||
})() | ||
(() => { w._$delayHydration.then((e) => { if (!(e instanceof PointerEvent) && !(e instanceof MouseEvent) && !(window.TouchEvent && e instanceof TouchEvent) || e instanceof MouseEvent && e.type !== 'click') { return }setTimeout(() => w.requestIdleCallback(() => setTimeout(() => e.target && e.target.click(), 500)), 50) }) })() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
function eventListeners() { | ||
const c = new AbortController() | ||
const p = new Promise((resolve) => { | ||
const hydrateOnEvents = '<%= options.hydrateOnEvents %>'.split(',') | ||
function handler(e) { | ||
hydrateOnEvents.forEach(e => w.removeEventListener(e, handler)) | ||
requestAnimationFrame(() => resolve(e)) | ||
} | ||
hydrateOnEvents.forEach((e) => { | ||
w.addEventListener(e, handler, { | ||
capture: true, | ||
once: true, | ||
passive: true, | ||
signal: c.signal, | ||
}) | ||
}) | ||
}) | ||
return { c: () => c.abort(), p } | ||
} | ||
|
||
function idleListener() { | ||
let id | ||
const p = new Promise((resolve) => { | ||
const isMobile = w.innerWidth < 640 | ||
const timeout = isMobile ? Number.parseInt('<%= options.postIdleTimeout.mobile %>') : Number.parseInt('<%= options.postIdleTimeout.desktop %>') | ||
const timeoutDelay = () => setTimeout( | ||
() => requestAnimationFrame(() => resolve('timeout')), | ||
timeout, | ||
) | ||
id = w.requestIdleCallback(timeoutDelay, { timeout: Number.parseInt('<%= options.idleCallbackTimeout %>') }) | ||
}) | ||
return { c: () => window.cancelIdleCallback(id), p } | ||
} | ||
const triggers = [idleListener(), eventListeners()] | ||
const hydrationPromise = Promise.race( | ||
triggers.map(t => t.p), | ||
).finally(() => { | ||
triggers.forEach(t => t.c()) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
(() => { | ||
w._$delayHydration.then((e) => { | ||
if ((!(e instanceof PointerEvent) && !(e instanceof MouseEvent) && !(window.TouchEvent && e instanceof TouchEvent)) || e instanceof MouseEvent && e.type !== 'click') { | ||
return | ||
} | ||
setTimeout(() => w.requestIdleCallback(() => setTimeout(() => e.target && e.target.click(), 500)), 50) | ||
}) | ||
})() |