From 39138b5b520b4e93060c4baf1d127ced6db139c0 Mon Sep 17 00:00:00 2001 From: Gabriel Engel Date: Mon, 8 Jun 2015 11:29:01 -0300 Subject: [PATCH] several performance improvements --- client/lib/RoomManager.coffee | 43 +++++++++++---------- client/views/app/chatWindowDashboard.coffee | 6 ++- server/methods/addUserToRoom.coffee | 10 +++-- server/methods/canAccessRoom.coffee | 1 - server/methods/createPrivateGroup.coffee | 28 ++++++++------ server/methods/sendMessage.coffee | 6 +-- server/publications/room.coffee | 2 +- 7 files changed, 54 insertions(+), 42 deletions(-) diff --git a/client/lib/RoomManager.coffee b/client/lib/RoomManager.coffee index 38b1182ae28..c42f123a506 100644 --- a/client/lib/RoomManager.coffee +++ b/client/lib/RoomManager.coffee @@ -9,23 +9,23 @@ subscription = Meteor.subscribe('subscription') return subscription - expireRoom = (roomId) -> - if openedRooms[roomId] - if openedRooms[roomId].sub? - for sub in openedRooms[roomId].sub + expireRoom = (rid) -> + if openedRooms[rid] + if openedRooms[rid].sub? + for sub in openedRooms[rid].sub sub.stop() - openedRooms[roomId].ready = false - openedRooms[roomId].active = false - delete openedRooms[roomId].timeout + openedRooms[rid].ready = false + openedRooms[rid].active = false + delete openedRooms[rid].timeout - ChatMessageHistory.remove rid: roomId + ChatMessageHistory.remove rid: rid computation = Tracker.autorun -> - for roomId, record of openedRooms when record.active is true + for rid, record of openedRooms when record.active is true record.sub = [ - Meteor.subscribe 'room', roomId - Meteor.subscribe 'messages', roomId + Meteor.subscribe 'room', rid + Meteor.subscribe 'messages', rid ] record.ready = record.sub[0].ready() and record.sub[1].ready() @@ -38,26 +38,27 @@ clearTimeout openedRooms[except].timeout delete openedRooms[except].timeout - for roomId of openedRooms - if roomId isnt except and not openedRooms[roomId].timeout? - openedRooms[roomId].timeout = setTimeout expireRoom, defaultTime, roomId + for rid of openedRooms + if rid isnt except and not openedRooms[rid].timeout? + openedRooms[rid].timeout = setTimeout expireRoom, defaultTime, rid - open = (roomId) -> - if not openedRooms[roomId]? - openedRooms[roomId] = + open = (rid) -> + if not openedRooms[rid]? + openedRooms[rid] = active: false ready: false if subscription.ready() - if ChatSubscription.findOne { rid: roomId }, { reactive: false } - openedRooms[roomId].active = true - setRoomExpireExcept roomId + # if ChatSubscription.findOne { rid: rid }, { reactive: false } + if openedRooms[rid].active isnt true + openedRooms[rid].active = true + setRoomExpireExcept rid computation.invalidate() return { ready: -> Dep.depend() - return openedRooms[roomId].ready + return openedRooms[rid].ready } open: open diff --git a/client/views/app/chatWindowDashboard.coffee b/client/views/app/chatWindowDashboard.coffee index 9555f5c86db..a3755758bd1 100644 --- a/client/views/app/chatWindowDashboard.coffee +++ b/client/views/app/chatWindowDashboard.coffee @@ -95,7 +95,11 @@ Template.chatWindowDashboard.helpers console.log 'chatWindowDashboard.roomName' if window.rocketDebug roomData = Session.get('roomData' + this._id) return '' unless roomData - return roomData.name + + if roomData.t is 'd' + return ChatSubscription.findOne({ rid: this._id }, { fields: { name: 1 } }).name + else + return roomData.name roomTypeIcon: -> console.log 'chatWindowDashboard.roomType' if window.rocketDebug diff --git a/server/methods/addUserToRoom.coffee b/server/methods/addUserToRoom.coffee index bb75f3c81de..dd6dff9f616 100644 --- a/server/methods/addUserToRoom.coffee +++ b/server/methods/addUserToRoom.coffee @@ -12,6 +12,8 @@ Meteor.methods if room.usernames.indexOf(data.username) isnt -1 return + now = new Date() + update = $push: usernames: @@ -24,17 +26,19 @@ Meteor.methods ChatSubscription.insert rid: data.rid - ts: (new Date()) + ts: now name: room.name t: room.t - unread: 0 + open: true + alert: true + unread: 1 u: _id: newUser._id username: data.username ChatMessage.insert rid: data.rid - ts: (new Date) + ts: now t: 'au' msg: newUser.name u: diff --git a/server/methods/canAccessRoom.coffee b/server/methods/canAccessRoom.coffee index 563bbe7202b..54a5bf82236 100644 --- a/server/methods/canAccessRoom.coffee +++ b/server/methods/canAccessRoom.coffee @@ -19,7 +19,6 @@ Meteor.methods canAccess = true if canAccess isnt true - throw new Meteor.Error 'without-permission', "[methods] canAccessRoom -> User doesn't have enough permissions" return false else return room diff --git a/server/methods/createPrivateGroup.coffee b/server/methods/createPrivateGroup.coffee index de0b1aeffeb..a6e5c5b5e62 100644 --- a/server/methods/createPrivateGroup.coffee +++ b/server/methods/createPrivateGroup.coffee @@ -7,7 +7,9 @@ Meteor.methods now = new Date() - members.push Meteor.user().username + me = Meteor.user() + + members.push me.username name = s.slugify name @@ -17,30 +19,32 @@ Meteor.methods ts: now t: 'p' u: - _id: Meteor.userId() - username: Meteor.user().username + _id: me._id + username: me.username name: name msgs: 0 for username in members - member = Meteor.users.findOne({username: username}) + member = Meteor.users.findOne({ username: username },{ fields: { username: 1 }}) if not member? continue - sub = - u: - _id: member._id - username: username + subscription = rid: rid ts: now name: name t: 'p' - unread: 0 + open: true + u: + _id: member._id + username: member.username - if username is Meteor.user().username - sub.ls = now + if username is me.username + subscription.ls = now + else + subscription.alert = true - ChatSubscription.insert sub + ChatSubscription.insert subscription return { rid: rid diff --git a/server/methods/sendMessage.coffee b/server/methods/sendMessage.coffee index b6c0a384ecc..0dcabbc4e53 100644 --- a/server/methods/sendMessage.coffee +++ b/server/methods/sendMessage.coffee @@ -105,9 +105,9 @@ Meteor.methods Save the message. If there was already a typing record, update it. ### ChatMessage.upsert - rid: message.rid - t: 't' - $and: [{ 'u._id': message.u._id }] + rid: message.rid + t: 't' + $and: [{ 'u._id': message.u._id }] , $set: message $unset: diff --git a/server/publications/room.coffee b/server/publications/room.coffee index b630cb3351e..99a9c1434c0 100644 --- a/server/publications/room.coffee +++ b/server/publications/room.coffee @@ -2,7 +2,7 @@ Meteor.publish 'room', (rid) -> unless this.userId return this.ready() - console.log '[publish] room ->'.green, 'rid:' + console.log '[publish] room ->'.green, 'arguments:', arguments if typeof rid isnt 'string' return this.ready()