-
Notifications
You must be signed in to change notification settings - Fork 3
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
refactor: improve subscription api #3
Conversation
30951d0
to
c4dab09
Compare
goTo
utilitygoTo
utility and improve subscription api
c4dab09
to
5d7c6af
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good overall. added some minor comments.
export function proxyWithHistory<V>(initialValue: V, skipSubscribe = false) { | ||
export function proxyWithHistory<V>( | ||
initialValue: V, | ||
skipSubscribeOrCallback: SkipSubscribeOrCallback = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may consider making the second argument option object for future extension.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup true
*/ | ||
history: ref<History<V>>({ | ||
wip: undefined, // to avoid infinite loop | ||
nodes: [], | ||
index: -1, | ||
unsubscribe: () => {}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if having unsubscribe here is a reasonable abstraction. I'd prefer paring subscribe
and unsubscribe
. I don't have a good idea, because it subscribe behind the scene, but if we have unsubscribe
here, we would feel like supporting more edge cases like subscribing twice, unsubscribing twice and so on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i actually tried this but i would have to wrap unsubscribe with ref as well which didnt work out for me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is because it is assigned after subscribe and artificially changes the history nodes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer either of these patterns:
const unsubscribe = foo.subscribe(...);
const unsubscribe = subscribe(foo, ...);
subscribe(foo, ...); unsubscribe(foo, ...);
in our case, the last one would be possible. but at the same time, it wouldn't look very nice.
back up a little bit, what's the motivation of unsubscribe in this case, and what are the use cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mainly added the unsubscribe method as a way to do cleanup for eg. if a component in react unmounts an d the subscription is no longer needed.
if this is not needed then i guess we can remove it for now.
if it is needed then maybe we move both subscribe and unsubscribe into history
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One solution I could think of without changing the code is:
- note clarify that the default behavior is not able to clean up.
- if one needs to unsubscribe, they should use
skipSubscribe=true
and manuallysubscribe()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i actually tried this but i would have to wrap unsubscribe with ref as well which didnt work out for me
I think you can define unsubscribe
method, and internally call a function in the history ref. That seems acceptable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
K I will stash this change then and split the pr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if one needs to unsubscribe, they should use skipSubscribe=true and manually subscribe().
Will go with this for now.
in the next PR (#5 ), I however, extracted the utility to check if the history save should happen. this should reduce the amount of functionality to copy/rewrite for the consumer
5d7c6af
to
dd89412
Compare
goTo
utility and improve subscription api
closing for now |
goTo
utilityhistory.unsubscribe
handler for cleanupproxyWithHistory
to be optionally a callback