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

[WIP] Subscribe mode #913

Closed
wants to merge 6 commits into from
Closed

[WIP] Subscribe mode #913

wants to merge 6 commits into from

Conversation

huozhi
Copy link
Member

@huozhi huozhi commented Jan 20, 2021

Draft solution to #728
To support subscription / disposable / observable data source, adding a new hook useSWRSubscription built on top of useSWR.

API

useSWRSupscrition(key, subscribeFn: SWRSubscription, config?)
type SWRSubscription<Data> = (
  onData: (data: Data) => void,
  onError: (err: any) => void,
): () => void

the purpose is to provider users enough flexibility to manage their own subscription/disposable on user land, but still keep the argument shape different from function type fetcher.

Usage

const {data, error} = useSWRSubscription(key, (onData, onError) {
    const dispose = remoteDataSource.subscribe(onData, onError)
    return () => dispose()
 })

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 20, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 48cb5c2:

Sandbox Source
SWR-Basic Configuration
SWR-States Configuration
SWR-Infinite Configuration

@huozhi huozhi linked an issue Jan 26, 2021 that may be closed by this pull request
@huozhi huozhi marked this pull request as ready for review January 26, 2021 06:12
@huozhi huozhi changed the title feature: subscribe mode Subscribe mode Mar 17, 2021
@huozhi huozhi removed the request for review from Timer March 17, 2021 05:11
@followbl
Copy link

would be awesome to get this in - we'd use it right away!

@huozhi huozhi changed the title Subscribe mode [WIP] Subscribe mode May 13, 2021
@iamsaurabhgupt
Copy link

@huozhi @shuding @pacocoursey how far is this from getting into production?

@huozhi
Copy link
Member Author

huozhi commented Jul 2, 2021

Close this to file another one with middleware implementation

@huozhi huozhi closed this Jul 2, 2021
@huozhi huozhi deleted the subscribe branch July 2, 2021 04:50
@souljorje
Copy link

@huozhi hey! any updates about it?

@huozhi
Copy link
Member Author

huozhi commented Jan 20, 2023

The implementation is moved to #1263

@vercel vercel locked as resolved and limited conversation to collaborators Jan 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC] Subscription mode for SWR
4 participants