UNPKG

@jcm/use-pusher

Version:

Fork of harelpls/use-pusher with the latest changes on master

51 lines (50 loc) 1.68 kB
import { PresenceChannel } from "pusher-js"; /** * 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 */ type PresenceChannelState = { members: Record<string, any>; me: Record<string, any> | undefined; myID: string | undefined; count: number; }; type MemberAction = { id: string; info?: Record<string, any>; }; type ReducerAction = { type: typeof SET_STATE | typeof ADD_MEMBER | typeof REMOVE_MEMBER; payload: Partial<PresenceChannelState> | MemberAction; }; /** Hook return value */ 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; export {};