UNPKG

@amityco/ts-sdk-react-native

Version:

Amity Social Cloud Typescript SDK

45 lines (38 loc) 1.23 kB
import { getActiveClient } from '~/client/api/activeClient'; import { createEventSubscriber } from '~/core/events'; type CallbackFn = ( channel: Amity.StaticInternalChannel, member: Amity.Membership<'channel'>, ) => void; const callbacks: CallbackFn[] = []; let mainDisposer: (() => void) | null = null; const dispose = (cb: CallbackFn) => { const index = callbacks.indexOf(cb); if (index > -1) { callbacks.splice(index, 1); } if (callbacks.length === 0) { mainDisposer?.(); } }; export const onChannelMemberRoleAdded = ( callback: (channel: Amity.StaticInternalChannel, member: Amity.Membership<'channel'>) => void, ): Amity.Unsubscriber => { if (callbacks.length === 0) { const client = getActiveClient(); const filter = async (payload: Amity.ProcessedChannelPayload) => { const { channels, channelUsers } = payload; callbacks.forEach(cb => cb(channels[0], channelUsers.find(channelUser => channelUser.membership === 'member')!), ); }; mainDisposer = createEventSubscriber( client, 'onChannelMemberRoleAdded', 'local.channel-moderator.role-added', filter, ); } callbacks.push(callback); return () => dispose(callback); };