Skip to content

Commit

Permalink
refactor: change the Map loop to forEach (#200)
Browse files Browse the repository at this point in the history
* refactor: change the Map loop to forEach

Using for...of created a lot of babel bloat, while requiring Symbol.iterator to be defined.

* Sync with Prettier
  • Loading branch information
thebuilder authored Mar 14, 2019
1 parent 466cd77 commit 831ca3a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
5 changes: 2 additions & 3 deletions src/intersection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,17 @@ export function unobserve(element: Element | null) {
let rootObserved = false
/* istanbul ignore else */
if (observerId) {
for (let [key, item] of INSTANCE_MAP) {
INSTANCE_MAP.forEach((item, key) => {
if (key !== element) {
if (item.observerId === observerId) {
itemsLeft = true
rootObserved = true
break
}
if (item.observer.root === root) {
rootObserved = true
}
}
}
})
}
if (!rootObserved && root) ROOT_IDS.delete(root)
if (observer && !itemsLeft) {
Expand Down
51 changes: 27 additions & 24 deletions src/useInView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,35 @@ export function useInView(options: IntersectionOptions = {}): HookResponse {
entry: undefined,
})

React.useEffect(() => {
if (!ref) return
observe(
ref,
(inView, intersection) => {
setState({ inView, entry: intersection })
React.useEffect(
() => {
if (!ref) return
observe(
ref,
(inView, intersection) => {
setState({ inView, entry: intersection })

if (inView && options.triggerOnce) {
// If it should only trigger once, unobserve the element after it's inView
unobserve(ref)
}
},
options,
)
if (inView && options.triggerOnce) {
// If it should only trigger once, unobserve the element after it's inView
unobserve(ref)
}
},
options,
)

return () => {
unobserve(ref)
}
}, [
// Only create a new Observer instance if the ref or any of the options have been changed.
ref,
options.threshold,
options.root,
options.rootMargin,
options.triggerOnce,
])
return () => {
unobserve(ref)
}
},
[
// Only create a new Observer instance if the ref or any of the options have been changed.
ref,
options.threshold,
options.root,
options.rootMargin,
options.triggerOnce,
],
)

React.useDebugValue(state.inView)

Expand Down

0 comments on commit 831ca3a

Please sign in to comment.