Skip to content

Commit

Permalink
Extract activity related code into activityManager
Browse files Browse the repository at this point in the history
  • Loading branch information
borisyankov committed Dec 12, 2016
1 parent 9ff1733 commit 8a861c6
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Expand Up @@ -56,7 +56,7 @@
"idx", "aaa", "bbb", "ccc", "ddd", "eee", "fff", "abcd", "aaaaaa", "ffffff", "24cac2",
"src", "attribs", "charset", "utf", "urlencoded", "htmlparser", "htmlparser2",
"chatbubbles", "nonexisting", "primarytext", "autocomple", "stringify", "backend",
"Menlo", "ok", "py", "todo", "Mc",
"Menlo", "ok", "py", "todo", "Mc", "lodash",
],
"skipIfMatch": [
"http://[^s]*",
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -17,6 +17,7 @@
"events": "^1.1.1",
"htmlparser2": "^3.9.2",
"immutable": "^3.8.1",
"lodash.throttle": "^4.1.1",
"moment": "^2.16.0",
"react": "^15.4.1",
"react-addons-shallow-compare": "^15.3.2",
Expand Down
6 changes: 0 additions & 6 deletions src/app/appActions.js
@@ -1,14 +1,8 @@
import { focusPing } from '../api';

import {
APP_ONLINE,
APP_OFFLINE,
} from '../constants';

export const appActivity = (auth) =>
async (dispatch) =>
await focusPing(auth, true, false);

export const appOnline = () => ({
type: APP_ONLINE,
});
Expand Down
3 changes: 2 additions & 1 deletion src/events/eventActions.js
Expand Up @@ -53,6 +53,7 @@ const processEvent = (dispatch, event) => {
});
break;
case 'realm_user':
case 'ream_bot':
case 'subscription':
case 'stream':
case 'pointer':
Expand All @@ -79,7 +80,7 @@ const processEvent = (dispatch, event) => {
}
};

