generated from stagas/ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
observe.ts
57 lines (53 loc) · 1.84 KB
/
observe.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { bool, toFluent } from 'to-fluent'
export class MutationObserverSettings implements MutationObserverInit {
attributeFilter?: string[]
attributeOldValue = bool
attributes = bool
characterData = bool
characterDataOldValue = bool
childList = bool
subtree = bool
/** Fire the observer callback initially with no mutations. */
initial = bool
}
export class ResizeObserverSettings implements ResizeObserverOptions {
box?: ResizeObserverBoxOptions
/** Fire the observer callback initially with no mutations. */
initial = bool
}
export class IntersectionObserverSettings {
root?: Element | Document | null;
rootMargin?: string;
threshold?: number | number[];
observer?: IntersectionObserver
}
export const observe = {
resize: toFluent(ResizeObserverSettings, settings =>
(el: Element, fn: ResizeObserverCallback) => {
const observer = new ResizeObserver(fn)
observer.observe(el, settings)
if (settings.initial) fn([], observer)
return () => observer.disconnect()
}),
intersection: toFluent(IntersectionObserverSettings, settings =>
(el: Element, fn: IntersectionObserverCallback) => {
const observer = settings.observer ?? new IntersectionObserver(fn, settings)
observer.observe(el)
return Object.assign(() => {
if (settings.observer) observer.unobserve(el)
else observer.disconnect()
}, { observer })
}),
mutation: toFluent(MutationObserverSettings, settings =>
(el: Element | ShadowRoot, fn: MutationCallback) => {
const observer = new MutationObserver(fn)
observer.observe(el, settings)
if (settings.initial) fn([], observer)
return () => observer.disconnect()
}),
gc: <T>(item: object, value: T, fn: (heldValue: T) => void) => {
const reg = new FinalizationRegistry(fn)
reg.register(item, value)
return reg
},
}