-
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.
🏷♻️ improved types, refactored hooks for simplicity
- Loading branch information
Harley Alexander
committed
Feb 18, 2020
1 parent
876cc4c
commit 0d77035
Showing
10 changed files
with
133 additions
and
146 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,3 @@ | ||
{ | ||
"siteId": "aa919ed6-b843-411b-b6f7-828ea279b040" | ||
} |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,23 +1,16 @@ | ||
import { Options, AuthOptions } from 'pusher-js'; | ||
import Pusher, { Options } from 'pusher-js'; | ||
import * as React from 'react'; | ||
|
||
export interface PusherContextValues { | ||
client?: any | undefined; | ||
client?: React.MutableRefObject<Pusher | undefined>; | ||
triggerEndpoint?: string; | ||
} | ||
|
||
export interface PusherProviderProps extends Options { | ||
clientKey: string; | ||
cluster: string; | ||
authEndpoint?: string; | ||
auth?: AuthOptions; | ||
cluster: 'mt1' | 'us2' | 'us3' | 'eu' | 'ap1' | 'ap2' | 'ap3' | 'ap4'; | ||
triggerEndpoint?: string; | ||
defer?: boolean; | ||
children: React.ReactNode; | ||
// for testing purposes | ||
value?: any; | ||
} | ||
|
||
export interface useChannelOptions { | ||
skip?: boolean; | ||
value?: PusherContextValues; | ||
} |
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 |
---|---|---|
@@ -1,28 +1,36 @@ | ||
import { useEffect, useState } from 'react'; | ||
import invariant from 'invariant'; | ||
|
||
import { Channel, PresenceChannel } from 'pusher-js'; | ||
import { usePusher } from './usePusher'; | ||
|
||
/** | ||
* Subscribe to channel | ||
* Subscribe to a channel | ||
* | ||
* @param channelName The name of the channel you want to subscribe to. | ||
* @typeparam Type of channel you're subscribing to. Can be one of Channel or PresenceChannel. | ||
* @returns Instance of the channel you just subscribed to. | ||
* | ||
* @example | ||
* useChannel("my-channel") | ||
* ```javascript | ||
* const channel = useChannel("my-channel") | ||
* channel.bind('some-event', () => {}) | ||
* ``` | ||
*/ | ||
|
||
export function useChannel(channelName: string) { | ||
export function useChannel<T extends Channel & PresenceChannel>(channelName: string) { | ||
// errors for missing arguments | ||
invariant(channelName, 'channelName required to subscribe to a channel'); | ||
|
||
const { client } = usePusher(); | ||
const pusherClient = client && client.current; | ||
|
||
const [channel, setChannel] = useState<any>(); | ||
const [channel, setChannel] = useState<T | undefined>(); | ||
|
||
useEffect(() => { | ||
if (!pusherClient) return; | ||
const channel = pusherClient.subscribe(channelName); | ||
setChannel(channel); | ||
const pusherChannel = pusherClient.subscribe(channelName); | ||
setChannel(pusherChannel as T); | ||
}, [channelName, pusherClient]); | ||
return channel; | ||
} |
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
Oops, something went wrong.