export const getEvents = (auth) =>
export const fetchEvents = (auth) =>
async (dispatch) => {
const data = await registerForEvents(auth);
const queueId = data.queue_id;
Expand Down
12 changes: 6 additions & 6 deletions src/main/MainScreen.js
Expand Up @@ -15,22 +15,22 @@ import UserListContainer from '../userlist/UserListContainer';
export default class MainScreen extends React.Component {

fetchOlder = () => {
const { auth, fetching, narrow, pointer, sendGetMessages } = this.props;
const { auth, fetching, narrow, pointer, fetchMessages } = this.props;
if (!fetching) {
sendGetMessages(auth, pointer[0], 10, 0, narrow);
fetchMessages(auth, pointer[0], 10, 0, narrow);
}
}

fetchNewer = () => {
const { auth, fetching, pointer, narrow, caughtUp, sendGetMessages } = this.props;
const { auth, fetching, pointer, narrow, caughtUp, fetchMessages } = this.props;
if (!fetching && !caughtUp) {
sendGetMessages(auth, pointer[1], 0, 10, narrow);
fetchMessages(auth, pointer[1], 0, 10, narrow);
}
}

narrow = (narrowOperator, pointer: number = Number.MAX_SAFE_INTEGER, messages = []) => {
const { auth, sendGetMessages } = this.props;
sendGetMessages(auth, pointer, 10, 10, narrowOperator || {});
const { auth, fetchMessages } = this.props;
fetchMessages(auth, pointer, 10, 10, narrowOperator || {});
}

render() {
Expand Down
26 changes: 6 additions & 20 deletions src/main/MainScreenContainer.js
@@ -1,10 +1,8 @@
import React from 'react';
import {
AppState,
} from 'react-native';
import { connect } from 'react-redux';

import boundActions from '../boundActions';
import { focusPing } from '../api';
import { getPointer } from '../chat/chatSelectors';
import { getAuth } from '../account/accountSelectors';
import MainScreen from './MainScreen';
Expand All @@ -15,27 +13,15 @@ class MainScreenContainer extends React.Component {
currentAppState: boolean,
}

handleAppStateChange = (currentAppState) => {
if (currentAppState === 'active') {
this.props.appActivity();
}
}

componentDidMount() {
AppState.addEventListener('change', this.handleAppStateChange);

const {
auth, narrow, getEvents, sendInitialGetUsers, appActivity, sendGetMessages,
auth, narrow, fetchEvents, fetchUsersAndStatus, fetchMessages,
} = this.props;

getEvents(auth);
sendInitialGetUsers(auth);
appActivity(auth);
sendGetMessages(auth, Number.MAX_SAFE_INTEGER, 10, 10, narrow);
}

componentWillUnmount() {
AppState.removeEventListener('change', this.handleAppStateChange);
fetchUsersAndStatus(auth);
fetchMessages(auth, Number.MAX_SAFE_INTEGER, 10, 10, narrow);
fetchEvents(auth);
focusPing(auth, true, false);
}

render() {
Expand Down
5 changes: 3 additions & 2 deletions src/message-list/messagesActions.js
@@ -1,10 +1,11 @@
import { Auth } from '../api/apiFetch';
import { getMessages } from '../api';
import {
CHAT_FETCHING_MESSAGES,
CHAT_FETCHED_MESSAGES,
} from '../constants';

export const sendGetMessages = (
export const fetchMessages = (
auth,
anchor: number,
numBefore: number,
Expand All @@ -15,7 +16,7 @@ export const sendGetMessages = (
dispatch({ type: CHAT_FETCHING_MESSAGES });

const messages = await getMessages(
auth,
auth: Auth,
anchor: number,
numBefore: number,
numAfter: number,
Expand Down
6 changes: 3 additions & 3 deletions src/userlist/userListActions.js
Expand Up @@ -13,7 +13,7 @@ export const sendFocusPing = (auth, hasFocus: boolean, newUserInput: boolean) =>
});
};

export const sendGetUsers = (auth) =>
export const fetchUsers = (auth) =>
async (dispatch) => {
const response = await getUsers(auth);
dispatch({
Expand All @@ -22,8 +22,8 @@ export const sendGetUsers = (auth) =>
});
};

export const sendInitialGetUsers = (auth) =>
export const fetchUsersAndStatus = (auth) =>
async (dispatch) => {
await dispatch(sendGetUsers(auth));
await dispatch(fetchUsers(auth));
await dispatch(sendFocusPing(auth, true, false));
};
41 changes: 41 additions & 0 deletions src/utils/activityManager.js
@@ -0,0 +1,41 @@
import {
AppState,
} from 'react-native';
import throttle from 'lodash.throttle';

import { focusPing } from '../api';
import { Auth } from '../api/apiFetch';

// Send once per minute.
// Send when restored from suspended state
// Send on new compose message
// Send on new narrow event

export default class ActivityManager {

auth: Auth;

constructor(auth: Auth) {
this.auth = auth;

AppState.addEventListener('change', this.handleAppStateChange);
}

// componentWillUnmount() {
// AppState.removeEventListener('change', this.handleAppStateChange);
// }

appActivity() {
throttle(() => focusPing(this.props.auth, true, false), 1000 * 60);
}

userInputActivity() {
throttle(() => focusPing(this.props.auth, true, false), 1000 * 5);
}

handleAppStateChange = (currentAppState) => {
if (currentAppState === 'active') {
this.appActivity();
}
}
}
16 changes: 10 additions & 6 deletions yarn.lock
Expand Up @@ -55,8 +55,8 @@ ajv-keywords@^1.0.0:
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-1.2.0.tgz#676c4f087bfe1e8b12dca6fda2f3c74f417b099c"

ajv@^4.7.0:
version "4.9.0"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-4.9.0.tgz#5a358085747b134eb567d6d15e015f1d7802f45c"
version "4.9.1"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-4.9.1.tgz#08e1b0a5fddc8b844d28ca7b03510e78812ee3a0"
dependencies:
co "^4.6.0"
json-stable-stringify "^1.0.1"
Expand Down Expand Up @@ -3560,6 +3560,10 @@ lodash.templatesettings@^3.0.0:
lodash._reinterpolate "^3.0.0"
lodash.escape "^3.0.0"

lodash.throttle@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/lodash.throttle/-/lodash.throttle-4.1.1.tgz#c23e91b710242ac70c37f1e1cda9274cc39bf2f4"

lodash@^3.1.0, lodash@^3.2.0, lodash@^3.3.1, lodash@^3.5.0, lodash@^3.6.0:
version "3.10.1"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"
Expand Down Expand Up @@ -3774,8 +3778,8 @@ mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@0.5.x, mkdirp@0.x.x:
minimist "0.0.8"

moment@^2.16.0, moment@2.x.x:
version "2.17.0"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.17.0.tgz#a4c292e02aac5ddefb29a6eed24f51938dd3b74f"
version "2.17.1"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.17.1.tgz#fed9506063f36b10f066c8b59a144d7faebe1d82"

morgan@~1.6.1:
version "1.6.1"
Expand Down Expand Up @@ -4817,8 +4821,8 @@ shellwords@^0.1.0:
resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.0.tgz#66afd47b6a12932d9071cbfd98a52e785cd0ba14"

signal-exit@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.1.tgz#5a4c884992b63a7acd9badb7894c3ee9cfccad81"
version "3.0.2"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"

simple-plist@0.1.4:
version "0.1.4"
Expand Down

0 comments on commit 8a861c6

Please sign in to comment.