Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CJS-5: Remove immutable #602

Merged
merged 24 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
550912a
removing immutables step 1
virdesai Jan 19, 2021
34e6508
update type test
nhannah Jan 19, 2021
8f77e30
small logic update
virdesai Jan 19, 2021
6b0318b
small changes
virdesai Jan 19, 2021
2c00979
Merge branch 'master' of https://github.com/GetStream/stream-chat-js …
virdesai Jan 20, 2021
bf40ae7
Merge branch 'master' of https://github.com/GetStream/stream-chat-js …
virdesai Jan 20, 2021
b8f6281
change 1 character variable to an actual name to follow best practice…
virdesai Jan 20, 2021
e48c2d0
Merge branch 'master' of https://github.com/GetStream/stream-chat-js …
virdesai Jan 21, 2021
e3e8f0d
Merge branch 'master' of https://github.com/GetStream/stream-chat-js …
virdesai Jan 22, 2021
cdb624c
Merge branch 'master' of https://github.com/GetStream/stream-chat-js …
virdesai Jan 25, 2021
e7b2810
minor changes
virdesai Jan 26, 2021
4ec0b81
Merge branch 'master' of https://github.com/GetStream/stream-chat-js …
virdesai Jan 28, 2021
0850bbe
delete in place
virdesai Jan 29, 2021
19f274a
Merge branch 'master' of https://github.com/GetStream/stream-chat-js …
virdesai Jan 29, 2021
ae685e7
PR updates
virdesai Feb 1, 2021
e301a1f
type cast
virdesai Feb 1, 2021
7fa80c0
3.0.0-beta-0
mahboubii Feb 1, 2021
acf322f
3.0.0-beta.0
mahboubii Feb 1, 2021
39d1006
adding types for translations
virdesai Feb 1, 2021
422684c
remove comment
virdesai Feb 1, 2021
e0a419b
type fix
virdesai Feb 1, 2021
7515f35
Merge pull request #607 from GetStream/translation_types
virdesai Feb 1, 2021
e27827b
Merge branch 'master' of https://github.com/GetStream/stream-chat-js …
virdesai Feb 4, 2021
2deeb79
PR updates and spelling fixes
virdesai Feb 5, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stream-chat",
"version": "2.12.0",
"version": "3.0.0-beta.0",
"description": "JS SDK for the Stream Chat API",
"author": "GetStream",
"homepage": "https://getstream.io/chat/",
Expand Down Expand Up @@ -37,14 +37,12 @@
"dependencies": {
"@babel/runtime": "^7.12.5",
"@types/jsonwebtoken": "^8.5.0",
"@types/seamless-immutable": "7.1.13",
"@types/ws": "^7.4.0",
"axios": "^0.21.1",
"base64-js": "^1.5.1",
"form-data": "^3.0.0",
"isomorphic-ws": "^4.0.1",
"jsonwebtoken": "^8.5.1",
"seamless-immutable": "^7.1.4",
"ws": "^7.4.2"
},
"devDependencies": {
Expand Down
1 change: 0 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const externalPackages = [
'axios',
'form-data',
'isomorphic-ws',
'seamless-immutable',
'base64-js',
/@babel\/runtime/,
];
Expand Down
154 changes: 66 additions & 88 deletions src/channel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Immutable from 'seamless-immutable';
import { ChannelState } from './channel_state';
import { isValidEventType } from './events';
import { logChatPromiseExecution, normalizeQuerySort } from './utils';
Expand Down Expand Up @@ -67,7 +66,6 @@ export class Channel<
data:
| ChannelData<ChannelType>
| ChannelResponse<ChannelType, CommandType, UserType>
| Immutable.Immutable<ChannelResponse<ChannelType, CommandType, UserType>>
| undefined;
_data: ChannelData<ChannelType> | ChannelResponse<ChannelType, CommandType, UserType>;
cid: string;
Expand Down Expand Up @@ -488,10 +486,7 @@ export class Channel<
async update(
channelData:
| Partial<ChannelData<ChannelType>>
| Partial<ChannelResponse<ChannelType, CommandType, UserType>>
| Partial<
Immutable.Immutable<ChannelResponse<ChannelType, CommandType, UserType>>
> = {},
| Partial<ChannelResponse<ChannelType, CommandType, UserType>> = {},
updateMessage?: Message<AttachmentType, MessageType, UserType>,
) {
// Strip out reserved names that will result in API errors.
Expand Down Expand Up @@ -933,7 +928,7 @@ export class Channel<
/**
* lastMessage - return the last message, takes into account that last few messages might not be perfectly sorted
*
* @return {Immutable.Immutable<ReturnType<ChannelState<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>['messageToImmutable']>> | undefined} Description
* @return {ReturnType<ChannelState<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>['formatMessage']> | undefined} Description
*/
lastMessage() {
// get last 5 messages, sort, return the latest
Expand All @@ -943,16 +938,12 @@ export class Channel<
min = 0;
}
const max = this.state.messages.length + 1;
const messageSlice = this.state.messages.slice(min, max).asMutable();
const messageSlice = this.state.messages.slice(min, max);

// sort by pk desc
messageSlice.sort((a, b) => b.created_at.getTime() - a.created_at.getTime());

let lastMessage;
if (messageSlice.length > 0) {
lastMessage = messageSlice[0];
}
return lastMessage;
return messageSlice[0];
}

/**
Expand Down Expand Up @@ -1132,7 +1123,7 @@ export class Channel<

/**
* lastRead - returns the last time the user marked the channel as read if the user never marked the channel as read, this will return null
* @return {Immutable.ImmutableDate | null | undefined}
* @return {Date | null | undefined}
*/
lastRead() {
this._checkInitialized();
Expand All @@ -1159,11 +1150,11 @@ export class Channel<
/**
* countUnread - Count of unread messages
*
* @param {Date | Immutable.ImmutableDate | null} [lastRead] lastRead the time that the user read a message, defaults to current user's read state
* @param {Date | null} [lastRead] lastRead the time that the user read a message, defaults to current user's read state
*
* @return {number} Unread count
*/
countUnread(lastRead?: Date | Immutable.ImmutableDate | null) {
countUnread(lastRead?: Date | null) {
if (!lastRead) return this.state.unreadCount;

let count = 0;
Expand Down Expand Up @@ -1191,7 +1182,7 @@ export class Channel<
if (
this._countMessageAsUnread(message) &&
(!lastRead || message.created_at > lastRead) &&
message.mentioned_users?.find((u) => u.id === userID)
message.mentioned_users?.some((user) => user.id === userID)
) {
count++;
}
Expand Down Expand Up @@ -1251,8 +1242,8 @@ export class Channel<
// set the channel as active...

const membersStr = state.members
.map((m) => m.user_id)
?.sort()
.map((member) => member.user_id)
.sort()
.join(',');
const tempChannelCid = `${this.type}:!members-${membersStr}`;

Expand Down Expand Up @@ -1530,47 +1521,48 @@ export class Channel<
},
);

const s = channel.state;
const channelState = channel.state;
switch (event.type) {
case 'typing.start':
if (event.user?.id) {
s.typing = s.typing.set(event.user.id, Immutable(event));
channelState.typing[event.user.id] = event;
}
break;
case 'typing.stop':
if (event.user?.id) {
s.typing = s.typing.without(event.user.id);
delete channelState.typing[event.user.id];
}
break;
case 'message.read':
if (event.user?.id) {
s.read = s.read.set(
event.user.id,
Immutable({ user: { ...event.user }, last_read: event.received_at }),
);
channelState.read[event.user.id] = {
// because in client.ts the handleEvent call that flows to this sets this `event.received_at = new Date();`
last_read: event.received_at as Date,
user: event.user,
};

if (event.user?.id === this.getClient().user?.id) {
s.unreadCount = 0;
channelState.unreadCount = 0;
}
}
break;
case 'user.watching.start':
case 'user.updated':
if (event.user?.id) {
s.watchers = s.watchers.set(event.user.id, Immutable(event.user));
channelState.watchers[event.user.id] = event.user;
}
break;
case 'user.watching.stop':
if (event.user?.id) {
s.watchers = s.watchers.without(event.user.id);
delete channelState.watchers[event.user.id];
}
break;
case 'message.deleted':
if (event.message) {
if (event.hard_delete) s.removeMessage(event.message);
else s.addMessageSorted(event.message);
if (event.hard_delete) channelState.removeMessage(event.message);
else channelState.addMessageSorted(event.message);
if (event.message.pinned) {
s.removePinnedMessage(event.message);
channelState.removePinnedMessage(event.message);
}
}
break;
Expand All @@ -1582,75 +1574,72 @@ export class Channel<
event.message.parent_id && !event.message.show_in_channel;

if (this.state.isUpToDate || isThreadMessage) {
s.addMessageSorted(event.message, ownMessage);
channelState.addMessageSorted(event.message, ownMessage);
}
if (event.message.pinned) {
s.addPinnedMessage(event.message);
channelState.addPinnedMessage(event.message);
}

if (ownMessage && event.user?.id) {
s.unreadCount = 0;
s.read = s.read.set(
event.user.id,
Immutable({
user: { ...event.user },
last_read: new Date(event.created_at as string),
}),
);
channelState.unreadCount = 0;
channelState.read[event.user.id] = {
last_read: new Date(event.created_at as string),
user: event.user,
};
} else if (this._countMessageAsUnread(event.message)) {
s.unreadCount = s.unreadCount + 1;
channelState.unreadCount = channelState.unreadCount + 1;
}
}
break;
case 'message.updated':
if (event.message) {
s.addMessageSorted(event.message);
channelState.addMessageSorted(event.message);
if (event.message.pinned) {
s.addPinnedMessage(event.message);
channelState.addPinnedMessage(event.message);
} else {
s.removePinnedMessage(event.message);
channelState.removePinnedMessage(event.message);
}
}
break;
case 'channel.truncated':
s.clearMessages();
s.unreadCount = 0;
channelState.clearMessages();
channelState.unreadCount = 0;
break;
case 'member.added':
case 'member.updated':
if (event.member?.user_id) {
s.members = s.members.set(event.member?.user_id, Immutable(event.member));
channelState.members[event.member.user_id] = event.member;
}
break;
case 'member.removed':
if (event.user?.id) {
s.members = s.members.without(event.user.id);
delete channelState.members[event.user.id];
}
break;
case 'channel.updated':
if (event.channel) {
channel.data = Immutable(event.channel);
channel.data = event.channel;
}
break;
case 'reaction.new':
if (event.reaction) {
s.addReaction(event.reaction, event.message);
channelState.addReaction(event.reaction, event.message);
}
break;
case 'reaction.deleted':
if (event.reaction) {
s.removeReaction(event.reaction, event.message);
channelState.removeReaction(event.reaction, event.message);
}
break;
case 'reaction.updated':
if (event.reaction) {
// assuming reaction.updated is only called if enforce_unique is true
s.addReaction(event.reaction, event.message, true);
channelState.addReaction(event.reaction, event.message, true);
}
break;
case 'channel.hidden':
if (event.clear_history) {
s.clearMessages();
channelState.clearMessages();
}
break;
default:
Expand Down Expand Up @@ -1722,78 +1711,67 @@ export class Channel<
UserType
>,
) {
const { state: clientState, user, userID } = this.getClient();

// add the Users
if (state.members) {
for (const m of state.members) {
if (m.user) {
this.getClient().state.updateUserReference(m.user, this.cid);
for (const member of state.members) {
if (member.user) {
clientState.updateUserReference(member.user, this.cid);
}
}
}

this.state.membership = Immutable(state.membership ? state.membership : {});

// TODO: CHECK WATCHERS TYPE!!!!!!
// if (state.watchers) {
// for (const watcher of state.watchers) {
// if (watcher) {
// this.getClient().state.updateUserReference(watcher, this.cid);
// }
// }
// }
this.state.membership = state.membership || {};

// immutable list of maps
const messages = state.messages || [];
if (!this.state.messages) {
this.state.messages = Immutable([]);
this.state.messages = [];
}
this.state.addMessagesSorted(messages, false, true);
if (!this.state.pinnedMessages) {
this.state.pinnedMessages = Immutable([]);
this.state.pinnedMessages = [];
}
this.state.addPinnedMessages(state.pinned_messages || []);
this.state.watcher_count = state.watcher_count ? state.watcher_count : 0;
this.state.watcher_count = state.watcher_count || 0;
// convert the arrays into objects for easier syncing...
if (state.watchers) {
for (const watcher of state.watchers) {
if (watcher) {
this.getClient().state.updateUserReference(watcher, this.cid);
this.state.watchers = this.state.watchers.set(watcher.id, watcher);
clientState.updateUserReference(watcher, this.cid);
this.state.watchers[watcher.id] = watcher;
}
}
}

// initialize read state to last message or current time if the channel is empty
// if the user is a member, this value will be overwritten later on otherwise this ensures
// that everything up to this point is not marked as unread
if (this.getClient().userID != null) {
const last_read =
this.state.last_message_at != null ? this.state.last_message_at : new Date();
const { user } = this.getClient();
if (userID != null) {
const last_read = this.state.last_message_at || new Date();
if (user) {
this.state.read = this.state.read.set(user.id, {
user: this.getClient().user,
this.state.read[user.id] = {
user,
last_read,
});
};
}
}

// apply read state if part of the state
if (state.read) {
for (const read of state.read) {
const parsedRead = Object.assign({ ...read });
parsedRead.last_read = new Date(read.last_read);
this.state.read = this.state.read.set(read.user.id, parsedRead);
if (read.user.id === this.getClient().user?.id) {
const parsedRead = { ...read, last_read: new Date(read.last_read) };
this.state.read[read.user.id] = parsedRead;
if (read.user.id === user?.id && typeof parsedRead.unread_messages === 'number') {
this.state.unreadCount = parsedRead.unread_messages;
}
}
}

if (state.members) {
for (const m of state.members) {
if (m.user) {
this.state.members = this.state.members.set(m.user.id, m);
for (const member of state.members) {
if (member.user) {
this.state.members[member.user.id] = member;
}
}
}
Expand Down
Loading