UNPKG

@harelpls/use-pusher

Version:

A wrapper around pusher-js for easy-as hooks in React.

141 lines (136 loc) 5.41 kB
// Generated by dts-bundle-generator v5.3.0 /// <reference types="prop-types" /> /// <reference types="react" /> import { Channel, Options, PresenceChannel, default as Pusher } from 'pusher-js'; import * as React from 'react'; export interface PusherContextValues { // client?: React.MutableRefObject<Pusher | undefined>; client?: Pusher; triggerEndpoint?: string; } export interface PusherProviderProps extends Options { _PusherRuntime?: typeof Pusher; clientKey: string | undefined; cluster: "mt1" | "us2" | "us3" | "eu" | "ap1" | "ap2" | "ap3" | "ap4" | string | undefined; triggerEndpoint?: string; defer?: boolean; // for testing purposes value?: PusherContextValues; } /** * Provides access to the pusher client instance. * * @returns a `MutableRefObject<Pusher|undefined>`. The instance is held by a `useRef()` hook. * @example * ```javascript * const { client } = usePusher(); * client.current.subscribe('my-channel'); * ``` */ export declare function usePusher(): PusherContextValues; export declare const NOT_IN_CONTEXT_WARNING = "No Pusher context. Did you forget to wrap your app in a <PusherProvider />?"; /** * 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` from `pusher-js`. * @returns Instance of the channel you just subscribed to. * * @example * ```javascript * const channel = useChannel("my-channel") * channel.bind('some-event', () => {}) * ``` */ export declare const NO_CHANNEL_NAME_WARNING = "No channel name passed to useChannel. No channel has been subscribed to."; export declare function useChannel<T extends Channel & PresenceChannel>(channelName: string | undefined): T | undefined; /** * Subscribe to presence channel events and get members back * * @param channelName name of presence the channel. Should have `presence-` suffix. * @returns Object with `channel`, `members` and `myID` properties. * * @example * ```javascript * const { channel, members, myID } = usePresenceChannel("presence-my-channel"); * ``` */ /** Internal state */ export declare type PresenceChannelState = { members: Record<string, any>; me: Record<string, any> | undefined; myID: string | undefined; count: number; }; export declare type MemberAction = { id: string; info?: Record<string, any>; }; export declare type ReducerAction = { type: typeof SET_STATE | typeof ADD_MEMBER | typeof REMOVE_MEMBER; payload: Partial<PresenceChannelState> | MemberAction; }; /** Hook return value */ export interface usePresenceChannelValue extends Partial<PresenceChannelState> { channel?: PresenceChannel; } /** Presence channel reducer to keep track of state */ export declare const SET_STATE = "set-state"; export declare const ADD_MEMBER = "add-member"; export declare const REMOVE_MEMBER = "remove-member"; export declare const presenceChannelReducer: (state: PresenceChannelState, { type, payload }: ReducerAction) => { id: string; info?: Record<string, any> | undefined; members: Record<string, any>; me: Record<string, any> | undefined; myID: string | undefined; count: number; } | { members: Record<string, any>; me: Record<string, any> | undefined; myID: string | undefined; count: number; }; export declare function usePresenceChannel(channelName: string | undefined): usePresenceChannelValue; /** * Subscribes to a channel event and registers a callback. * @param channel Pusher channel to bind to * @param eventName Name of event to bind to * @param callback Callback to call on a new event */ export declare function useEvent<D>(channel: Channel | PresenceChannel | undefined, eventName: string, callback: (data?: D, metadata?: { user_id: string; }) => void): void; /** * * @param channel the channel you'd like to trigger clientEvents on. Get this from [[useChannel]] or [[usePresenceChannel]]. * @typeparam TData shape of the data you're sending with the event. * @returns A memoized trigger function that will perform client events on the channel. * @example * ```javascript * const channel = useChannel('my-channel'); * const trigger = useClientTrigger(channel) * * const handleClick = () => trigger('some-client-event', {}); * ``` */ export declare function useClientTrigger<TData = {}>(channel: Channel | PresenceChannel | undefined): (eventName: string, data: TData) => void; /** * Hook to provide a trigger function that calls the server defined in `PusherProviderProps.triggerEndpoint` using `fetch`. * Any `auth?.headers` in the config object will be passed with the `fetch` call. * * @param channelName name of channel to call trigger on * @typeparam TData shape of the data you're sending with the event * * @example * ```typescript * const trigger = useTrigger<{message: string}>('my-channel'); * trigger('my-event', {message: 'hello'}); * ``` */ export declare function useTrigger<TData = {}>(channelName: string): (eventName: string, data?: TData | undefined) => Promise<Response>; export declare const NO_AUTH_HEADERS_WARNING = "No auth parameters supplied to <PusherProvider />. Your events will be unauthenticated."; /** Wrapper around the core PusherProvider that passes in the Pusher lib */ export declare const PusherProvider: React.FC<PusherProviderProps>; export declare const __PusherContext: React.Context<PusherContextValues>; export {};