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

Subscription mode #1263

Merged
merged 31 commits into from
Feb 24, 2023
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
cbf8752
update subscribe with middleware
huozhi Jul 2, 2021
af89adb
fix lint & test
huozhi Jul 19, 2021
7283270
update typing, fix test and lint
huozhi Feb 1, 2022
bfa9056
refactor callback, rename, use refs for callbacks
huozhi Feb 3, 2022
510524a
Delay unmount
huozhi Feb 3, 2022
abfa159
Merge branch 'main' into subscribe
huozhi Feb 3, 2022
8ed9d72
Merge branch 'main' into subscribe
huozhi Apr 5, 2022
8f48cce
Merge branch 'main' into subscribe
huozhi Apr 12, 2022
8cfc3f5
no swr destruction and add getters, revert pkg.json
huozhi Apr 16, 2022
c023e89
callback -> next
huozhi Apr 17, 2022
2f33952
rename fix types and update test
huozhi Apr 17, 2022
138ede4
change exports to unstable_subscription
huozhi Apr 19, 2022
350f022
Merge branch 'main' into subscribe
huozhi May 17, 2022
7ad1935
manage subs with useESE
huozhi May 17, 2022
beaf868
use serialize
huozhi May 18, 2022
0e873eb
merge canary
huozhi Sep 22, 2022
81e2ab1
Merge branch 'main' into subscribe
huozhi Jan 23, 2023
4753152
fix lint
huozhi Jan 23, 2023
401738c
use cache helper to update error
huozhi Jan 24, 2023
6aed4ea
update exports path
huozhi Jan 26, 2023
ca2668b
fix script
huozhi Feb 1, 2023
c0ab66e
Merge branch 'main' into subscribe
huozhi Feb 1, 2023
8276703
pub check
huozhi Feb 1, 2023
c21aa98
add exp jsdoc
huozhi Feb 24, 2023
1c2b974
use cache-scoped storage
shuding Feb 24, 2023
5656fef
use prefixed key; add more tests
shuding Feb 24, 2023
12e3111
fix type
shuding Feb 24, 2023
0c04bfd
remove subscriber ref
shuding Feb 24, 2023
73ffb1c
rename
shuding Feb 24, 2023
7963ae8
fix lint
huozhi Feb 24, 2023
54dfaee
Merge branch 'main' into subscribe
huozhi Feb 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions subscription/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export const subscription = (<Data, Error>(useSWRNext: SWRHook) =>
new Map<string, () => void>()
])
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const [subscriptions, disposers] = subscriptionStorage.get(cache)!

huozhi marked this conversation as resolved.
Show resolved Hide resolved
useIsomorphicLayoutEffect(() => {
Expand Down Expand Up @@ -85,13 +87,15 @@ export const subscription = (<Data, Error>(useSWRNext: SWRHook) =>
// Prevent frequent unsubscribe caused by unmount
setTimeout(() => {
// TODO: Throw error during development if count is undefined.
const count = subscriptions.get(subscriptionKey)! - 1
const count = subscriptions.get(subscriptionKey)
if (count == null) return
huozhi marked this conversation as resolved.
Show resolved Hide resolved

subscriptions.set(subscriptionKey, count)
subscriptions.set(subscriptionKey, count - 1)

// Dispose if it's the last one.
if (!count) {
disposers.get(subscriptionKey)!()
if (count === 1) {
const dispose = disposers.get(subscriptionKey)
dispose?.()
}
})
}
Expand Down