diff --git a/src/index.tsx b/src/index.tsx index 3c24bf2d..dd6bb846 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -2,7 +2,7 @@ import * as React from 'react'; import { InView } from './InView'; export { InView } from './InView'; export { useInView } from './useInView'; -export { observe } from './observe'; +export { observe, _observerMap } from './observe'; export default InView; @@ -13,15 +13,6 @@ export type ObserverInstanceCallback = ( entry: IntersectionObserverEntry, ) => void; -export type ObserverInstance = { - inView: boolean; - readonly callback: ObserverInstanceCallback; - readonly element: Element; - readonly observerId: string; - readonly observer: IntersectionObserver; - readonly thresholds: ReadonlyArray; -}; - interface RenderProps { inView: boolean; entry: IntersectionObserverEntry | undefined; diff --git a/src/observe.ts b/src/observe.ts index 3461c6bc..aec2d6f6 100644 --- a/src/observe.ts +++ b/src/observe.ts @@ -1,6 +1,6 @@ import { ObserverInstanceCallback } from './index'; -export const ObserverMap = new Map< +export const _observerMap = new Map< string, { id: string; @@ -44,7 +44,7 @@ export function optionsToId(options: IntersectionObserverInit) { function createObserver(options: IntersectionObserverInit) { // Create a unique ID for this observer instance, based on the root, root margin and threshold. let id = optionsToId(options); - let instance = ObserverMap.get(id); + let instance = _observerMap.get(id); if (!instance) { // Create a map of elements this observer is going to observe. Each element has a list of callbacks that should be triggered, once it comes into view. @@ -85,7 +85,7 @@ function createObserver(options: IntersectionObserverInit) { elements, }; - ObserverMap.set(id, instance); + _observerMap.set(id, instance); } return instance; @@ -128,7 +128,7 @@ export function observe( if (elements.size === 0) { // No more elements are being observer by this instance, so destroy it observer.disconnect(); - ObserverMap.delete(id); + _observerMap.delete(id); } }; } diff --git a/src/test-utils.ts b/src/test-utils.ts index 4c82b41a..bc1d2955 100644 --- a/src/test-utils.ts +++ b/src/test-utils.ts @@ -1,5 +1,5 @@ import { act } from 'react-dom/test-utils'; -import { ObserverMap } from './observe'; +import { _observerMap } from './index'; type Item = { callback: IntersectionObserverCallback; @@ -49,7 +49,7 @@ afterEach(() => { // @ts-ignore global.IntersectionObserver.mockClear(); observers.clear(); - ObserverMap.clear(); + _observerMap.clear(); }); function triggerIntersection(