Skip to content

Commit

Permalink
Refactor loadFollowing/loadFollows
Browse files Browse the repository at this point in the history
  • Loading branch information
svk31 committed Aug 3, 2016
1 parent 1f1361d commit 0b7fbe9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 43 deletions.
4 changes: 2 additions & 2 deletions app/redux/FetchDataSaga.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {takeLatest, takeEvery} from 'redux-saga';
import {call, put, select, fork} from 'redux-saga/effects';
import {loadFollowers} from 'app/redux/FollowSaga';
import {loadFollows} from 'app/redux/FollowSaga';
import Apis from 'shared/api_client/ApiInstances';
import GlobalReducer from './GlobalReducer';
import constants from './constants';
Expand All @@ -16,7 +16,7 @@ export function* fetchState(location_change_action) {
const {pathname, /*search*/} = location_change_action.payload;
if(/@[a-z0-9\.-]+$/.test(pathname)) {
const username = pathname.substring(2)
yield fork(loadFollowers, username, 'blog')
yield fork(loadFollows, "get_followers", username, 'blog')
}
const server_location = yield select(state => state.offchain.get('server_location'));
if (pathname === server_location) return;
Expand Down
46 changes: 8 additions & 38 deletions app/redux/FollowSaga.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ import {call, put} from 'redux-saga/effects';
import {Apis} from 'shared/api_client';

// Test limit with 2 (not 1, infinate looping)
export function* loadFollowing(follower, type, start = '', limit = 100) {
const res = fromJS(yield Apis.follow('get_following', follower, start, type, limit))
export function* loadFollows(method, follower, type, start = '', limit = 100) {
const res = fromJS(yield Apis.follow(method, follower, start, type, limit))
// console.log('res.toJS()', res.toJS())
let cnt = 0
let lastFollowing = null
const key = method === "get_following" ? "following" : "follower";

yield put({type: 'global/UPDATE', payload: {
key: ['follow', 'get_following', follower],
key: ['follow', method, follower],
notSet: Map(),
updater: m => {
m = m.update('result', Map(), m2 => {
res.forEach(value => {
cnt++
const what = value.get('what')
const following = lastFollowing = value.get('following')
const following = lastFollowing = value.get(key)
m2 = m2.set(following, what)
})
return m2
Expand All @@ -25,43 +27,11 @@ export function* loadFollowing(follower, type, start = '', limit = 100) {
}
}})
if(cnt === limit) {
yield call(loadFollowing, follower, type, lastFollowing)
yield call(loadFollows, method, follower, type, lastFollowing)
} else {
yield put({type: 'global/UPDATE', payload: {
key: ['follow', 'get_following', follower],
key: ['follow', method, follower],
updater: m => m.merge({loading: false, error: null})
}})
}
}

// Test limit with 2 (not 1, infinate looping)
export function* loadFollowers(following, type, start = '', limit = 100) {
const res = fromJS(yield Apis.follow('get_followers', following, start, type, limit))
// console.log('res.toJS()', res.toJS())
let cnt = 0
let last = null
yield put({type: 'global/UPDATE', payload: {
key: ['follow', 'get_followers', following],
notSet: Map(),
updater: m => {
m = m.update('result', Map(), m2 => {
res.forEach(value => {
cnt++
const what = value.get('what')
const follower = last = value.get('follower')
m2 = m2.set(follower, what)
})
return m2
})
return m.merge({loading: true, error: null})
}
}})
if(cnt === limit) {
yield call(loadFollowers, following, type, last)
} else {
yield put({type: 'global/UPDATE', payload: {
key: ['follow', 'get_following', following],
updater: m => m.merge({loading: false, error: null})
}})
}
}
6 changes: 3 additions & 3 deletions app/redux/UserSaga.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {browserHistory} from 'react-router'
import {serverApiLogin, serverApiLogout, /*serverApiRecordEvent*/} from 'app/utils/ServerApiClient';
import {Apis} from 'shared/api_client';
import {serverApiRecordEvent} from 'app/utils/ServerApiClient';
import {loadFollowing} from 'app/redux/FollowSaga'
import {loadFollows} from 'app/redux/FollowSaga'

export const userWatches = [
watchRemoveHighSecurityKeys, // keep first to remove keys early when a page change happens
Expand Down Expand Up @@ -60,8 +60,8 @@ function* usernamePasswordLogin(action) {
const current = yield select(state => state.user.get('current'))
if(current) {
const username = current.get('username')
yield fork(loadFollowing, username, 'blog')
yield fork(loadFollowing, username, 'ignore')
yield fork(loadFollows, "get_following", username, 'blog')
yield fork(loadFollows, "get_following", username, 'ignore')
}
}

Expand Down

0 comments on commit 0b7fbe9

Please sign in to comment.