Skip to content

Commit c01a0cf

Browse files
heygradyRendez
authored andcommitted
chore(Symbol): move storage above getPooled; replace for...of with while
1 parent e2e566c commit c01a0cf

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ Single React component or element that is used as the target (observable).
191191
* Changes happen asynchronously, similar to the way `requestIdleCallback` works.
192192
* Although you can consider callbacks immediate - always below 1 second - you can also get an immediate response on an
193193
element's visibility with `observer.takeRecords()`.
194-
* The primitives _Map_, _Set_, and _Symbol_ are required and won't be transpiled by default. Consider using a polyfill
194+
* The primitives `Map` an `Set` are required. You may need to include a polyfill
195195
for browsers lacking ES2015 support. If you're using babel, include `"babel-polyfill"` somewhere to your codebase.
196196

197197
## Polyfill

src/IntersectionObserverContainer.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { parseRootMargin, shallowCompareOptions } from './utils';
22

3+
export const storage = new Map();
4+
35
export function getPooled(options = {}) {
46
const root = options.root || null;
57
const rootMargin = parseRootMargin(options.rootMargin);
68
const threshold = Array.isArray(options.threshold)
79
? options.threshold
810
: [typeof options.threshold !== 'undefined' ? options.threshold : 0];
9-
// eslint-disable-next-line no-restricted-syntax
10-
for (const observer of storage.keys()) {
11+
const observers = storage.keys();
12+
let observer;
13+
while ((observer = observers.next().value)) {
1114
const unmatched = [
1215
[root, observer.root],
1316
[rootMargin, observer.rootMargin],
@@ -21,8 +24,6 @@ export function getPooled(options = {}) {
2124
return null;
2225
}
2326

24-
export const storage = new Map();
25-
2627
/**
2728
* If instances of a class can be reused because the options map,
2829
* we avoid creating instances of Intersection Observer by reusing them.
@@ -35,8 +36,9 @@ export default class IntersectionObserverContainer {
3536
static findElement(entry, observer) {
3637
const elements = storage.get(observer);
3738
if (elements) {
38-
// eslint-disable-next-line no-restricted-syntax
39-
for (const element of elements.values()) {
39+
const values = elements.values();
40+
let element;
41+
while ((element = values.next().value)) {
4042
if (element.target === entry.target) {
4143
return element;
4244
}

0 commit comments

Comments
 (0)