From a4a889bed0ba81f04debfbe8a36960843c767bcd Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Fri, 16 Feb 2018 20:57:46 -0200 Subject: [PATCH] fix performance status users --- android/gradle.properties | 2 +- app/actions/activeUsers.js | 4 ++-- app/lib/rocketchat.js | 32 +++++++++++++++++--------------- app/sagas/activeUsers.js | 25 ++++--------------------- 4 files changed, 24 insertions(+), 39 deletions(-) diff --git a/android/gradle.properties b/android/gradle.properties index 732c56e3e..a86312fd7 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -18,4 +18,4 @@ # org.gradle.parallel=true android.useDeprecatedNdk=true -# VERSIONCODE=999999999 +VERSIONCODE=999999999 diff --git a/app/actions/activeUsers.js b/app/actions/activeUsers.js index ab74a45d9..a273d7348 100644 --- a/app/actions/activeUsers.js +++ b/app/actions/activeUsers.js @@ -1,9 +1,9 @@ import * as types from './actionsTypes'; -export function requestActiveUser(user) { +export function requestActiveUser(users) { return { type: types.ACTIVE_USERS.REQUEST, - user + users }; } diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index 292476bb1..f6fcfe8ed 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -45,21 +45,27 @@ const RocketChat = { throw new Error({ error: 'invalid server' }); }, _setUser(ddpMessage) { - let status; - if (!ddpMessage.fields) { - status = 'offline'; - } else { - status = ddpMessage.fields.status || 'offline'; - } - + this.activeUsers = this.activeUsers || {}; const { user } = reduxStore.getState().login; + + const status = (ddpMessage.fields && ddpMessage.fields.status) || 'offline'; + if (user && user.id === ddpMessage.id) { return reduxStore.dispatch(setUser({ status })); } - const activeUser = {}; - activeUser[ddpMessage.id] = status; - return reduxStore.dispatch(requestActiveUser(activeUser)); + if (this._setUserTimer) { + clearTimeout(this._setUserTimer); + this._setUserTimer = null; + } + + + this._setUserTimer = setTimeout(() => { + reduxStore.dispatch(requestActiveUser(this.activeUsers)); + this._setUserTimer = null; + return this.activeUsers = {}; + }, 1000); + this.activeUsers[ddpMessage.id] = status; }, reconnect() { if (this.ddp) { @@ -94,11 +100,7 @@ const RocketChat = { this.ddp.on('connected', () => this.ddp.subscribe('activeUsers', null, false)); - this.ddp.on('users', (ddpMessage) => { - if (ddpMessage.collection === 'users') { - return RocketChat._setUser(ddpMessage); - } - }); + this.ddp.on('users', ddpMessage => RocketChat._setUser(ddpMessage)); this.ddp.on('stream-room-messages', (ddpMessage) => { const message = this._buildMessage(ddpMessage.fields.args[0]); diff --git a/app/sagas/activeUsers.js b/app/sagas/activeUsers.js index de5c02582..70ca903ef 100644 --- a/app/sagas/activeUsers.js +++ b/app/sagas/activeUsers.js @@ -1,30 +1,13 @@ -import { put, take, race, fork } from 'redux-saga/effects'; -import { delay } from 'redux-saga'; +import { put, takeLatest } from 'redux-saga/effects'; import * as types from '../actions/actionsTypes'; import { setActiveUser } from '../actions/activeUsers'; -const watchActiveUsers = function* handleInput() { - let obj = {}; - while (true) { - const { status, timeout } = yield race({ - status: take(types.ACTIVE_USERS.REQUEST), - timeout: delay(3000) - }); - if (timeout && Object.keys(obj).length > 0) { - yield put(setActiveUser(obj)); - obj = {}; - } - if (status) { - obj = { - ...obj, - ...status.user - }; - } - } +const watchActiveUsers = function* handleInput({ users }) { + yield put(setActiveUser(users)); }; const root = function* root() { - yield fork(watchActiveUsers); + yield takeLatest(types.ACTIVE_USERS.REQUEST, watchActiveUsers); }; export default root;