Debian Codegraph @

packages/labs/ssr-dom-shim/src/lib/observers.ts

raw
1 /**
2 * @license
3 * Copyright 2024 Google LLC
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 /**
8 * This is a limited implemenation of the observer family of classes.
9 * They are essentially no-op implementations that allow code that uses them to
10 * run without error, but they do not actually perform any observation. This
11 * works in SSR where the observed changes would never occur.
12 */
13
14 type MutationObserverInterface = MutationObserver;
15
16 /**
17 * @see https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
18 */
19 const MutationObserverShim = class MutationObserver
20 implements MutationObserverInterface
21 {
22 constructor(_callback: MutationCallback) {}
23 disconnect(): void {}
24 observe(_target: Node, _options?: MutationObserverInit): void {}
25 takeRecords(): MutationRecord[] {
26 return [];
27 }
28 };
29 const MutationObserverShimWithRealType =
30 MutationObserverShim as object as typeof MutationObserver;
31 export {MutationObserverShimWithRealType as MutationObserver};
32
33 type ResizeObserverInterface = ResizeObserver;
34
35 /**
36 * @see https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver
37 */
38 const ResizeObserverShim = class ResizeObserver
39 implements ResizeObserverInterface
40 {
41 constructor(_callback: ResizeObserverCallback) {}
42 disconnect(): void {}
43 observe(_target: Element, _options?: ResizeObserverOptions): void {}
44 unobserve(_target: Element): void {}
45 };
46 const ResizeObserverShimWithRealType =
47 ResizeObserverShim as object as typeof ResizeObserver;
48 export {ResizeObserverShimWithRealType as ResizeObserver};
49
50 type IntersectionObserverInterface = IntersectionObserver;
51
52 /**
53 * @see https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver
54 */
55 const IntersectionObserverShim = class IntersectionObserver
56 implements IntersectionObserverInterface
57 {
58 constructor(
59 _callback: IntersectionObserverCallback,
60 private __options?: IntersectionObserverInit
61 ) {}
62 get root(): Element | Document | null {
63 return this.__options?.root ?? null;
64 }
65 get rootMargin(): string {
66 return this.__options?.rootMargin ?? '0px 0px 0px 0px';
67 }
68 get thresholds(): readonly number[] {
69 return Array.isArray(this.__options?.threshold)
70 ? this.__options.threshold
71 : [this.__options?.threshold ?? 0];
72 }
73 disconnect(): void {}
74 observe(_target: Element): void {}
75 takeRecords(): IntersectionObserverEntry[] {
76 return [];
77 }
78 unobserve(_target: Element): void {}
79 };
80 const IntersectionObserverShimWithRealType =
81 IntersectionObserverShim as object as typeof IntersectionObserver;
82 export {IntersectionObserverShimWithRealType as IntersectionObserver};