-
Similar to: This would be especially useful with libraries like rxfire This ties into web socket (or similar), server subscriptions. If you're not convinced consider the popularity of rxjs. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 3 replies
-
How would you use SWR with Observables? Do you want support in the fetcher or as a way to get the data? |
Beta Was this translation helpful? Give feedback.
-
I've been thinking about this too. (but I don't have a clear way in mind to do that). 🤔 const { data } = useSWRSubscription('key', (onData, onError) => {
const starCountRef = firebase.database().ref('posts/1/starCount')
starCountRef.on('value', function(snapshot) {
onData(snapshot.val())
})
return () => starCountRef.off()
} But there're a lot of challenges, and it doesn't seem to have any benefits of |
Beta Was this translation helpful? Give feedback.
-
Maybe an API like that should be used together with useSWR, as a way to mutate the cache and not to get the data directly, this way you can subscribe to WebSockets or GraphQL subscriptions there and get new values, but you still get the normal SWR features like in focus revalidation. |
Beta Was this translation helpful? Give feedback.
-
What do you think of an API like this one: const { data } = useSWR("/api/messages", fetcher, {
subscribe(key, mutate) {
const unsubscribe = ws.subscribe(key, (newMessage) =>
mutate((currentMessages) => currentMessage.concat(newMessage))
);
return unsubscribe;
},
}); The new option |
Beta Was this translation helpful? Give feedback.
-
We had few concerns about the usage of subscription and wann collect more feedbacks from community about the scenarios of using SWR with observable cases. so it's not landed yet. We wanna make it decouple from swr core but covering more observable/subscription patterns. Theres's a subscription RFC in the discssuion. Feel free to post the use cases you need or what you have today and want to work with swr later. |
Beta Was this translation helpful? Give feedback.
What do you think of an API like this one:
The new option
subscribe
is a function receiving the key and the bound mutate, here it starts the subscription and update the cache when there is a new value, and it returns a function to unsubscribe when the component unmount.