-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Necati Özmen <[email protected]>
- Loading branch information
1 parent
fbd781d
commit 91e4837
Showing
12 changed files
with
132 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./useSubscription"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { useContext, useEffect } from "react"; | ||
import { useQueryClient } from "react-query"; | ||
import { useCacheQueries } from "@hooks"; | ||
import { ILiveContext, LiveEvent } from "../../interfaces"; | ||
import { LiveContext } from "@contexts/live"; | ||
|
||
export type UseSubscriptionProps = { | ||
channel: string; | ||
resource: string; | ||
liveMode?: "immediate" | "controlled"; | ||
enabled?: boolean; | ||
onLiveEvent?: (event: LiveEvent) => void; | ||
}; | ||
|
||
export const useSubscription = ({ | ||
resource, | ||
channel, | ||
enabled = false, | ||
liveMode, | ||
onLiveEvent, | ||
}: UseSubscriptionProps): void => { | ||
const queryClient = useQueryClient(); | ||
const getAllQueries = useCacheQueries(); | ||
const liveDataContext = useContext<ILiveContext>(LiveContext); | ||
|
||
useEffect(() => { | ||
let subscription: any; | ||
|
||
if (liveMode && enabled) { | ||
subscription = liveDataContext?.subscribe({ | ||
channel, | ||
type: "*", | ||
callback: (event) => { | ||
if (liveMode === "immediate") { | ||
getAllQueries(resource).forEach((query) => { | ||
queryClient.invalidateQueries(query.queryKey); | ||
}); | ||
} else if (liveMode === "controlled") { | ||
onLiveEvent?.(event); | ||
} | ||
}, | ||
}); | ||
} | ||
|
||
console.log("subscribed", channel); | ||
|
||
return () => { | ||
if (subscription) { | ||
liveDataContext?.unsubscribe(subscription); | ||
console.log("unsubscribed", channel); | ||
} | ||
}; | ||
}, [enabled]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters