Skip to content

Commit

Permalink
made chat service evented
Browse files Browse the repository at this point in the history
  • Loading branch information
roncodes committed Apr 3, 2024
1 parent 1198f4a commit 6fc4a8f
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions addon/services/chat.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import Service, { inject as service } from '@ember/service';
import Evented from '@ember/object/evented';
import { tracked } from '@glimmer/tracking';
import { isArray } from '@ember/array';
import { task } from 'ember-concurrency';

export default class ChatService extends Service {
export default class ChatService extends Service.extend(Evented) {
@service store;
@service currentUser;
@tracked channels = [];
@tracked openChannels = [];

openChannel(chatChannelRecord) {
this.openChannels.pushObject(chatChannelRecord);
this.trigger('chat.opened', chatChannelRecord);
}

closeChannel(chatChannelRecord) {
const index = this.openChannels.findIndex((_) => _.id === chatChannelRecord.id);
if (index >= 0) {
this.openChannels.removeAt(index);
this.trigger('chat.closed', chatChannelRecord);
}
}

Expand All @@ -26,28 +29,38 @@ export default class ChatService extends Service {

createChatChannel(name) {
const chatChannel = this.store.createRecord('chat-channel', { name });
return chatChannel.save();
return chatChannel.save().finally(() => {
this.trigger('chat.created', chatChannelRecord);
});
}

deleteChatChannel(chatChannelRecord) {
return chatChannelRecord.destroyRecord();
return chatChannelRecord.destroyRecord().finally(() => {
this.trigger('chat.deleted', chatChannelRecord);
});
}

updateChatChannel(chatChannelRecord, props = {}) {
chatChannelRecord.setProperties(props);
return chatChannelRecord.save();
return chatChannelRecord.save().finally(() => {
this.trigger('chat.updated', chatChannelRecord);
});
}

addParticipant(chatChannelRecord, userRecord) {
const chatParticipant = this.store.createRecord('chat-participant', {
chat_channel_uuid: chatChannelRecord.id,
user_uuid: userRecord.id,
});
return chatParticipant.save();
return chatParticipant.save().finally(() => {
this.trigger('chat.added_participant', chatChannelRecord, chatParticipant);
});
}

removeParticipant(chatParticipant) {
return chatParticipant.destroyRecord();
return chatParticipant.destroyRecord().finally(() => {
this.trigger('chat.removed_participant', chatChannelRecord, chatParticipant);
});
}

async sendMessage(chatChannelRecord, senderRecord, messageContent = '') {
Expand All @@ -56,13 +69,17 @@ export default class ChatService extends Service {
sender_uuid: senderRecord.id,
content: messageContent,
});
await chatMessage.save();
await chatMessage.save().finally(() => {
this.trigger('chat.message_created', chatMessage, chatChannelRecord);
});
chatChannelRecord.messages.pushObject(chatMessage);
await this.loadMessages.perform(chatChannelRecord);
}

deleteMessage(chatMessageRecord) {
return chatMessageRecord.destroyRecord();
return chatMessageRecord.destroyRecord().finally(() => {
this.trigger('chat.message_deleted', chatMessageRecord);
});
}

@task *loadMessages(chatChannelRecord) {
Expand Down

0 comments on commit 6fc4a8f

Please sign in to comment.