Skip to content

Commit

Permalink
Post-merge fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
arnautov-anton committed Feb 12, 2025
1 parent 8a87f88 commit ca04832
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 82 deletions.
86 changes: 36 additions & 50 deletions src/channel_manager.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -21,41 +13,35 @@ import {
shouldConsiderPinnedChannels,
} from './utils';

export type ChannelManagerPagination<SCG extends ExtendableGenerics = DefaultGenerics> = {
filters: ChannelFilters<SCG>;
export type ChannelManagerPagination = {
filters: ChannelFilters;
hasNext: boolean;
isLoading: boolean;
isLoadingNext: boolean;
options: ChannelOptions;
sort: ChannelSort<SCG>;
sort: ChannelSort;
};

export type ChannelManagerState<SCG extends ExtendableGenerics = DefaultGenerics> = {
channels: Channel<SCG>[];
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
* has been initialized yet (i.e a query has already been done at least once) or not. We do
* this to prevent state.channels from being forced to be nullable.
*/
initialized: boolean;
pagination: ChannelManagerPagination<SCG>;
pagination: ChannelManagerPagination;
};

export type ChannelSetterParameterType<SCG extends ExtendableGenerics = DefaultGenerics> = ValueOrPatch<
ChannelManagerState<SCG>['channels']
>;
export type ChannelSetterType<SCG extends ExtendableGenerics = DefaultGenerics> = (
arg: ChannelSetterParameterType<SCG>,
) => void;
export type ChannelSetterParameterType = ValueOrPatch<ChannelManagerState['channels']>;
export type ChannelSetterType = (arg: ChannelSetterParameterType) => void;

export type GenericEventHandlerType<T extends unknown[]> = (
...args: T
) => void | (() => void) | ((...args: T) => Promise<void>) | Promise<void>;
export type EventHandlerType<SCG extends ExtendableGenerics = DefaultGenerics> = GenericEventHandlerType<[Event<SCG>]>;
export type EventHandlerOverrideType<SCG extends ExtendableGenerics = DefaultGenerics> = GenericEventHandlerType<
[ChannelSetterType<SCG>, Event<SCG>]
>;
export type EventHandlerType = GenericEventHandlerType<[Event]>;
export type EventHandlerOverrideType = GenericEventHandlerType<[ChannelSetterType, Event]>;

export type ChannelManagerEventTypes =
| 'notification.added_to_channel'
Expand All @@ -79,8 +65,8 @@ export type ChannelManagerEventHandlerNames =
| 'notificationNewMessageHandler'
| 'notificationRemovedFromChannelHandler';

export type ChannelManagerEventHandlerOverrides<SCG extends ExtendableGenerics = DefaultGenerics> = Partial<
Record<ChannelManagerEventHandlerNames, EventHandlerOverrideType<SCG>>
export type ChannelManagerEventHandlerOverrides = Partial<
Record<ChannelManagerEventHandlerNames, EventHandlerOverrideType>
>;

export const channelManagerEventToHandlerMapping: {
Expand Down Expand Up @@ -143,12 +129,12 @@ export const DEFAULT_CHANNEL_MANAGER_PAGINATION_OPTIONS = {
*
* @internal
*/
export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
public readonly state: StateStore<ChannelManagerState<SCG>>;
private client: StreamChat<SCG>;
export class ChannelManager {
public readonly state: StateStore<ChannelManagerState>;
private client: StreamChat;
private unsubscribeFunctions: Set<() => void> = new Set();
private eventHandlers: Map<string, EventHandlerType<SCG>> = new Map();
private eventHandlerOverrides: Map<string, EventHandlerOverrideType<SCG>> = new Map();
private eventHandlers: Map<string, EventHandlerType> = new Map();
private eventHandlerOverrides: Map<string, EventHandlerOverrideType> = new Map();
private options: ChannelManagerOptions = {};
private stateOptions: ChannelStateOptions = {};

Expand All @@ -157,12 +143,12 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
eventHandlerOverrides = {},
options = {},
}: {
client: StreamChat<SCG>;
eventHandlerOverrides?: ChannelManagerEventHandlerOverrides<SCG>;
client: StreamChat;
eventHandlerOverrides?: ChannelManagerEventHandlerOverrides;
options?: ChannelManagerOptions;
}) {
this.client = client;
this.state = new StateStore<ChannelManagerState<SCG>>({
this.state = new StateStore<ChannelManagerState>({
channels: [],
pagination: {
isLoading: false,
Expand All @@ -177,7 +163,7 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
this.setEventHandlerOverrides(eventHandlerOverrides);
this.setOptions(options);
this.eventHandlers = new Map(
Object.entries<EventHandlerType<SCG>>({
Object.entries<EventHandlerType>({
channelDeletedHandler: this.channelDeletedHandler,
channelHiddenHandler: this.channelHiddenHandler,
channelVisibleHandler: this.channelVisibleHandler,
Expand All @@ -190,7 +176,7 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
);
}

public setChannels = (valueOrFactory: ChannelSetterParameterType<SCG>) => {
public setChannels = (valueOrFactory: ChannelSetterParameterType) => {
this.state.next((current) => {
const { channels: currentChannels } = current;
const newChannels = isPatch(valueOrFactory) ? valueOrFactory(currentChannels) : valueOrFactory;
Expand All @@ -204,25 +190,25 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
});
};

public setEventHandlerOverrides = (eventHandlerOverrides: ChannelManagerEventHandlerOverrides<SCG> = {}) => {
public setEventHandlerOverrides = (eventHandlerOverrides: ChannelManagerEventHandlerOverrides = {}) => {
const truthyEventHandlerOverrides = Object.entries(eventHandlerOverrides).reduce<
Partial<ChannelManagerEventHandlerOverrides<SCG>>
Partial<ChannelManagerEventHandlerOverrides>
>((acc, [key, value]) => {
if (value) {
acc[key as keyof ChannelManagerEventHandlerOverrides<SCG>] = value;
acc[key as keyof ChannelManagerEventHandlerOverrides] = value;
}
return acc;
}, {});
this.eventHandlerOverrides = new Map(Object.entries<EventHandlerOverrideType<SCG>>(truthyEventHandlerOverrides));
this.eventHandlerOverrides = new Map(Object.entries<EventHandlerOverrideType>(truthyEventHandlerOverrides));
};

public setOptions = (options: ChannelManagerOptions = {}) => {
this.options = { ...DEFAULT_CHANNEL_MANAGER_OPTIONS, ...options };
};

public queryChannels = async (
filters: ChannelFilters<SCG>,
sort: ChannelSort<SCG> = [],
filters: ChannelFilters,
sort: ChannelSort = [],
options: ChannelOptions = {},
stateOptions: ChannelStateOptions = {},
) => {
Expand Down Expand Up @@ -311,7 +297,7 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
}
};

private notificationAddedToChannelHandler = async (event: Event<SCG>) => {
private notificationAddedToChannelHandler = async (event: Event) => {
const { id, type, members } = event?.channel ?? {};
if (!type || !this.options.allowNotLoadedChannelPromotionForEvent?.['notification.added_to_channel']) {
return;
Expand Down Expand Up @@ -344,7 +330,7 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
);
};

private channelDeletedHandler = (event: Event<SCG>) => {
private channelDeletedHandler = (event: Event) => {
const { channels } = this.state.getLatestValue();
if (!channels) {
return;
Expand All @@ -363,7 +349,7 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {

private channelHiddenHandler = this.channelDeletedHandler;

private newMessageHandler = (event: Event<SCG>) => {
private newMessageHandler = (event: Event) => {
const { pagination, channels } = this.state.getLatestValue();
if (!channels) {
return;
Expand Down Expand Up @@ -412,7 +398,7 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
);
};

private notificationNewMessageHandler = async (event: Event<SCG>) => {
private notificationNewMessageHandler = async (event: Event) => {
const { id, type } = event?.channel ?? {};

const { channels, pagination } = this.state.getLatestValue();
Expand Down Expand Up @@ -448,7 +434,7 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
);
};

private channelVisibleHandler = async (event: Event<SCG>) => {
private channelVisibleHandler = async (event: Event) => {
const { channels, pagination } = this.state.getLatestValue();
const { sort, filters } = pagination ?? {};
const { channel_type: channelType, channel_id: channelId } = event;
Expand Down Expand Up @@ -485,7 +471,7 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {

private notificationRemovedFromChannelHandler = this.channelDeletedHandler;

private memberUpdatedHandler = (event: Event<SCG>) => {
private memberUpdatedHandler = (event: Event) => {
const { pagination, channels } = this.state.getLatestValue();
const { filters, sort } = pagination;
if (
Expand Down Expand Up @@ -549,7 +535,7 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
this.setChannels(newChannels);
};

private subscriptionOrOverride = (event: Event<SCG>) => {
private subscriptionOrOverride = (event: Event) => {
const handlerName = channelManagerEventToHandlerMapping[event.type as ChannelManagerEventTypes];
const defaultEventHandler = this.eventHandlers.get(handlerName);
const eventHandlerOverride = this.eventHandlerOverrides.get(handlerName);
Expand Down
6 changes: 3 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ export class StreamChat {
eventHandlerOverrides = {},
options = {},
}: {
eventHandlerOverrides?: ChannelManagerEventHandlerOverrides<StreamChatGenerics>;
eventHandlerOverrides?: ChannelManagerEventHandlerOverrides;
options?: ChannelManagerOptions;
}) => {
return new ChannelManager({ client: this, eventHandlerOverrides, options });
Expand Down Expand Up @@ -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<ChannelAPIResponse<AStreamChatGenerics>>}> } search channels response
* @return {Promise<{ channels: Array<ChannelAPIResponse>}> } search channels response
*/
async queryChannels(
filterConditions: ChannelFilters,
Expand Down Expand Up @@ -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<StreamChatGenerics>) =>
const memberIds = (custom.members ?? []).map((member: string | NewMemberPayload) =>
typeof member === 'string' ? member : member.user_id ?? '',
);
const membersStr = memberIds.sort().join(',');
Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3505,10 +3505,10 @@ export type VelocityFilterConfig = {
async?: boolean;
};

export type PromoteChannelParams<SCG extends ExtendableGenerics = DefaultGenerics> = {
channels: Array<Channel<SCG>>;
channelToMove: Channel<SCG>;
sort: ChannelSort<SCG>;
export type PromoteChannelParams = {
channels: Array<Channel>;
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.
Expand Down
Loading

0 comments on commit ca04832

Please sign in to comment.