Skip to content

Commit f077f72

Browse files
Cleanup and adjustments
1 parent 3f05732 commit f077f72

10 files changed

Lines changed: 101 additions & 54 deletions

File tree

src/LiveLocationManager.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
import { withCancellation } from './utils/concurrency';
1212
import { StateStore } from './store';
1313
import { WithSubscriptions } from './utils/WithSubscriptions';
14-
import type { ListenerKeys, StreamChat } from './client';
14+
import type { StreamChat } from './client';
1515
import type { Unsubscribe } from './store';
1616
import type {
17+
EventType,
1718
MessageResponse,
1819
SharedLiveLocationResponse,
1920
SharedLocationResponse,
@@ -221,7 +222,7 @@ export class LiveLocationManager extends WithSubscriptions {
221222
'live_location_sharing.started',
222223
'message.updated',
223224
'message.deleted',
224-
] satisfies ListenerKeys[]
225+
] satisfies EventType[]
225226
).map((eventType) =>
226227
this.client.on(eventType, (event) => {
227228
if (!event.message) return;

src/channel.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
logChatPromiseExecution,
99
messageSetPagination,
1010
} from './utils';
11-
import type { ListenerKeys, StreamChat } from './client';
11+
import type { StreamChat } from './client';
1212
import { chatLoggerSystem } from './logger';
1313
import { DEFAULT_QUERY_CHANNEL_MESSAGE_LIST_PAGE_SIZE } from './constants';
1414
import type {
@@ -21,10 +21,11 @@ import type {
2121
ChannelQueryOptions,
2222
ChannelResponse,
2323
ChannelUpdateOptions,
24-
CombinedEvents,
2524
CreateDraftResponse,
25+
Event,
2626
EventHandler,
2727
EventPayload,
28+
EventType,
2829
GetRepliesAPIResponse,
2930
GetRepliesRequest,
3031
LiveLocationPayload,
@@ -76,7 +77,7 @@ export class Channel extends ChannelApi {
7677
_data: ChannelData;
7778
cid: string;
7879
/** */
79-
listeners: Map<ListenerKeys, Set<EventHandler>>;
80+
listeners: Map<EventType, Set<EventHandler>>;
8081
state: ChannelState;
8182
/**
8283
* This boolean is a vague indication of whether the channel exists on chat backend.
@@ -283,7 +284,7 @@ export class Channel extends ChannelApi {
283284
* @param event - For example `{ type: 'message.read' }`.
284285
* @returns The server response.
285286
*/
286-
override async sendEvent(request: { event: CombinedEvents }) {
287+
override async sendEvent(request: { event: Event }) {
287288
this._checkInitialized();
288289
return await super.sendEvent(request);
289290
}
@@ -1497,7 +1498,7 @@ export class Channel extends ChannelApi {
14971498
* @param callbackOrNothing - The callback to call when an event type was provided (optional).
14981499
* @returns An object with an `unsubscribe()` method.
14991500
*/
1500-
on<T extends ListenerKeys | string>(
1501+
on<T extends EventType | string>(
15011502
eventType: T,
15021503
callback: EventHandler<T>,
15031504
): { unsubscribe: () => void };
@@ -1506,7 +1507,7 @@ export class Channel extends ChannelApi {
15061507
callbackOrString: EventHandler | string,
15071508
callbackOrNothing?: EventHandler,
15081509
): { unsubscribe: () => void } {
1509-
const key = callbackOrNothing ? (callbackOrString as ListenerKeys) : 'all';
1510+
const key = callbackOrNothing ? (callbackOrString as EventType) : 'all';
15101511
const callback = callbackOrNothing
15111512
? callbackOrNothing
15121513
: (callbackOrString as EventHandler);
@@ -1541,10 +1542,10 @@ export class Channel extends ChannelApi {
15411542
* @param callbackOrString - The event type, or the callback when removing an all-events listener.
15421543
* @param callbackOrNothing - The callback to remove when an event type was provided (optional).
15431544
*/
1544-
off<T extends ListenerKeys | string>(eventType: T, callback: EventHandler): void;
1545+
off<T extends EventType | string>(eventType: T, callback: EventHandler): void;
15451546
off(callback: EventHandler): void;
15461547
off(callbackOrString: EventHandler | string, callbackOrNothing?: EventHandler): void {
1547-
const key = callbackOrNothing ? (callbackOrString as ListenerKeys) : 'all';
1548+
const key = callbackOrNothing ? (callbackOrString as EventType) : 'all';
15481549
const callback = callbackOrNothing
15491550
? callbackOrNothing
15501551
: (callbackOrString as EventHandler);
@@ -1562,7 +1563,7 @@ export class Channel extends ChannelApi {
15621563
}
15631564
}
15641565

1565-
_handleChannelEvent(event: CombinedEvents) {
1566+
_handleChannelEvent(event: Event) {
15661567
// eslint-disable-next-line @typescript-eslint/no-this-alias
15671568
const channel = this;
15681569
logger

src/channel_manager.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type {
33
ChannelFilters,
44
ChannelSort,
55
ChannelStateOptions,
6-
CombinedEvents,
6+
Event,
77
EventPayload,
88
QueryChannelsAPIResponse,
99
QueryChannelsRequest,
@@ -59,9 +59,9 @@ export type ChannelSetterParameterType = ValueOrPatch<ChannelManagerState['chann
5959
export type ChannelSetterType = (arg: ChannelSetterParameterType) => void;
6060

6161
export type GenericEventHandlerType<T extends unknown[]> = (...args: T) => any;
62-
export type EventHandlerType = GenericEventHandlerType<[CombinedEvents]>;
62+
export type EventHandlerType = GenericEventHandlerType<[Event]>;
6363
export type EventHandlerOverrideType = GenericEventHandlerType<
64-
[ChannelSetterType, CombinedEvents]
64+
[ChannelSetterType, Event]
6565
>;
6666

6767
export type ChannelManagerEventTypes =
@@ -527,7 +527,7 @@ export class ChannelManager extends WithSubscriptions {
527527
}
528528
};
529529

530-
private notificationAddedToChannelHandler = async (event_: CombinedEvents) => {
530+
private notificationAddedToChannelHandler = async (event_: Event) => {
531531
const event = event_ as EventPayload<'notification.added_to_channel'>;
532532
const { id, type, members } = event?.channel ?? {};
533533

@@ -569,7 +569,7 @@ export class ChannelManager extends WithSubscriptions {
569569
);
570570
};
571571

572-
private channelDeletedHandler = (event_: CombinedEvents) => {
572+
private channelDeletedHandler = (event_: Event) => {
573573
const event = event_ as EventPayload<
574574
'channel.deleted' | 'channel.hidden' | 'notification.removed_from_channel'
575575
>;
@@ -594,7 +594,7 @@ export class ChannelManager extends WithSubscriptions {
594594

595595
private channelHiddenHandler = this.channelDeletedHandler;
596596

597-
private newMessageHandler = (event_: CombinedEvents) => {
597+
private newMessageHandler = (event_: Event) => {
598598
const event = event_ as EventPayload<'message.new'>;
599599

600600
const { pagination, channels } = this.state.getLatestValue();
@@ -646,7 +646,7 @@ export class ChannelManager extends WithSubscriptions {
646646
);
647647
};
648648

649-
private notificationNewMessageHandler = async (event_: CombinedEvents) => {
649+
private notificationNewMessageHandler = async (event_: Event) => {
650650
const event = event_ as EventPayload<'notification.message_new'>;
651651

652652
const { id, type } = event?.channel ?? {};
@@ -685,7 +685,7 @@ export class ChannelManager extends WithSubscriptions {
685685
);
686686
};
687687

688-
private channelVisibleHandler = async (event_: CombinedEvents) => {
688+
private channelVisibleHandler = async (event_: Event) => {
689689
const event = event_ as EventPayload<'channel.visible' | 'channel.hidden'>;
690690
const { channel_type: channelType, channel_id: channelId } = event;
691691

@@ -725,7 +725,7 @@ export class ChannelManager extends WithSubscriptions {
725725

726726
private notificationRemovedFromChannelHandler = this.channelDeletedHandler;
727727

728-
private memberUpdatedHandler = (event_: CombinedEvents) => {
728+
private memberUpdatedHandler = (event_: Event) => {
729729
const event = event_ as EventPayload<'member.updated'>;
730730
const { pagination, channels } = this.state.getLatestValue();
731731
const { filters, sort = [] } = getResponseFiltersAndSort(pagination);
@@ -795,7 +795,7 @@ export class ChannelManager extends WithSubscriptions {
795795
this.setChannels(newChannels);
796796
};
797797

798-
private subscriptionOrOverride = (event: CombinedEvents) => {
798+
private subscriptionOrOverride = (event: Event) => {
799799
const handlerName =
800800
channelManagerEventToHandlerMapping[event.type as ChannelManagerEventTypes];
801801
const defaultEventHandler = this.eventHandlers.get(handlerName);

src/client.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ import type {
3333
ChannelOptions,
3434
ChannelResponse,
3535
ChannelStateOptions,
36-
CombinedEvents,
3736
Configs,
3837
ConnectAPIResponse,
3938
DeviceIdentifier,
39+
Event,
4040
EventHandler,
41+
EventType,
4142
FlagMessageResponse,
4243
FlagUserResponse,
4344
GetThreadOptions,
@@ -130,8 +131,6 @@ export type MessageComposerSetupState = {
130131
setupFunction: MessageComposerSetupFunction | null;
131132
};
132133

133-
export type ListenerKeys = CombinedEvents['type'] | 'all';
134-
135134
type ClientUser = PartializeAllBut<OwnUserResponse, 'id'> & { anon?: boolean };
136135

137136
export class StreamChat extends ChatApi {
@@ -162,7 +161,7 @@ export class StreamChat extends ChatApi {
162161
clientId?: string;
163162
configs: Configs;
164163
key: string;
165-
listeners: Map<ListenerKeys, Set<EventHandler>>;
164+
listeners: Map<EventType, Set<EventHandler>>;
166165
/**
167166
* When network is recovered, we re-query the active channels on client. But in single query, you can recover
168167
* only 30 channels. So its not guaranteed that all the channels in activeChannels object have updated state.
@@ -727,15 +726,15 @@ export class StreamChat extends ChatApi {
727726
* @returns An object with an `unsubscribe()` method.
728727
*/
729728
on(callback: EventHandler): { unsubscribe: () => void };
730-
on<T extends ListenerKeys | string>(
729+
on<T extends EventType | string>(
731730
eventType: T,
732731
callback: EventHandler<T>,
733732
): { unsubscribe: () => void };
734733
on(
735734
callbackOrString: EventHandler | string,
736735
callbackOrNothing?: EventHandler,
737736
): { unsubscribe: () => void } {
738-
const key = callbackOrNothing ? (callbackOrString as ListenerKeys) : 'all';
737+
const key = callbackOrNothing ? (callbackOrString as EventType) : 'all';
739738
const callback = callbackOrNothing
740739
? callbackOrNothing
741740
: (callbackOrString as EventHandler);
@@ -769,7 +768,7 @@ export class StreamChat extends ChatApi {
769768
off(callback: EventHandler): void;
770769
off(eventType: string, callback: EventHandler): void;
771770
off(callbackOrString: EventHandler | string, callbackOrNothing?: EventHandler) {
772-
const key = callbackOrNothing ? (callbackOrString as ListenerKeys) : 'all';
771+
const key = callbackOrNothing ? (callbackOrString as EventType) : 'all';
773772
const callback = callbackOrNothing
774773
? callbackOrNothing
775774
: (callbackOrString as EventHandler);
@@ -785,14 +784,14 @@ export class StreamChat extends ChatApi {
785784
}
786785
}
787786

788-
dispatchEvent = (event: CombinedEvents) => {
787+
dispatchEvent = (event: Event) => {
789788
if (!event.received_at) event.received_at = new Date();
790789

791790
// client event handlers
792791
const postListenerCallbacks = this._handleClientEvent(event as WSEvent);
793792

794793
// channel event handlers
795-
const cid = (event as Extract<CombinedEvents, { cid?: any }>).cid;
794+
const cid = (event as Extract<Event, { cid?: any }>).cid;
796795
const channel = cid ? this.activeChannels[cid] : undefined;
797796
if (channel) {
798797
channel._handleChannelEvent(event as WSEvent);
@@ -1060,7 +1059,7 @@ export class StreamChat extends ChatApi {
10601059
};
10611060
}
10621061

1063-
_callClientListeners = (event: CombinedEvents) => {
1062+
_callClientListeners = (event: Event) => {
10641063
const allSet = this.listeners.get('all');
10651064
const targetSet = this.listeners.get(event.type);
10661065

src/messageComposer/messageComposer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ import type {
2323
CommandResponse,
2424
DraftMessage,
2525
DraftResponse,
26+
EventType,
2627
LocalMessage,
2728
MessageResponse,
2829
UserResponse,
2930
} from '../types';
3031
import { chatLoggerSystem } from '../logger';
3132
import { WithSubscriptions } from '../utils/WithSubscriptions';
32-
import type { ListenerKeys, StreamChat } from '../client';
33+
import type { StreamChat } from '../client';
3334
import type { CommandSendability, MessageComposerConfig } from './configuration/types';
3435
import type {
3536
CommandSuggestionDisabledReason,
@@ -621,7 +622,7 @@ export class MessageComposer extends WithSubscriptions {
621622
'reaction.new',
622623
'reaction.deleted', // todo: do we need to subscribe to this especially when the whole state is overriden?
623624
'reaction.updated', // todo: do we need to subscribe to this especially when the whole state is overriden?
624-
] satisfies ListenerKeys[];
625+
] satisfies EventType[];
625626

626627
const unsubscribeFunctions = eventTypes.map(
627628
(eventType) =>

src/offline-support/offline_support_api.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type {
22
APIErrorResponse,
33
ChannelResponse,
4-
CombinedEvents,
4+
Event,
55
EventPayload,
6+
EventType,
67
LocalMessage,
78
Message,
89
MessageResponse,
@@ -17,7 +18,7 @@ import type {
1718
PrepareBatchDBQueries,
1819
} from './types';
1920
import { OfflineError } from './types';
20-
import type { ListenerKeys, StreamChat } from '../client';
21+
import type { StreamChat } from '../client';
2122
import type { AxiosError } from 'axios';
2223
import { OfflineDBSyncManager } from './offline_sync_manager';
2324
import { chatLoggerSystem } from '../logger';
@@ -966,7 +967,7 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
966967
return [];
967968
}
968969

969-
const getReactionMethod = (type: ListenerKeys) => {
970+
const getReactionMethod = (type: EventType) => {
970971
switch (type) {
971972
case 'reaction.new':
972973
return this.insertReaction;
@@ -1039,7 +1040,7 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
10391040
event,
10401041
execute = true,
10411042
}: {
1042-
event: CombinedEvents;
1043+
event: Event;
10431044
execute?: boolean;
10441045
}) => {
10451046
const { type } = event;

src/reminders/ReminderManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { WithSubscriptions } from '../utils/WithSubscriptions';
66
import type { ReminderResponseBaseOrResponse } from './Reminder';
77
import type { StreamChat } from '../client';
88
import type {
9-
CombinedEvents,
109
CreateReminderOptions,
10+
Event,
1111
EventPayload,
1212
LocalMessage,
1313
MessageResponse,
@@ -160,7 +160,7 @@ export class ReminderManager extends WithSubscriptions {
160160
// Timers API END //
161161

162162
// WS event handling START //
163-
static isReminderWsEventPayload = (event: CombinedEvents): event is ReminderEvent =>
163+
static isReminderWsEventPayload = (event: Event): event is ReminderEvent =>
164164
'reminder' in event &&
165165
!!event.reminder &&
166166
(event.type.startsWith('reminder.') || event.type === 'notification.reminder_due');

src/thread.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
throttle,
88
} from './utils';
99
import type {
10+
EventType,
1011
LocalMessage,
1112
MessagePaginationOptions,
1213
MessageResponse,
@@ -16,7 +17,7 @@ import type {
1617
} from './types';
1718
import type { SortParamRequest as Gen_SortParamRequest } from './gen/models';
1819
import type { Channel } from './channel';
19-
import type { ListenerKeys, StreamChat } from './client';
20+
import type { StreamChat } from './client';
2021
import type { CustomThreadData } from './custom_types';
2122
import { MessageComposer } from './messageComposer';
2223
import { WithSubscriptions } from './utils/WithSubscriptions';
@@ -400,7 +401,7 @@ export class Thread extends WithSubscriptions {
400401
'reaction.new',
401402
'reaction.deleted',
402403
'reaction.updated',
403-
] satisfies ListenerKeys[];
404+
] satisfies EventType[];
404405

405406
const unsubscribeFunctions = eventTypes.map(
406407
(eventType) =>

0 commit comments

Comments
 (0)