Replies: 9 comments 3 replies
-
Thank you for writing this amazing RFC!
This doesn't solve the data problem right? Like how do I notify SWR that I got the data (or error).
I think we can probably also return a
I'm 👍 on doing it. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the quick feedback, I updated the draft, providing some callbacks as second args - function observeWebsocket(key) {
+ function observeWebsocket(key, callbacks) {
const ws = new WebSocket(key)
ws.onmessage = (event) => {
- return event.data
+ callbacks.onSuccess(event.data)
}
+ ws.onerror = (error) => {
+ callbacks.onError(event.data)
+ }
return () => ws.close()
}
I guess it could be |
Beta Was this translation helpful? Give feedback.
-
@huozhi or a general Regards useSWRObservable([1, 2, 3], (onData, onError, onStateUpdate, 1, 2, 3) => {
// ...
}) Hmmm I think we still need to find something simpler... |
Beta Was this translation helpful? Give feedback.
-
yup, the basic event target like stuff and observable sounds like 2 scenarios, the first one is more simple. Event Target Like Subscriptions// event target (web defined)
eventTarget.addEventListener/removeEventListener(fn)
// event emitter
eventEmitter.on/off(fn)
// subscribable
close = subscribable.subscribe(fn)
close() Observablesubscription = observable.subscribe(onNext, onError, onComplete)
// or
subscription = observable.subscribe({
next() {...},
error() {...},
complete() {...},
}) and obserable also has some interesting standard from community like callbag so in that case, I feel we should either find a united way to let user encapsulate their observable to fit swr, or we make a very simple API contract with only few callbacks and let user handle the complexity themselves to keep swr lightweight. |
Beta Was this translation helpful? Give feedback.
-
just found use-subscription's API design for subscription case, to distinguish from observable case |
Beta Was this translation helpful? Give feedback.
-
Just come to mention that my PR (#457) come from a different use-case than the idea of the RFC. The My proposal to add a Imagine this flow, in your fetcher you do a So, I think we should support both, |
Beta Was this translation helpful? Give feedback.
-
Could you please shred some light on the overall direction you plan to move forward with? |
Beta Was this translation helpful? Give feedback.
-
I'm experimenting with a similar concept in a wrapper of SWR for Ethereum https://github.com/aboutlo/ether-swr
|
Beta Was this translation helpful? Give feedback.
-
agree with @sergiodxa that we'd better to support both. I'm trying to make the API more flexible in #1263 that we could let user do a little customization to fit the API. observable is definitely kind of heavy, so I wanna find the balance between them and try to get closer to subscription shape.
@aboutlo 👍 like the idea you build the customized subscription on top of core swr hook, awesome! |
Beta Was this translation helpful? Give feedback.
-
Define the Problem
currently
useSWR
anduseSWRInfinite
are more designed for endpoints or data sources that can directly access and patch proactively. The data flow is like belowBut for some cases SWR doesn't have enough solid ability to interact with data sources such as observable data (states returned from an observable stream), subscribable data (data been sent from a remote websocket endpoint).
then the
fetcher
of SWR will lose meaning since they're not really accessible initiavely from user end. we can surly do asubscribe
orobserve
call to attach on the source. however we cannot know the state of a certain time.besides
fetcher
, themutate
,trigger
andrevalidate
APIs are also kind f disabled due to the passive mode. I want to find a new way to think of the communication path.Solution
propose to bring a new hook API
useSWRSubscription
to SWR to fit in the subscribable situation. unlike the basic useSWR hook, it won't return the manipulation APIs of useSWR.API
Usage
then in this case, if the key changed, SWR is able to close and re-subscribe to the source for new key;
the uncertain thing is the returned error might not always be valid since some obseravle won't throw error when the connection get closed;
Other Possible Solutions
make revalidate and returned trigger with no effect. but mutate is still able to patch states to that key
Pros & Cons
Pros
Cons
PR: #1263
Beta Was this translation helpful? Give feedback.
All reactions