From ca0483250f6be5ee6612c8ab6202c364d738ddf6 Mon Sep 17 00:00:00 2001 From: Anton Arnautov Date: Wed, 12 Feb 2025 18:13:22 +0100 Subject: [PATCH] Post-merge fixes --- src/channel_manager.ts | 86 ++++++++++++++++++------------------------ src/client.ts | 6 +-- src/types.ts | 8 ++-- src/utils.ts | 50 ++++++++++++------------ 4 files changed, 68 insertions(+), 82 deletions(-) diff --git a/src/channel_manager.ts b/src/channel_manager.ts index b0de0b645a..4b3b2d63ee 100644 --- a/src/channel_manager.ts +++ b/src/channel_manager.ts @@ -1,13 +1,5 @@ import type { StreamChat } from './client'; -import type { - DefaultGenerics, - ExtendableGenerics, - Event, - ChannelOptions, - ChannelStateOptions, - ChannelFilters, - ChannelSort, -} from './types'; +import type { Event, ChannelOptions, ChannelStateOptions, ChannelFilters, ChannelSort } from './types'; import { StateStore, ValueOrPatch, isPatch } from './store'; import { Channel } from './channel'; import { @@ -21,17 +13,17 @@ import { shouldConsiderPinnedChannels, } from './utils'; -export type ChannelManagerPagination = { - filters: ChannelFilters; +export type ChannelManagerPagination = { + filters: ChannelFilters; hasNext: boolean; isLoading: boolean; isLoadingNext: boolean; options: ChannelOptions; - sort: ChannelSort; + sort: ChannelSort; }; -export type ChannelManagerState = { - channels: Channel[]; +export type ChannelManagerState = { + channels: Channel[]; /** * This value will become true the first time queryChannels is successfully executed and * will remain false otherwise. It's used as a control property regarding whether the list @@ -39,23 +31,17 @@ export type ChannelManagerState; + pagination: ChannelManagerPagination; }; -export type ChannelSetterParameterType = ValueOrPatch< - ChannelManagerState['channels'] ->; -export type ChannelSetterType = ( - arg: ChannelSetterParameterType, -) => void; +export type ChannelSetterParameterType = ValueOrPatch; +export type ChannelSetterType = (arg: ChannelSetterParameterType) => void; export type GenericEventHandlerType = ( ...args: T ) => void | (() => void) | ((...args: T) => Promise) | Promise; -export type EventHandlerType = GenericEventHandlerType<[Event]>; -export type EventHandlerOverrideType = GenericEventHandlerType< - [ChannelSetterType, Event] ->; +export type EventHandlerType = GenericEventHandlerType<[Event]>; +export type EventHandlerOverrideType = GenericEventHandlerType<[ChannelSetterType, Event]>; export type ChannelManagerEventTypes = | 'notification.added_to_channel' @@ -79,8 +65,8 @@ export type ChannelManagerEventHandlerNames = | 'notificationNewMessageHandler' | 'notificationRemovedFromChannelHandler'; -export type ChannelManagerEventHandlerOverrides = Partial< - Record> +export type ChannelManagerEventHandlerOverrides = Partial< + Record >; export const channelManagerEventToHandlerMapping: { @@ -143,12 +129,12 @@ export const DEFAULT_CHANNEL_MANAGER_PAGINATION_OPTIONS = { * * @internal */ -export class ChannelManager { - public readonly state: StateStore>; - private client: StreamChat; +export class ChannelManager { + public readonly state: StateStore; + private client: StreamChat; private unsubscribeFunctions: Set<() => void> = new Set(); - private eventHandlers: Map> = new Map(); - private eventHandlerOverrides: Map> = new Map(); + private eventHandlers: Map = new Map(); + private eventHandlerOverrides: Map = new Map(); private options: ChannelManagerOptions = {}; private stateOptions: ChannelStateOptions = {}; @@ -157,12 +143,12 @@ export class ChannelManager { eventHandlerOverrides = {}, options = {}, }: { - client: StreamChat; - eventHandlerOverrides?: ChannelManagerEventHandlerOverrides; + client: StreamChat; + eventHandlerOverrides?: ChannelManagerEventHandlerOverrides; options?: ChannelManagerOptions; }) { this.client = client; - this.state = new StateStore>({ + this.state = new StateStore({ channels: [], pagination: { isLoading: false, @@ -177,7 +163,7 @@ export class ChannelManager { this.setEventHandlerOverrides(eventHandlerOverrides); this.setOptions(options); this.eventHandlers = new Map( - Object.entries>({ + Object.entries({ channelDeletedHandler: this.channelDeletedHandler, channelHiddenHandler: this.channelHiddenHandler, channelVisibleHandler: this.channelVisibleHandler, @@ -190,7 +176,7 @@ export class ChannelManager { ); } - public setChannels = (valueOrFactory: ChannelSetterParameterType) => { + public setChannels = (valueOrFactory: ChannelSetterParameterType) => { this.state.next((current) => { const { channels: currentChannels } = current; const newChannels = isPatch(valueOrFactory) ? valueOrFactory(currentChannels) : valueOrFactory; @@ -204,16 +190,16 @@ export class ChannelManager { }); }; - public setEventHandlerOverrides = (eventHandlerOverrides: ChannelManagerEventHandlerOverrides = {}) => { + public setEventHandlerOverrides = (eventHandlerOverrides: ChannelManagerEventHandlerOverrides = {}) => { const truthyEventHandlerOverrides = Object.entries(eventHandlerOverrides).reduce< - Partial> + Partial >((acc, [key, value]) => { if (value) { - acc[key as keyof ChannelManagerEventHandlerOverrides] = value; + acc[key as keyof ChannelManagerEventHandlerOverrides] = value; } return acc; }, {}); - this.eventHandlerOverrides = new Map(Object.entries>(truthyEventHandlerOverrides)); + this.eventHandlerOverrides = new Map(Object.entries(truthyEventHandlerOverrides)); }; public setOptions = (options: ChannelManagerOptions = {}) => { @@ -221,8 +207,8 @@ export class ChannelManager { }; public queryChannels = async ( - filters: ChannelFilters, - sort: ChannelSort = [], + filters: ChannelFilters, + sort: ChannelSort = [], options: ChannelOptions = {}, stateOptions: ChannelStateOptions = {}, ) => { @@ -311,7 +297,7 @@ export class ChannelManager { } }; - private notificationAddedToChannelHandler = async (event: Event) => { + private notificationAddedToChannelHandler = async (event: Event) => { const { id, type, members } = event?.channel ?? {}; if (!type || !this.options.allowNotLoadedChannelPromotionForEvent?.['notification.added_to_channel']) { return; @@ -344,7 +330,7 @@ export class ChannelManager { ); }; - private channelDeletedHandler = (event: Event) => { + private channelDeletedHandler = (event: Event) => { const { channels } = this.state.getLatestValue(); if (!channels) { return; @@ -363,7 +349,7 @@ export class ChannelManager { private channelHiddenHandler = this.channelDeletedHandler; - private newMessageHandler = (event: Event) => { + private newMessageHandler = (event: Event) => { const { pagination, channels } = this.state.getLatestValue(); if (!channels) { return; @@ -412,7 +398,7 @@ export class ChannelManager { ); }; - private notificationNewMessageHandler = async (event: Event) => { + private notificationNewMessageHandler = async (event: Event) => { const { id, type } = event?.channel ?? {}; const { channels, pagination } = this.state.getLatestValue(); @@ -448,7 +434,7 @@ export class ChannelManager { ); }; - private channelVisibleHandler = async (event: Event) => { + private channelVisibleHandler = async (event: Event) => { const { channels, pagination } = this.state.getLatestValue(); const { sort, filters } = pagination ?? {}; const { channel_type: channelType, channel_id: channelId } = event; @@ -485,7 +471,7 @@ export class ChannelManager { private notificationRemovedFromChannelHandler = this.channelDeletedHandler; - private memberUpdatedHandler = (event: Event) => { + private memberUpdatedHandler = (event: Event) => { const { pagination, channels } = this.state.getLatestValue(); const { filters, sort } = pagination; if ( @@ -549,7 +535,7 @@ export class ChannelManager { this.setChannels(newChannels); }; - private subscriptionOrOverride = (event: Event) => { + private subscriptionOrOverride = (event: Event) => { const handlerName = channelManagerEventToHandlerMapping[event.type as ChannelManagerEventTypes]; const defaultEventHandler = this.eventHandlers.get(handlerName); const eventHandlerOverride = this.eventHandlerOverrides.get(handlerName); diff --git a/src/client.ts b/src/client.ts index ed86725558..d9effaf661 100644 --- a/src/client.ts +++ b/src/client.ts @@ -601,7 +601,7 @@ export class StreamChat { eventHandlerOverrides = {}, options = {}, }: { - eventHandlerOverrides?: ChannelManagerEventHandlerOverrides; + eventHandlerOverrides?: ChannelManagerEventHandlerOverrides; options?: ChannelManagerOptions; }) => { return new ChannelManager({ client: this, eventHandlerOverrides, options }); @@ -1577,7 +1577,7 @@ export class StreamChat { * @param {ChannelStateOptions} [stateOptions] State options object. These options will only be used for state management and won't be sent in the request. * - stateOptions.skipInitialization - Skips the initialization of the state for the channels matching the ids in the list. * - * @return {Promise<{ channels: Array>}> } search channels response + * @return {Promise<{ channels: Array}> } search channels response */ async queryChannels( filterConditions: ChannelFilters, @@ -1926,7 +1926,7 @@ export class StreamChat { getChannelByMembers = (channelType: string, custom: ChannelData) => { // Check if the channel already exists. // Only allow 1 channel object per cid - const memberIds = (custom.members ?? []).map((member: string | NewMemberPayload) => + const memberIds = (custom.members ?? []).map((member: string | NewMemberPayload) => typeof member === 'string' ? member : member.user_id ?? '', ); const membersStr = memberIds.sort().join(','); diff --git a/src/types.ts b/src/types.ts index 3e5032b581..edaeeea1da 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3505,10 +3505,10 @@ export type VelocityFilterConfig = { async?: boolean; }; -export type PromoteChannelParams = { - channels: Array>; - channelToMove: Channel; - sort: ChannelSort; +export type PromoteChannelParams = { + channels: Array; + channelToMove: Channel; + sort: ChannelSort; /** * If the index of the channel within `channels` list which is being moved upwards * (`channelToMove`) is known, you can supply it to skip extra calculation. diff --git a/src/utils.ts b/src/utils.ts index 754228d979..53c3fe3fd3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -826,12 +826,12 @@ export const messageSetPagination = (params: MessagePaginationUpdatedParams) => */ const WATCH_QUERY_IN_PROGRESS_FOR_CHANNEL: Record | undefined> = {}; -type GetChannelParams = { - client: StreamChat; - channel?: Channel; +type GetChannelParams = { + client: StreamChat; + channel?: Channel; id?: string; members?: string[]; - options?: ChannelQueryOptions; + options?: ChannelQueryOptions; type?: string; }; /** @@ -844,14 +844,14 @@ type GetChannelParams({ +export const getAndWatchChannel = async ({ channel, client, id, members, options, type, -}: GetChannelParams) => { +}: GetChannelParams) => { if (!channel && !type) { throw new Error('Channel or channel type have to be provided to query a channel.'); } @@ -905,8 +905,8 @@ export const generateChannelTempCid = (channelType: string, members: string[]) = * Checks if a channel is pinned or not. Will return true only if channel.state.membership.pinned_at exists. * @param channel */ -export const isChannelPinned = ( - channel: Channel, +export const isChannelPinned = ( + channel: Channel, ) => { if (!channel) return false; @@ -919,8 +919,8 @@ export const isChannelPinned = ( - channel: Channel, +export const isChannelArchived = ( + channel: Channel, ) => { if (!channel) return false; @@ -934,8 +934,8 @@ export const isChannelArchived = ( - filters: ChannelFilters, +export const shouldConsiderArchivedChannels = ( + filters: ChannelFilters, ) => { if (!filters) return false; @@ -950,17 +950,17 @@ export const shouldConsiderArchivedChannels = ({ +export const extractSortValue = ({ atIndex, sort, targetKey, }: { atIndex: number; - targetKey: keyof ChannelSortBase; - sort?: ChannelSort; + targetKey: keyof ChannelSortBase; + sort?: ChannelSort; }) => { if (!sort) return null; - let option: null | ChannelSortBase = null; + let option: null | ChannelSortBase = null; if (Array.isArray(sort)) { option = sort[atIndex] ?? null; @@ -988,8 +988,8 @@ export const extractSortValue = ( - sort: ChannelSort, +export const shouldConsiderPinnedChannels = ( + sort: ChannelSort, ) => { const value = findPinnedAtSortOrder({ sort }); @@ -1003,10 +1003,10 @@ export const shouldConsiderPinnedChannels = ({ +export const findPinnedAtSortOrder = ({ sort, }: { - sort: ChannelSort; + sort: ChannelSort; }) => extractSortValue({ atIndex: 0, @@ -1020,10 +1020,10 @@ export const findPinnedAtSortOrder = ({ +export const findLastPinnedChannelIndex = ({ channels, }: { - channels: Channel[]; + channels: Channel[]; }) => { let lastPinnedChannelIndex: number | null = null; @@ -1049,12 +1049,12 @@ export const findLastPinnedChannelIndex = ({ +export const promoteChannel = ({ channels, channelToMove, channelToMoveIndexWithinChannels, sort, -}: PromoteChannelParams) => { +}: PromoteChannelParams) => { // get index of channel to move up const targetChannelIndex = channelToMoveIndexWithinChannels ?? channels.findIndex((channel) => channel.cid === channelToMove.cid); @@ -1066,7 +1066,7 @@ export const promoteChannel = (channelToMove); + const isTargetChannelPinned = isChannelPinned(channelToMove); if (targetChannelAlreadyAtTheTop || (considerPinnedChannels && isTargetChannelPinned)) { return channels;