-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from semoal/improvements
Some improvements... #89 Incompatible with React versions >16 #40 is this hooks automatically disconnect channels? #43 Introduce to hoist channel connection #44 useChannel(false) to prevent eager channel connection
- Loading branch information
Showing
13 changed files
with
220 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: "🚀 CI Workflow" | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
workflow_call: {} | ||
pull_request: {} | ||
|
||
jobs: | ||
test: | ||
name: 🃏 Test | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: 🛑 Cancel Previous Runs | ||
uses: styfle/cancel-workflow-action@0.9.1 | ||
|
||
- name: ⬇️ Checkout repo | ||
uses: actions/checkout@v3 | ||
|
||
- name: ⎔ Setup node | ||
uses: actions/setup-node@v3 | ||
with: | ||
cache: "yarn" | ||
node-version: 16 | ||
|
||
- name: 📥 Download deps | ||
run: yarn install | ||
|
||
- name: 🃏 Test | ||
run: yarn test |
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,88 @@ | ||
import { Channel, PresenceChannel } from "pusher-js"; | ||
import React, { useCallback, useRef } from "react"; | ||
import { ChannelsContextValues } from "./types"; | ||
|
||
import { usePusher } from "./usePusher"; | ||
|
||
// context setup | ||
const ChannelsContext = React.createContext<ChannelsContextValues>({}); | ||
export const __ChannelsContext = ChannelsContext; | ||
|
||
type AcceptedChannels = Channel | PresenceChannel; | ||
type ConnectedChannels = { | ||
[channelName: string]: AcceptedChannels[]; | ||
}; | ||
|
||
/** | ||
* Provider that creates your channels instances and provides it to child hooks throughout your app. | ||
*/ | ||
|
||
export const ChannelsProvider: React.FC<{ children: React.ReactNode }> = ({ | ||
children, | ||
}) => { | ||
const { client } = usePusher(); | ||
const connectedChannels = useRef<ConnectedChannels>({}); | ||
|
||
const subscribe = useCallback( | ||
<T extends Channel & PresenceChannel>(channelName: string) => { | ||
/** Return early if there's no client */ | ||
if (!client || !channelName) return; | ||
|
||
/** Subscribe to channel and set it in state */ | ||
const pusherChannel = client.subscribe(channelName); | ||
connectedChannels.current[channelName] = [ | ||
...(connectedChannels.current[channelName] || []), | ||
pusherChannel, | ||
]; | ||
return pusherChannel as T; | ||
}, | ||
[client, connectedChannels] | ||
); | ||
|
||
const unsubscribe = useCallback( | ||
(channelName: string) => { | ||
/** Return early if there's no props */ | ||
if ( | ||
!client || | ||
!channelName || | ||
!(channelName in connectedChannels.current) | ||
) | ||
return; | ||
/** If just one connection, unsubscribe totally*/ | ||
if (connectedChannels.current[channelName].length === 1) { | ||
client.unsubscribe(channelName); | ||
delete connectedChannels.current[channelName]; | ||
} else { | ||
connectedChannels.current[channelName].pop(); | ||
} | ||
}, | ||
[connectedChannels, client] | ||
); | ||
|
||
const getChannel = useCallback( | ||
<T extends Channel & PresenceChannel>(channelName: string) => { | ||
/** Return early if there's no client */ | ||
if ( | ||
!client || | ||
!channelName || | ||
!(channelName in connectedChannels.current) | ||
) | ||
return; | ||
/** Return channel */ | ||
return connectedChannels.current[channelName][0] as T; | ||
}, | ||
[connectedChannels, client] | ||
); | ||
|
||
return ( | ||
<ChannelsContext.Provider | ||
value={{ | ||
unsubscribe, | ||
subscribe, | ||
getChannel, | ||
}} | ||
> | ||
{children} | ||
</ChannelsContext.Provider> | ||
); | ||
}; |
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,19 @@ | ||
import { useContext, useEffect } from "react"; | ||
import { __ChannelsContext } from "./ChannelsProvider"; | ||
import { ChannelsContextValues } from "./types"; | ||
|
||
/** | ||
* Provides access to the channels global provider. | ||
*/ | ||
|
||
export function useChannels() { | ||
const context = useContext<ChannelsContextValues>(__ChannelsContext); | ||
useEffect(() => { | ||
if (!context || !Object.keys(context).length) | ||
console.warn(NOT_IN_CONTEXT_WARNING); | ||
}, [context]); | ||
return context; | ||
} | ||
|
||
const NOT_IN_CONTEXT_WARNING = | ||
"No Channels context. Did you forget to wrap your app in a <ChannelsProvider />?"; |
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
Oops, something went wrong.