Skip to content

Commit

Permalink
fix: correct import path for observermap (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
thebuilder authored Nov 23, 2021
1 parent a6d11be commit d4ecf3a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 16 deletions.
11 changes: 1 addition & 10 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<number>;
};

interface RenderProps {
inView: boolean;
entry: IntersectionObserverEntry | undefined;
Expand Down
8 changes: 4 additions & 4 deletions src/observe.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ObserverInstanceCallback } from './index';

export const ObserverMap = new Map<
export const _observerMap = new Map<
string,
{
id: string;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -85,7 +85,7 @@ function createObserver(options: IntersectionObserverInit) {
elements,
};

ObserverMap.set(id, instance);
_observerMap.set(id, instance);
}

return instance;
Expand Down Expand Up @@ -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);
}
};
}
4 changes: 2 additions & 2 deletions src/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { act } from 'react-dom/test-utils';
import { ObserverMap } from './observe';
import { _observerMap } from './index';

type Item = {
callback: IntersectionObserverCallback;
Expand Down Expand Up @@ -49,7 +49,7 @@ afterEach(() => {
// @ts-ignore
global.IntersectionObserver.mockClear();
observers.clear();
ObserverMap.clear();
_observerMap.clear();
});

function triggerIntersection(
Expand Down

1 comment on commit d4ecf3a

@vercel
Copy link

@vercel vercel bot commented on d4ecf3a Nov 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.