Created
March 5, 2020 13:33
-
-
Save sueLan/0a27674e419bf36565c8bc3e3bba3499 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_onUpdateSync( | |
viewableIndicesToCheck, | |
onViewableItemsChanged, | |
createViewToken, | |
) { | |
// Filter out indices that have gone out of view since this call was scheduled. | |
viewableIndicesToCheck = viewableIndicesToCheck.filter(ii => | |
this._viewableIndices.includes(ii), | |
); | |
const prevItems = this._viewableItems; | |
// Using map, so the time complexity would be o(n) | |
const nextItems = new Map( | |
viewableIndicesToCheck.map(ii => { | |
const viewable = createViewToken(ii, true); | |
return [viewable.key, viewable]; | |
}), | |
); | |
const changed = []; | |
for (const [key, viewable] of nextItems) { | |
if (!prevItems.has(key)) { | |
changed.push(viewable); | |
} | |
} | |
for (const [key, viewable] of prevItems) { | |
if (!nextItems.has(key)) { | |
changed.push({...viewable, isViewable: false}); | |
} | |
} | |
if (changed.length > 0) { | |
this._viewableItems = nextItems; | |
onViewableItemsChanged({ | |
viewableItems: Array.from(nextItems.values()), | |
changed, | |
viewabilityConfig: this._config, | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment