|
| 1 | +import { isString } from '@vue/shared' |
| 2 | +import { DOMNodeTypes, isComment } from './hydration' |
| 3 | + |
| 4 | +/** |
| 5 | + * A lazy hydration strategy for async components. |
| 6 | + * @param hydrate - call this to perform the actual hydration. |
| 7 | + * @param forEachElement - iterate through the root elements of the component's |
| 8 | + * non-hydrated DOM, accounting for possible fragments. |
| 9 | + * @returns a teardown function to be called if the async component is unmounted |
| 10 | + * before it is hydrated. This can be used to e.g. remove DOM event |
| 11 | + * listeners. |
| 12 | + */ |
| 13 | +export type HydrationStrategy = ( |
| 14 | + hydrate: () => void, |
| 15 | + forEachElement: (cb: (el: Element) => any) => void, |
| 16 | +) => (() => void) | void |
| 17 | + |
| 18 | +export type HydrationStrategyFactory<Options = any> = ( |
| 19 | + options?: Options, |
| 20 | +) => HydrationStrategy |
| 21 | + |
| 22 | +export const hydrateOnIdle: HydrationStrategyFactory = () => hydrate => { |
| 23 | + const id = requestIdleCallback(hydrate) |
| 24 | + return () => cancelIdleCallback(id) |
| 25 | +} |
| 26 | + |
| 27 | +export const hydrateOnVisible: HydrationStrategyFactory<string | number> = |
| 28 | + (margin = 0) => |
| 29 | + (hydrate, forEach) => { |
| 30 | + const ob = new IntersectionObserver( |
| 31 | + entries => { |
| 32 | + for (const e of entries) { |
| 33 | + if (!e.isIntersecting) continue |
| 34 | + ob.disconnect() |
| 35 | + hydrate() |
| 36 | + break |
| 37 | + } |
| 38 | + }, |
| 39 | + { |
| 40 | + rootMargin: isString(margin) ? margin : margin + 'px', |
| 41 | + }, |
| 42 | + ) |
| 43 | + forEach(el => ob.observe(el)) |
| 44 | + return () => ob.disconnect() |
| 45 | + } |
| 46 | + |
| 47 | +export const hydrateOnMediaQuery: HydrationStrategyFactory<string> = |
| 48 | + query => hydrate => { |
| 49 | + if (query) { |
| 50 | + const mql = matchMedia(query) |
| 51 | + if (mql.matches) { |
| 52 | + hydrate() |
| 53 | + } else { |
| 54 | + mql.addEventListener('change', hydrate, { once: true }) |
| 55 | + return () => mql.removeEventListener('change', hydrate) |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | +export const hydrateOnInteraction: HydrationStrategyFactory< |
| 61 | + string | string[] |
| 62 | +> = |
| 63 | + (interactions = []) => |
| 64 | + (hydrate, forEach) => { |
| 65 | + if (isString(interactions)) interactions = [interactions] |
| 66 | + let hasHydrated = false |
| 67 | + const doHydrate = (e: Event) => { |
| 68 | + if (!hasHydrated) { |
| 69 | + hasHydrated = true |
| 70 | + teardown() |
| 71 | + hydrate() |
| 72 | + // replay event |
| 73 | + e.target!.dispatchEvent(new (e.constructor as any)(e.type, e)) |
| 74 | + } |
| 75 | + } |
| 76 | + const teardown = () => { |
| 77 | + forEach(el => { |
| 78 | + for (const i of interactions) { |
| 79 | + el.removeEventListener(i, doHydrate) |
| 80 | + } |
| 81 | + }) |
| 82 | + } |
| 83 | + forEach(el => { |
| 84 | + for (const i of interactions) { |
| 85 | + el.addEventListener(i, doHydrate, { once: true }) |
| 86 | + } |
| 87 | + }) |
| 88 | + return teardown |
| 89 | + } |
| 90 | + |
| 91 | +export function forEachElement(node: Node, cb: (el: Element) => void) { |
| 92 | + // fragment |
| 93 | + if (isComment(node) && node.data === '[') { |
| 94 | + let depth = 1 |
| 95 | + let next = node.nextSibling |
| 96 | + while (next) { |
| 97 | + if (next.nodeType === DOMNodeTypes.ELEMENT) { |
| 98 | + cb(next as Element) |
| 99 | + } else if (isComment(next)) { |
| 100 | + if (next.data === ']') { |
| 101 | + if (--depth === 0) break |
| 102 | + } else if (next.data === '[') { |
| 103 | + depth++ |
| 104 | + } |
| 105 | + } |
| 106 | + next = next.nextSibling |
| 107 | + } |
| 108 | + } else { |
| 109 | + cb(node as Element) |
| 110 | + } |
| 111 | +} |
0 commit comments