Clearing custom emojis from index and pool when necessary #172
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
👋
Problem & how to reproduce:
File
emoji-index.js
has four global variables, that will mutate whenever you search an emoji with the search input:Considering you have Emoji Pickers in different places (different communities), where the custom emojis that are provided to those pickers vary, if the index nor the pool get "contaminated" with the custom emojis of one Picker, every other will contain them in their search results.
Solution provided
_The best approach would probably have been to re-factor those four global variables to bind them to the Emoji Picker instead of having them be globally instantiated. However, the
emoji-index.js
util exports emojis and emoticons, therefore in order to avoid any further regression, I approached the issue differently to work on a patch rather than a refactor ~_Instead of calling
addCustomToPool
at every search call triggered, I store the previously provided custom emojis and store them in a list, that I compare the custom emojis provided along the search with, to define whether we need to feed the pool (and the emojisList) with those. In addition to that, whenever there's a change identified between the previous, and the new custom emojis, I remove the entries of old custom emojis, and clear the indexes, which in the case of a new custom prop provided, is a reliable (and possibly the least expensive) way to clear the indexed results and pools.I know this is a pretty specific fix for projects that are using emoji pickers in different contexts, sorry about that! 🙏
Edit: I reproduced the faulty behaviour in a sandbox so you can witness it: https://codesandbox.io/s/20wwk5nj0r