fix performance status users

This commit is contained in:
Guilherme Gazzo 2018-02-16 20:57:46 -02:00
parent 77d7f51f30
commit a4a889bed0
No known key found for this signature in database
GPG Key ID: 1F85C9AD922D0829
4 changed files with 24 additions and 39 deletions

View File

@ -18,4 +18,4 @@
# org.gradle.parallel=true # org.gradle.parallel=true
android.useDeprecatedNdk=true android.useDeprecatedNdk=true
# VERSIONCODE=999999999 VERSIONCODE=999999999

View File

@ -1,9 +1,9 @@
import * as types from './actionsTypes'; import * as types from './actionsTypes';
export function requestActiveUser(user) { export function requestActiveUser(users) {
return { return {
type: types.ACTIVE_USERS.REQUEST, type: types.ACTIVE_USERS.REQUEST,
user users
}; };
} }

View File

@ -45,21 +45,27 @@ const RocketChat = {
throw new Error({ error: 'invalid server' }); throw new Error({ error: 'invalid server' });
}, },
_setUser(ddpMessage) { _setUser(ddpMessage) {
let status; this.activeUsers = this.activeUsers || {};
if (!ddpMessage.fields) {
status = 'offline';
} else {
status = ddpMessage.fields.status || 'offline';
}
const { user } = reduxStore.getState().login; const { user } = reduxStore.getState().login;
const status = (ddpMessage.fields && ddpMessage.fields.status) || 'offline';
if (user && user.id === ddpMessage.id) { if (user && user.id === ddpMessage.id) {
return reduxStore.dispatch(setUser({ status })); return reduxStore.dispatch(setUser({ status }));
} }
const activeUser = {}; if (this._setUserTimer) {
activeUser[ddpMessage.id] = status; clearTimeout(this._setUserTimer);
return reduxStore.dispatch(requestActiveUser(activeUser)); this._setUserTimer = null;
}
this._setUserTimer = setTimeout(() => {
reduxStore.dispatch(requestActiveUser(this.activeUsers));
this._setUserTimer = null;
return this.activeUsers = {};
}, 1000);
this.activeUsers[ddpMessage.id] = status;
}, },
reconnect() { reconnect() {
if (this.ddp) { if (this.ddp) {
@ -94,11 +100,7 @@ const RocketChat = {
this.ddp.on('connected', () => this.ddp.subscribe('activeUsers', null, false)); this.ddp.on('connected', () => this.ddp.subscribe('activeUsers', null, false));
this.ddp.on('users', (ddpMessage) => { this.ddp.on('users', ddpMessage => RocketChat._setUser(ddpMessage));
if (ddpMessage.collection === 'users') {
return RocketChat._setUser(ddpMessage);
}
});
this.ddp.on('stream-room-messages', (ddpMessage) => { this.ddp.on('stream-room-messages', (ddpMessage) => {
const message = this._buildMessage(ddpMessage.fields.args[0]); const message = this._buildMessage(ddpMessage.fields.args[0]);

View File

@ -1,30 +1,13 @@
import { put, take, race, fork } from 'redux-saga/effects'; import { put, takeLatest } from 'redux-saga/effects';
import { delay } from 'redux-saga';
import * as types from '../actions/actionsTypes'; import * as types from '../actions/actionsTypes';
import { setActiveUser } from '../actions/activeUsers'; import { setActiveUser } from '../actions/activeUsers';
const watchActiveUsers = function* handleInput() { const watchActiveUsers = function* handleInput({ users }) {
let obj = {}; yield put(setActiveUser(users));
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 root = function* root() { const root = function* root() {
yield fork(watchActiveUsers); yield takeLatest(types.ACTIVE_USERS.REQUEST, watchActiveUsers);
}; };
export default root; export default root;