diff --git a/app/actions/actionsTypes.js b/app/actions/actionsTypes.js index 2b6d447cd..454d8cbdc 100644 --- a/app/actions/actionsTypes.js +++ b/app/actions/actionsTypes.js @@ -75,7 +75,7 @@ export const SERVER = createRequestTypes('SERVER', [ ]); export const METEOR = createRequestTypes('METEOR_CONNECT', [...defaultTypes, 'DISCONNECT']); export const LOGOUT = 'LOGOUT'; // logout is always success -export const ACTIVE_USERS = createRequestTypes('ACTIVE_USERS', ['SET']); +export const ACTIVE_USERS = createRequestTypes('ACTIVE_USERS', ['SET', 'REQUEST']); export const INCREMENT = 'INCREMENT'; export const DECREMENT = 'DECREMENT'; diff --git a/app/actions/activeUsers.js b/app/actions/activeUsers.js index 16c14d50c..ab74a45d9 100644 --- a/app/actions/activeUsers.js +++ b/app/actions/activeUsers.js @@ -1,9 +1,15 @@ import * as types from './actionsTypes'; - -export function setActiveUser(user) { +export function requestActiveUser(user) { return { - type: types.ACTIVE_USERS.SET, + type: types.ACTIVE_USERS.REQUEST, user }; } + +export function setActiveUser(data) { + return { + type: types.ACTIVE_USERS.SET, + data + }; +} diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index a5f193558..7a9de3d8c 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -11,7 +11,7 @@ import * as actions from '../actions'; import { someoneTyping } from '../actions/room'; import { setUser } from '../actions/login'; import { disconnect, connectSuccess } from '../actions/connect'; -import { setActiveUser } from '../actions/activeUsers'; +import { requestActiveUser } from '../actions/activeUsers'; export { Accounts } from 'react-native-meteor'; @@ -62,7 +62,7 @@ const RocketChat = { const activeUser = {}; activeUser[ddpMessage.id] = status; - return reduxStore.dispatch(setActiveUser(activeUser)); + return reduxStore.dispatch(requestActiveUser(activeUser)); }, connect(_url) { return new Promise((resolve) => { diff --git a/app/sagas/activeUsers.js b/app/sagas/activeUsers.js new file mode 100644 index 000000000..5b7fc2ded --- /dev/null +++ b/app/sagas/activeUsers.js @@ -0,0 +1,30 @@ +import { put, take, race, fork } from 'redux-saga/effects'; +import { delay } from 'redux-saga'; +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) { + yield put(setActiveUser(obj)); + obj = {}; + } + if (status) { + obj = { + ...obj, + ...status.user + }; + } + } +}; + +const root = function* root() { + yield fork(watchActiveUsers); +}; +export default root; diff --git a/app/sagas/index.js b/app/sagas/index.js index 068401c54..a27f59a54 100644 --- a/app/sagas/index.js +++ b/app/sagas/index.js @@ -8,6 +8,7 @@ import selectServer from './selectServer'; import createChannel from './createChannel'; import init from './init'; import state from './state'; +import activeUsers from './activeUsers'; const root = function* root() { yield all([ @@ -19,7 +20,8 @@ const root = function* root() { connect(), messages(), selectServer(), - state() + state(), + activeUsers() ]); };