Skip to content
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

Closed
wants to merge 1 commit into from
Closed

Conversation

lwhiteley
Copy link
Collaborator

@lwhiteley lwhiteley commented Jan 8, 2024

  • add goTo utility
  • improve internal subscription api
    • allow to provide callback for simpler hooking
    • expose history.unsubscribe handler for cleanup
    • allow second param of proxyWithHistory to be optionally a callback

@lwhiteley lwhiteley requested a review from dai-shi January 8, 2024 05:34
@lwhiteley lwhiteley force-pushed the go-to-utility branch 8 times, most recently from 30951d0 to c4dab09 Compare January 8, 2024 08:12
@lwhiteley lwhiteley changed the title feat: add goTo utility feat: add goTo utility and improve subscription api Jan 8, 2024
Copy link
Member

@dai-shi dai-shi left a 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
Copy link
Member

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.

Copy link
Collaborator Author

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: () => {},
Copy link
Member

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.

Copy link
Collaborator Author

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

Copy link
Collaborator Author

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

Copy link
Member

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?

Copy link
Collaborator Author

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

Copy link
Member

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 manually subscribe().

Copy link
Member

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.

Copy link
Collaborator Author

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

Copy link
Collaborator Author

@lwhiteley lwhiteley Jan 8, 2024

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

@lwhiteley lwhiteley changed the title feat: add goTo utility and improve subscription api refactor: improve subscription api Jan 8, 2024
@lwhiteley lwhiteley added the may consider later closed but still has consideration label Jan 8, 2024
@lwhiteley
Copy link
Collaborator Author

closing for now

@lwhiteley lwhiteley closed this Jan 8, 2024
@lwhiteley lwhiteley deleted the go-to-utility branch January 8, 2024 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
may consider later closed but still has consideration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants