Active users throttle

This commit is contained in:
Diego Mello 2017-12-08 15:52:18 -02:00
parent 53b7e5c5cb
commit 069bad4a99
5 changed files with 45 additions and 7 deletions

View File

@ -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';

View File

@ -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
};
}

View File

@ -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) => {

30
app/sagas/activeUsers.js Normal file
View File

@ -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;

View File

@ -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()
]);
};