Skip to content

Async suggestions #757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 7, 2020
Merged

Conversation

Chrissi2812
Copy link
Contributor

This allows to use async functions for items and onFilter.

Keep in mind first the items will get loaded an then the onFilter will run over them.
So maybe use an empty items array and query your backend in the onFilter function.

Little Example:

{
    items: [], 
    onFilter: async (_items, query) => {
        return await fetch(`https://yourapi.com/${query}`);
    }
}  

Possible Breaking Change

onFilter and items function are not called onExit.
Otherwise the onExit callback would be delayed until both other functions are finished.

Fix #755

Verified

This commit was signed with the committer’s verified signature.
Chrissi2812 Christoph

Verified

This commit was signed with the committer’s verified signature.
Chrissi2812 Christoph

Verified

This commit was signed with the committer’s verified signature.
Chrissi2812 Christoph

Verified

This commit was signed with the committer’s verified signature.
Chrissi2812 Christoph
@Chrissi2812
Copy link
Contributor Author

I had to change the sprite loader because it didn't worked after enabling @babel/plugin-transform-runtime for the promises

@samjarmakani
Copy link

Hi, is this being merged soon? I'm trying to fetch suggestions from an API endpoint like so:

onFilter: (items, query) => {
    if (!query) {
        return items
    }

    return new Promise((resolve, reject) => {
        axios.get('/api/users/at/?name='+query)
        .then(res => {
            resolve(res.data)
        })
    })
}

But it doesn't resolve and instead just returns a Promise. I'm assuming async will solve this?

@hanspagel
Copy link
Contributor

We do our best to get through the backlog of PRs and issues in all related repositories, but give us some time, especially for things that lead to breaking changes. 😬

@samjarmakani
Copy link

We do our best to get through the backlog of PRs and issues in all related repositories, but give us some time, especially for things that lead to breaking changes. 😬

Oh of course, I totally understand. Please take your time.

Thank you for the response!

@samjarmakani
Copy link

Hi, any update? :)

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@philippkuehn philippkuehn merged commit e9af3b9 into ueberdosis:master Aug 7, 2020
@hanspagel
Copy link
Contributor

Thanks @Chrissi2812! 🙌

@samjarmakani
Copy link

Would we be able to get a working example of async data?

I've left the items array empty and am fetching the users from an API endpoint:

items: [],
onFilter: async (_items, query) => {
   return await fetch(`/api/users/at/?name=${query}`);
}

The data being returned from the endpoint is in the exact same format as the examples, but it doesn't get loaded into the popup. It just says "No users found".

@samjarmakani
Copy link

Solved using this code in the onFilter method:

onFilter: async (items, query) => {
                                return await new Promise((resolve) => {
                                    axios.get('/api/users/at/?name='+query)
                                    .then(res => {
                                        resolve(res.data)
                                    })
                                })
                            }

@dadigu
Copy link

dadigu commented Aug 18, 2020

I have a quick question regarding this feature, How would you go about debouncing the request? Since a request is made on every query change, I'd like to debounce it for at least 500ms so the user isn't spamming the api while typing.

Thanks

@hanspagel
Copy link
Contributor

Try to wrap it in an async debounce function: https://www.npmjs.com/package/debounce-async

Hope that helps!

@dadigu
Copy link

dadigu commented Aug 18, 2020

Sweet! worked like a charm, Thanks!

new Mention({
  ...
  onFilter: debounce(this.onFilter.bind(this), 500),
  ...
})

@Chrissi2812 Chrissi2812 deleted the async-suggestions branch August 21, 2020 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Async suggestions
5 participants