Skip to content

Commit 1ce05a8

Browse files
Rename applyModifications to setupFunction, cleanup
1 parent 8cf136f commit 1ce05a8

File tree

2 files changed

+21
-35
lines changed

2 files changed

+21
-35
lines changed

src/client.ts

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -230,30 +230,21 @@ import type {
230230
import { ChannelManager } from './channel_manager';
231231
import { NotificationManager } from './notifications';
232232
import { StateStore } from './store';
233-
import type { MessageComposerOptions } from './messageComposer';
234-
import { MessageComposer } from './messageComposer';
233+
import type { MessageComposer } from './messageComposer';
235234

236235
function isString(x: unknown): x is string {
237236
return typeof x === 'string' || x instanceof String;
238237
}
239238

240-
type MessageComposerDefine = ({
241-
constructorParameters,
242-
}: {
243-
constructorParameters: MessageComposerOptions;
244-
}) => MessageComposer;
239+
type MessageComposerTearDownFunction = () => void;
245240

246-
type MessageComposerApplyModifications = ({
241+
type MessageComposerSetupFunction = ({
247242
composer,
248243
}: {
249244
composer: MessageComposer;
250-
}) => void | (() => void);
245+
}) => void | MessageComposerTearDownFunction;
251246

252-
// TODO: maybe template modifications
253-
// { template1: { applyModifications... }, template2: {applyModifications} }
254-
// new MessageComposer({ channel, modificationTemplate: 'template1' })
255247
type MessageComposerSetupState = {
256-
define: MessageComposerDefine;
257248
/**
258249
* Each `MessageComposer` runs this function each time its signature changes or
259250
* whenever you run `MessageComposer.registerSubscriptions`. Function returned
@@ -262,12 +253,7 @@ type MessageComposerSetupState = {
262253
* modified parts is the general way to go but if your setup gets a bit
263254
* complicated, feel free to restore the whole composer with `MessageComposer.restore`.
264255
*/
265-
applyModifications: MessageComposerApplyModifications | null;
266-
};
267-
268-
const INITIAL_MESSAGE_COMPOSER_SETUP_STATE: MessageComposerSetupState = {
269-
define: ({ constructorParameters }) => new MessageComposer(constructorParameters),
270-
applyModifications: null,
256+
setupFunction: MessageComposerSetupFunction | null;
271257
};
272258

273259
export class StreamChat {
@@ -325,8 +311,12 @@ export class StreamChat {
325311
sdkIdentifier?: SdkIdentifier;
326312
deviceIdentifier?: DeviceIdentifier;
327313
private nextRequestAbortController: AbortController | null = null;
328-
public _messageComposerSetupState: StateStore<MessageComposerSetupState> =
329-
new StateStore(INITIAL_MESSAGE_COMPOSER_SETUP_STATE);
314+
/**
315+
* @private
316+
*/
317+
_messageComposerSetupState = new StateStore<MessageComposerSetupState>({
318+
setupFunction: null,
319+
});
330320

331321
/**
332322
* Initialize a client
@@ -4397,13 +4387,9 @@ export class StreamChat {
43974387
return await this.post<QueryDraftsResponse>(this.baseURL + '/drafts/query', payload);
43984388
}
43994389

4400-
// TODO: this might not be needed
4401-
public createMessageComposer: MessageComposerDefine = (setup) =>
4402-
this._messageComposerSetupState.getLatestValue().define(setup);
4403-
4404-
public setMessageComposerApplyModifications = (
4405-
applyModifications: MessageComposerSetupState['applyModifications'],
4390+
public setMessageComposerSetupFunction = (
4391+
setupFunction: MessageComposerSetupState['setupFunction'],
44064392
) => {
4407-
this._messageComposerSetupState.partialNext({ applyModifications });
4393+
this._messageComposerSetupState.partialNext({ setupFunction });
44084394
};
44094395
}

src/messageComposer/messageComposer.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,19 +417,19 @@ export class MessageComposer {
417417
};
418418

419419
private subscribeMessageComposerSetupStateChange = () => {
420-
let cleanupBefore: (() => void) | null = null;
420+
let tearDown: (() => void) | null = null;
421421
const unsubscribe = this.client._messageComposerSetupState.subscribeWithSelector(
422-
({ applyModifications }) => ({
423-
applyModifications,
422+
({ setupFunction: setup }) => ({
423+
setup,
424424
}),
425-
({ applyModifications }) => {
426-
cleanupBefore?.();
427-
cleanupBefore = applyModifications?.({ composer: this }) ?? null;
425+
({ setup }) => {
426+
tearDown?.();
427+
tearDown = setup?.({ composer: this }) ?? null;
428428
},
429429
);
430430

431431
return () => {
432-
cleanupBefore?.();
432+
tearDown?.();
433433
unsubscribe();
434434
};
435435
};

0 commit comments

Comments
 (0)