Skip to content

Commit

Permalink
Revert "fix: queue channel WS events until the channel is initialized (
Browse files Browse the repository at this point in the history
…#1179)"

This reverts commit 2073579
  • Loading branch information
MartinCupela committed Oct 24, 2023
1 parent 97d408d commit 94e6cac
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 131 deletions.
25 changes: 0 additions & 25 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,6 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
lastTypingEvent: Date | null;
isTyping: boolean;
disconnected: boolean;
/**
* Collects the incoming WS events before the channel is marked as initialized.
* This prevents executing procedures that depend on channel being initialized.
* Once the channel is marked as initialized the queue is flushed.
*/
wsEventQueue: Event<StreamChatGenerics>[];

/**
* constructor - Create a channel
Expand Down Expand Up @@ -138,7 +132,6 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
this.lastTypingEvent = null;
this.isTyping = false;
this.disconnected = false;
this.wsEventQueue = [];
}

/**
Expand Down Expand Up @@ -787,7 +780,6 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
const combined = { ...defaultOptions, ...options };
const state = await this.query(combined, 'latest');
this.initialized = true;
this._flushWsEventQueue();
this.data = state.channel;

this._client.logger('info', `channel:watch() - started watching channel ${this.cid}`, {
Expand Down Expand Up @@ -1214,12 +1206,6 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
// eslint-disable-next-line sonarjs/cognitive-complexity
_handleChannelEvent(event: Event<StreamChatGenerics>) {
const channel = this;

if (!this._isInitialized()) {
this.wsEventQueue.push(event);
return;
}

this._client.logger(
'info',
`channel:_handleChannelEvent - Received event of type { ${event.type} } on ${this.cid}`,
Expand Down Expand Up @@ -1455,10 +1441,6 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
return `${this.getClient().baseURL}/channels/${this.type}/${this.id}`;
};

_isInitialized() {
return this.initialized || this.offlineMode || this.getClient()._isUsingServerAuth();
}

_checkInitialized() {
if (!this.initialized && !this.offlineMode && !this.getClient()._isUsingServerAuth()) {
throw Error(
Expand Down Expand Up @@ -1572,11 +1554,4 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
this.disconnected = true;
this.state.setIsUpToDate(false);
}

_flushWsEventQueue() {
while (this.wsEventQueue.length) {
const event = this.wsEventQueue.shift();
if (event) this.getClient().dispatchEvent(event);
}
}
}
25 changes: 0 additions & 25 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,3 @@ export const EVENT_MAP = {
'connection.recovered': true,
'transport.changed': true,
};

// events handled by channel._handleChannelEvent
export const CHANNEL_HANDLED_EVENTS = {
'typing.start': true,
'typing.stop': true,
'message.read': true,
'user.watching.start': true,
'user.updated': true,
'user.watching.stop': true,
'message.deleted': true,
'message.new': true,
'message.updated': true,
'channel.truncated': true,
'member.added': true,
'member.updated': true,
'member.removed': true,
'channel.updated': true,
'reaction.new': true,
'reaction.deleted': true,
'reaction.updated': true,
'channel.hidden': true,
'channel.visible': true,
'user.banned': true,
'user.unbanned': true,
};
83 changes: 3 additions & 80 deletions test/unit/channel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import chai from 'chai';
import sinon from 'sinon';
import { v4 as uuidv4 } from 'uuid';

import { generateChannel } from './test-utils/generateChannel';
Expand All @@ -9,7 +8,8 @@ import { generateUser } from './test-utils/generateUser';
import { getClientWithUser } from './test-utils/getClient';
import { getOrCreateChannelApi } from './test-utils/getOrCreateChannelApi';

import { CHANNEL_HANDLED_EVENTS, ChannelState, StreamChat } from '../../src';
import { StreamChat } from '../../src/client';
import { ChannelState } from '../../src';

const expect = chai.expect;

Expand All @@ -21,13 +21,13 @@ describe('Channel count unread', function () {
beforeEach(() => {
user = { id: 'user' };
lastRead = new Date('2020-01-01T00:00:00');
const channelResponse = generateChannel();

client = new StreamChat('apiKey');
client.user = user;
client.userID = 'user';
client.userMuteStatus = (targetId) => targetId.startsWith('mute');

const channelResponse = generateChannel();
channel = client.channel(channelResponse.channel.type, channelResponse.channel.id);
channel.initialized = true;
channel.lastRead = () => lastRead;
Expand Down Expand Up @@ -532,83 +532,6 @@ describe('Channel _handleChannelEvent', function () {
});
});

describe('Channel WS events buffer', () => {
const user = { id: 'user' };
let client;
let channel;

beforeEach(() => {
client = new StreamChat('apiKey');
client.user = user;
client.userID = user.id;
client.userMuteStatus = (targetId) => targetId.startsWith('mute');
channel = client.channel('messaging', 'id');
channel.initialized = false;
});

const eventTypes = Object.keys(CHANNEL_HANDLED_EVENTS);
const receiveAllChannelEvents = (channel) => {
eventTypes.forEach((type) => {
channel._handleChannelEvent({ type });
});
};

it('when uninitialized', () => {
receiveAllChannelEvents(channel);

expect(channel.wsEventQueue).to.have.length(eventTypes.length);
});

it('does not buffer WS events when in offline mode', () => {
channel.offlineMode = true;

receiveAllChannelEvents(channel);

expect(channel.wsEventQueue).to.be.empty;
});

it('does not buffer WS events with server-side client', () => {
client = new StreamChat('apiKey', 'secret');
client.user = user;
client.userID = user.id;
channel = client.channel('messaging', 'id');

receiveAllChannelEvents(channel);

expect(channel.wsEventQueue).to.be.empty;
});

it('does not buffer WS events on initialized channel', () => {
channel.initialized = true;

receiveAllChannelEvents(channel);

expect(channel.wsEventQueue).to.be.empty;
});

it('buffers WS events and channel.watch() flushes upon channel initialization', async () => {
sinon.stub(client, 'doAxiosRequest').resolves({ channel: generateChannel(), members: [] });

receiveAllChannelEvents(channel);

expect(channel.wsEventQueue).to.have.length(eventTypes.length);
await channel.watch();
expect(channel.wsEventQueue).to.be.empty;
client.doAxiosRequest.restore();
});

it('buffers WS events and channel.query() does not flush the queue', async () => {
sinon.stub(client, 'doAxiosRequest').resolves({ channel: generateChannel(), members: [] });

receiveAllChannelEvents(channel);

expect(channel.wsEventQueue).to.have.length(eventTypes.length);
await channel.query();
expect(channel.wsEventQueue).to.have.length(eventTypes.length);
client.doAxiosRequest.restore();
});
});

describe('Uninitialized Channel', () => {
const user = { id: 'user' };
let client;
Expand Down
1 change: 0 additions & 1 deletion test/unit/channel_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,6 @@ describe('ChannelState clean', () => {
client.userID = 'observer';
channel = new Channel(client, 'live', 'stream', {});
client.activeChannels[channel.cid] = channel;
channel.initialized = true;
});

it('should remove any stale typing events with either string or Date received_at', async () => {
Expand Down

0 comments on commit 94e6cac

Please sign in to comment.