Skip to content

Commit de80973

Browse files
Post-merge fixes
1 parent b5171da commit de80973

File tree

4 files changed

+51
-88
lines changed

4 files changed

+51
-88
lines changed

src/channel_manager.ts

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
import type { StreamChat } from './client';
2-
import type {
3-
DefaultGenerics,
4-
ExtendableGenerics,
5-
Event,
6-
ChannelOptions,
7-
ChannelStateOptions,
8-
ChannelFilters,
9-
ChannelSort,
10-
} from './types';
2+
import type { Event, ChannelOptions, ChannelStateOptions, ChannelFilters, ChannelSort } from './types';
113
import { StateStore, ValueOrPatch, isPatch } from './store';
124
import { Channel } from './channel';
135
import {
@@ -22,41 +14,35 @@ import {
2214
uniqBy,
2315
} from './utils';
2416

25-
export type ChannelManagerPagination<SCG extends ExtendableGenerics = DefaultGenerics> = {
26-
filters: ChannelFilters<SCG>;
17+
export type ChannelManagerPagination = {
18+
filters: ChannelFilters;
2719
hasNext: boolean;
2820
isLoading: boolean;
2921
isLoadingNext: boolean;
3022
options: ChannelOptions;
31-
sort: ChannelSort<SCG>;
23+
sort: ChannelSort;
3224
};
3325

34-
export type ChannelManagerState<SCG extends ExtendableGenerics = DefaultGenerics> = {
35-
channels: Channel<SCG>[];
26+
export type ChannelManagerState = {
27+
channels: Channel[];
3628
/**
3729
* This value will become true the first time queryChannels is successfully executed and
3830
* will remain false otherwise. It's used as a control property regarding whether the list
3931
* has been initialized yet (i.e a query has already been done at least once) or not. We do
4032
* this to prevent state.channels from being forced to be nullable.
4133
*/
4234
initialized: boolean;
43-
pagination: ChannelManagerPagination<SCG>;
35+
pagination: ChannelManagerPagination;
4436
};
4537

46-
export type ChannelSetterParameterType<SCG extends ExtendableGenerics = DefaultGenerics> = ValueOrPatch<
47-
ChannelManagerState<SCG>['channels']
48-
>;
49-
export type ChannelSetterType<SCG extends ExtendableGenerics = DefaultGenerics> = (
50-
arg: ChannelSetterParameterType<SCG>,
51-
) => void;
38+
export type ChannelSetterParameterType = ValueOrPatch<ChannelManagerState['channels']>;
39+
export type ChannelSetterType = (arg: ChannelSetterParameterType) => void;
5240

5341
export type GenericEventHandlerType<T extends unknown[]> = (
5442
...args: T
5543
) => void | (() => void) | ((...args: T) => Promise<void>) | Promise<void>;
56-
export type EventHandlerType<SCG extends ExtendableGenerics = DefaultGenerics> = GenericEventHandlerType<[Event<SCG>]>;
57-
export type EventHandlerOverrideType<SCG extends ExtendableGenerics = DefaultGenerics> = GenericEventHandlerType<
58-
[ChannelSetterType<SCG>, Event<SCG>]
59-
>;
44+
export type EventHandlerType = GenericEventHandlerType<[Event]>;
45+
export type EventHandlerOverrideType = GenericEventHandlerType<[ChannelSetterType, Event]>;
6046

6147
export type ChannelManagerEventTypes =
6248
| 'notification.added_to_channel'
@@ -80,8 +66,8 @@ export type ChannelManagerEventHandlerNames =
8066
| 'notificationNewMessageHandler'
8167
| 'notificationRemovedFromChannelHandler';
8268

83-
export type ChannelManagerEventHandlerOverrides<SCG extends ExtendableGenerics = DefaultGenerics> = Partial<
84-
Record<ChannelManagerEventHandlerNames, EventHandlerOverrideType<SCG>>
69+
export type ChannelManagerEventHandlerOverrides = Partial<
70+
Record<ChannelManagerEventHandlerNames, EventHandlerOverrideType>
8571
>;
8672

8773
export const channelManagerEventToHandlerMapping: {
@@ -144,12 +130,12 @@ export const DEFAULT_CHANNEL_MANAGER_PAGINATION_OPTIONS = {
144130
*
145131
* @internal
146132
*/
147-
export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
148-
public readonly state: StateStore<ChannelManagerState<SCG>>;
149-
private client: StreamChat<SCG>;
133+
export class ChannelManager {
134+
public readonly state: StateStore<ChannelManagerState>;
135+
private client: StreamChat;
150136
private unsubscribeFunctions: Set<() => void> = new Set();
151-
private eventHandlers: Map<string, EventHandlerType<SCG>> = new Map();
152-
private eventHandlerOverrides: Map<string, EventHandlerOverrideType<SCG>> = new Map();
137+
private eventHandlers: Map<string, EventHandlerType> = new Map();
138+
private eventHandlerOverrides: Map<string, EventHandlerOverrideType> = new Map();
153139
private options: ChannelManagerOptions = {};
154140
private stateOptions: ChannelStateOptions = {};
155141

@@ -158,12 +144,12 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
158144
eventHandlerOverrides = {},
159145
options = {},
160146
}: {
161-
client: StreamChat<SCG>;
162-
eventHandlerOverrides?: ChannelManagerEventHandlerOverrides<SCG>;
147+
client: StreamChat;
148+
eventHandlerOverrides?: ChannelManagerEventHandlerOverrides;
163149
options?: ChannelManagerOptions;
164150
}) {
165151
this.client = client;
166-
this.state = new StateStore<ChannelManagerState<SCG>>({
152+
this.state = new StateStore<ChannelManagerState>({
167153
channels: [],
168154
pagination: {
169155
isLoading: false,
@@ -178,7 +164,7 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
178164
this.setEventHandlerOverrides(eventHandlerOverrides);
179165
this.setOptions(options);
180166
this.eventHandlers = new Map(
181-
Object.entries<EventHandlerType<SCG>>({
167+
Object.entries<EventHandlerType>({
182168
channelDeletedHandler: this.channelDeletedHandler,
183169
channelHiddenHandler: this.channelHiddenHandler,
184170
channelVisibleHandler: this.channelVisibleHandler,
@@ -191,7 +177,7 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
191177
);
192178
}
193179

194-
public setChannels = (valueOrFactory: ChannelSetterParameterType<SCG>) => {
180+
public setChannels = (valueOrFactory: ChannelSetterParameterType) => {
195181
this.state.next((current) => {
196182
const { channels: currentChannels } = current;
197183
const newChannels = isPatch(valueOrFactory) ? valueOrFactory(currentChannels) : valueOrFactory;
@@ -205,25 +191,25 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
205191
});
206192
};
207193

208-
public setEventHandlerOverrides = (eventHandlerOverrides: ChannelManagerEventHandlerOverrides<SCG> = {}) => {
194+
public setEventHandlerOverrides = (eventHandlerOverrides: ChannelManagerEventHandlerOverrides = {}) => {
209195
const truthyEventHandlerOverrides = Object.entries(eventHandlerOverrides).reduce<
210-
Partial<ChannelManagerEventHandlerOverrides<SCG>>
196+
Partial<ChannelManagerEventHandlerOverrides>
211197
>((acc, [key, value]) => {
212198
if (value) {
213-
acc[key as keyof ChannelManagerEventHandlerOverrides<SCG>] = value;
199+
acc[key as keyof ChannelManagerEventHandlerOverrides] = value;
214200
}
215201
return acc;
216202
}, {});
217-
this.eventHandlerOverrides = new Map(Object.entries<EventHandlerOverrideType<SCG>>(truthyEventHandlerOverrides));
203+
this.eventHandlerOverrides = new Map(Object.entries<EventHandlerOverrideType>(truthyEventHandlerOverrides));
218204
};
219205

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

224210
public queryChannels = async (
225-
filters: ChannelFilters<SCG>,
226-
sort: ChannelSort<SCG> = [],
211+
filters: ChannelFilters,
212+
sort: ChannelSort = [],
227213
options: ChannelOptions = {},
228214
stateOptions: ChannelStateOptions = {},
229215
) => {
@@ -294,7 +280,7 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
294280
const newOptions = { ...options, offset: newOffset };
295281

296282
this.state.partialNext({
297-
channels: uniqBy<Channel<SCG>>([...(channels || []), ...nextChannels], 'cid'),
283+
channels: uniqBy<Channel>([...(channels || []), ...nextChannels], 'cid'),
298284
pagination: {
299285
...pagination,
300286
hasNext: (nextChannels?.length ?? 0) >= limit,
@@ -313,7 +299,7 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
313299
}
314300
};
315301

316-
private notificationAddedToChannelHandler = async (event: Event<SCG>) => {
302+
private notificationAddedToChannelHandler = async (event: Event) => {
317303
const { id, type, members } = event?.channel ?? {};
318304

319305
if (!type || !this.options.allowNotLoadedChannelPromotionForEvent?.['notification.added_to_channel']) {
@@ -349,7 +335,7 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
349335
);
350336
};
351337

352-
private channelDeletedHandler = (event: Event<SCG>) => {
338+
private channelDeletedHandler = (event: Event) => {
353339
const { channels } = this.state.getLatestValue();
354340
if (!channels) {
355341
return;
@@ -368,7 +354,7 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
368354

369355
private channelHiddenHandler = this.channelDeletedHandler;
370356

371-
private newMessageHandler = (event: Event<SCG>) => {
357+
private newMessageHandler = (event: Event) => {
372358
const { pagination, channels } = this.state.getLatestValue();
373359
if (!channels) {
374360
return;
@@ -417,7 +403,7 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
417403
);
418404
};
419405

420-
private notificationNewMessageHandler = async (event: Event<SCG>) => {
406+
private notificationNewMessageHandler = async (event: Event) => {
421407
const { id, type } = event?.channel ?? {};
422408

423409
if (!id || !type) {
@@ -454,7 +440,7 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
454440
);
455441
};
456442

457-
private channelVisibleHandler = async (event: Event<SCG>) => {
443+
private channelVisibleHandler = async (event: Event) => {
458444
const { channel_type: channelType, channel_id: channelId } = event;
459445

460446
if (!channelType || !channelId) {
@@ -493,7 +479,7 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
493479

494480
private notificationRemovedFromChannelHandler = this.channelDeletedHandler;
495481

496-
private memberUpdatedHandler = (event: Event<SCG>) => {
482+
private memberUpdatedHandler = (event: Event) => {
497483
const { pagination, channels } = this.state.getLatestValue();
498484
const { filters, sort } = pagination;
499485
if (
@@ -557,7 +543,7 @@ export class ChannelManager<SCG extends ExtendableGenerics = DefaultGenerics> {
557543
this.setChannels(newChannels);
558544
};
559545

560-
private subscriptionOrOverride = (event: Event<SCG>) => {
546+
private subscriptionOrOverride = (event: Event) => {
561547
const handlerName = channelManagerEventToHandlerMapping[event.type as ChannelManagerEventTypes];
562548
const defaultEventHandler = this.eventHandlers.get(handlerName);
563549
const eventHandlerOverride = this.eventHandlerOverrides.get(handlerName);

src/client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ export class StreamChat {
604604
eventHandlerOverrides = {},
605605
options = {},
606606
}: {
607-
eventHandlerOverrides?: ChannelManagerEventHandlerOverrides<StreamChatGenerics>;
607+
eventHandlerOverrides?: ChannelManagerEventHandlerOverrides;
608608
options?: ChannelManagerOptions;
609609
}) => {
610610
return new ChannelManager({ client: this, eventHandlerOverrides, options });
@@ -1580,7 +1580,7 @@ export class StreamChat {
15801580
* @param {ChannelStateOptions} [stateOptions] State options object. These options will only be used for state management and won't be sent in the request.
15811581
* - stateOptions.skipInitialization - Skips the initialization of the state for the channels matching the ids in the list.
15821582
*
1583-
* @return {Promise<{ channels: Array<ChannelAPIResponse<AStreamChatGenerics>>}> } search channels response
1583+
* @return {Promise<{ channels: Array<ChannelAPIResponse>}> } search channels response
15841584
*/
15851585
async queryChannels(
15861586
filterConditions: ChannelFilters,
@@ -1921,7 +1921,7 @@ export class StreamChat {
19211921
getChannelByMembers = (channelType: string, custom: ChannelData) => {
19221922
// Check if the channel already exists.
19231923
// Only allow 1 channel object per cid
1924-
const memberIds = (custom.members ?? []).map((member: string | NewMemberPayload<StreamChatGenerics>) =>
1924+
const memberIds = (custom.members ?? []).map((member: string | NewMemberPayload) =>
19251925
typeof member === 'string' ? member : member.user_id ?? '',
19261926
);
19271927
const membersStr = memberIds.sort().join(',');

src/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3514,10 +3514,10 @@ export type VelocityFilterConfig = {
35143514
async?: boolean;
35153515
};
35163516

3517-
export type PromoteChannelParams<SCG extends ExtendableGenerics = DefaultGenerics> = {
3518-
channels: Array<Channel<SCG>>;
3519-
channelToMove: Channel<SCG>;
3520-
sort: ChannelSort<SCG>;
3517+
export type PromoteChannelParams = {
3518+
channels: Array<Channel>;
3519+
channelToMove: Channel;
3520+
sort: ChannelSort;
35213521
/**
35223522
* If the index of the channel within `channels` list which is being moved upwards
35233523
* (`channelToMove`) is known, you can supply it to skip extra calculation.

src/utils.ts

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -865,14 +865,7 @@ type GetChannelParams = {
865865
* @param id
866866
* @param channel
867867
*/
868-
export const getAndWatchChannel = async ({
869-
channel,
870-
client,
871-
id,
872-
members,
873-
options,
874-
type,
875-
}: GetChannelParams) => {
868+
export const getAndWatchChannel = async ({ channel, client, id, members, options, type }: GetChannelParams) => {
876869
if (!channel && !type) {
877870
throw new Error('Channel or channel type have to be provided to query a channel.');
878871
}
@@ -926,9 +919,7 @@ export const generateChannelTempCid = (channelType: string, members: string[]) =
926919
* Checks if a channel is pinned or not. Will return true only if channel.state.membership.pinned_at exists.
927920
* @param channel
928921
*/
929-
export const isChannelPinned = (
930-
channel: Channel,
931-
) => {
922+
export const isChannelPinned = (channel: Channel) => {
932923
if (!channel) return false;
933924

934925
const member = channel.state.membership;
@@ -940,9 +931,7 @@ export const isChannelPinned = (
940931
* Checks if a channel is archived or not. Will return true only if channel.state.membership.archived_at exists.
941932
* @param channel
942933
*/
943-
export const isChannelArchived = (
944-
channel: Channel,
945-
) => {
934+
export const isChannelArchived = (channel: Channel) => {
946935
if (!channel) return false;
947936

948937
const member = channel.state.membership;
@@ -955,9 +944,7 @@ export const isChannelArchived = (
955944
* on filters. Will return true only if filters.archived exists and is a boolean value.
956945
* @param filters
957946
*/
958-
export const shouldConsiderArchivedChannels = (
959-
filters: ChannelFilters,
960-
) => {
947+
export const shouldConsiderArchivedChannels = (filters: ChannelFilters) => {
961948
if (!filters) return false;
962949

963950
return typeof filters.archived === 'boolean';
@@ -1009,9 +996,7 @@ export const extractSortValue = ({
1009996
/**
1010997
* Returns true only if `{ pinned_at: -1 }` or `{ pinned_at: 1 }` option is first within the `sort` array.
1011998
*/
1012-
export const shouldConsiderPinnedChannels = (
1013-
sort: ChannelSort,
1014-
) => {
999+
export const shouldConsiderPinnedChannels = (sort: ChannelSort) => {
10151000
const value = findPinnedAtSortOrder({ sort });
10161001

10171002
if (typeof value !== 'number') return false;
@@ -1024,11 +1009,7 @@ export const shouldConsiderPinnedChannels = (
10241009
* an array sort value type has the first value be an object containing pinned_at.
10251010
* @param sort
10261011
*/
1027-
export const findPinnedAtSortOrder = ({
1028-
sort,
1029-
}: {
1030-
sort: ChannelSort;
1031-
}) =>
1012+
export const findPinnedAtSortOrder = ({ sort }: { sort: ChannelSort }) =>
10321013
extractSortValue({
10331014
atIndex: 0,
10341015
sort,
@@ -1041,11 +1022,7 @@ export const findPinnedAtSortOrder = ({
10411022
* start of the array.
10421023
* @param channels
10431024
*/
1044-
export const findLastPinnedChannelIndex = ({
1045-
channels,
1046-
}: {
1047-
channels: Channel[];
1048-
}) => {
1025+
export const findLastPinnedChannelIndex = ({ channels }: { channels: Channel[] }) => {
10491026
let lastPinnedChannelIndex: number | null = null;
10501027

10511028
for (const channel of channels) {

0 commit comments

Comments
 (0)