Skip to content

Commit 9428489

Browse files
committed
refactor(takeRecords): removed takeRecords as it didn't fit a good case
1 parent a51edb1 commit 9428489

File tree

4 files changed

+3
-79
lines changed

4 files changed

+3
-79
lines changed

README.md

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ npm install --save intersection-observer
6464

6565
#### Inside your codebase
6666
```jsx
67+
import React, { Component } from 'react';
6768
import 'intersection-observer'; // adding optional polyfill
6869
import Observer from '@researchgate/react-intersection-observer';
6970

70-
class Component extends React.Component {
71+
class TargetComponent extends Component {
7172
handleIntersection = (event) => {
7273
if (event.isIntersecting) {
7374
console.log('I am intersecting ');
@@ -80,7 +81,7 @@ class Component extends React.Component {
8081
const options = {
8182
onChange: this.handleIntersection
8283
root: "#scrolling-container"
83-
rootMargin: "0% 0% -25% 0%"
84+
rootMargin: "0% 0% -25%"
8485
};
8586

8687
return (
@@ -176,29 +177,6 @@ Type: `element|component`
176177

177178
Single React component or element that is used as the target to observe.
178179

179-
## Methods
180-
181-
### takeRecords(options?: IntersectionObserverOptions)
182-
183-
Calls all the observer instance's `takeRecords()` methods if no options are passed, and if options are passed triggers only those matching all of the options.
184-
185-
> The `IntersectionObserver` method `takeRecords()` returns an array of `IntersectionObserverEntry` objects, one for each targeted element which has experienced an intersection change since the last time the intersections were checked, either explicitly through a call to this method or implicitly by an automatic call to the observer's callback.
186-
>
187-
> [MDN: IntersectionObserver.takeRecords()](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/takeRecords)
188-
189-
#### [What are the use cases of `takeRecords`?](https://github.com/WICG/IntersectionObserver/issues/133#issuecomment-224368861)
190-
In cases where you need to synchronously get any `IntersectionObserverEntry` objects that have been generated but not yet delivered.
191-
192-
#### Usage
193-
```js
194-
import { takeRecords } from '@researchgate/react-intersection-observer';
195-
196-
takeRecords({
197-
root: document.getElementById('#scrolling-container'),
198-
rootMargin: '0% 0% -25% 0%',
199-
});
200-
```
201-
202180
## Polyfill
203181

204182
When needing the full spec's support, we highly recommend using the [IntersectionObserver polyfill](https://github.com/WICG/IntersectionObserver/tree/gh-pages/polyfill).

src/IntersectionObserverContainer.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,6 @@ export default class IntersectionObserverContainer {
4545
return null;
4646
}
4747

48-
static takeRecords(options) {
49-
if (options) {
50-
const observer = getPooled(options);
51-
if (observer) {
52-
observer.takeRecords();
53-
}
54-
} else {
55-
// eslint-disable-next-line no-restricted-syntax
56-
for (const observer of storage.keys()) {
57-
observer.takeRecords();
58-
}
59-
}
60-
}
61-
6248
static observe(element) {
6349
let targets;
6450
if (storage.has(element.observer)) {

src/index.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
import IntersectionObserverContainer from './IntersectionObserverContainer';
2-
31
export { default } from './IntersectionObserver';
42
export { parseRootMargin } from './utils';
5-
export const takeRecords = IntersectionObserverContainer.takeRecords;

test/IntersectionObserverContainer.test.js

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -179,40 +179,3 @@ describe('#findElement', () => {
179179
expect(instance2).toEqual(entry2);
180180
});
181181
});
182-
183-
describe('#takeRecords', () => {
184-
test('should get called once on the observer retrieved by matching options', () => {
185-
const options = { ...defaultOptions, threshold: 1 };
186-
const observer = IntersectionObserverContainer.create(noop, options);
187-
const element = { target: { nodeType: 1, id: 1 }, observer };
188-
IntersectionObserverContainer.observe(element);
189-
const spy = jest.spyOn(observer, 'takeRecords');
190-
IntersectionObserverContainer.takeRecords(options);
191-
IntersectionObserverContainer.takeRecords(defaultOptions);
192-
expect(spy).toHaveBeenCalledTimes(1);
193-
});
194-
195-
test('should get called once for all observers not having options argument', () => {
196-
const observer1 = IntersectionObserverContainer.create(noop, defaultOptions);
197-
const element1 = { target: { nodeType: 1, id: 1 }, observer: observer1 };
198-
IntersectionObserverContainer.observe(element1);
199-
const options = { ...defaultOptions, threshold: 1 };
200-
const observer2 = IntersectionObserverContainer.create(noop, options);
201-
const element2 = { target: { nodeType: 1, id: 1 }, observer: observer2 };
202-
IntersectionObserverContainer.observe(element2);
203-
const spy1 = jest.spyOn(observer1, 'takeRecords');
204-
const spy2 = jest.spyOn(observer2, 'takeRecords');
205-
IntersectionObserverContainer.takeRecords();
206-
expect(storage.size).toEqual(2);
207-
expect(spy1).toHaveBeenCalledTimes(1);
208-
expect(spy2).toHaveBeenCalledTimes(1);
209-
});
210-
211-
test('never gets called when storage holds no observers', () => {
212-
const observer = IntersectionObserverContainer.create(noop, defaultOptions);
213-
const spy = jest.spyOn(observer, 'takeRecords');
214-
IntersectionObserverContainer.takeRecords();
215-
IntersectionObserverContainer.takeRecords(defaultOptions);
216-
expect(spy).not.toBeCalled();
217-
});
218-
});

0 commit comments

Comments
 (0)