Skip to content
This repository has been archived by the owner on Apr 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #39 from k0kubun/search-streaming
Browse files Browse the repository at this point in the history
Search streaming
  • Loading branch information
k0kubun authored Oct 3, 2016
2 parents 1c94fec + 62dd44f commit 22327cd
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 2 deletions.
41 changes: 40 additions & 1 deletion src/actions/stream.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import TwitterClient from '../utils/twitter-client';
import { addTweet, removeTweet } from './tweets';
import { addTweet, addTweetToTab, removeTweet } from './tweets';

export const SET_OPEN_STREAM = 'SET_OPEN_STREAM';
export const SET_OPEN_FILTER = 'SET_OPEN_FILTER';
export const CLOSE_STREAM = 'CLOSE_STREAM';
export const CLOSE_FILTER = 'CLOSE_FILTER';

export const setOpenStream = (stream, account) => {
return { type: SET_OPEN_STREAM, stream, account }
Expand Down Expand Up @@ -49,3 +51,40 @@ export const reconnectStreaming = (account) => {
dispatch(startStreaming(account));
}
}

export const setOpenFilter = (stream, account) => {
return { type: SET_OPEN_FILTER, stream, account }
}

export const closeFilter = (account) => {
return { type: CLOSE_FILTER, account }
}

export const startFilter = (query, account) => {
return dispatch => {
const client = new TwitterClient(account);
client.filterStream(query, (stream) => {
stream.on('data', (data) => {
if (data['friends']) {
// noop
} else if (data['event']) {
// noop
} else if (data['delete']) {
// noop
} else if (data['created_at']) {
// This is a normal tweet
dispatch(addTweetToTab(data, account, 'search'));
}
});

dispatch(setOpenFilter(stream, account));
});
}
}

export const reconnectFilter = (query, account) => {
return (dispatch) => {
dispatch(closeFilter(account));
dispatch(startFilter(query, account));
}
}
1 change: 1 addition & 0 deletions src/containers/search-container.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const mapDispatchToProps = (dispatch, props) => {
event.preventDefault();
dispatch(Actions.setSearchQuery(event.target.value, props.account));
dispatch(Actions.loadSearch(event.target.value, props.account, true))
dispatch(Actions.startFilter(event.target.value, props.account));
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/reducers/filter-by-user-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Actions from '../actions';

export const filterByUserId = (state = {}, action) => {
switch (action.type) {
case Actions.SET_OPEN_FILTER:
return {
...state,
[action.account.id_str]: action.stream,
};
case Actions.CLOSE_FILTER:
if (state[action.account.id_str]) {
state[action.account.id_str].destroy();
}
return {
...state,
[action.account.id_str]: null,
}
default:
return state;
}
}
2 changes: 2 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { accounts } from './accounts';
import { activeAccountIndex } from './active-account-index';
import { activeListIdByUserId } from './active-list-id-by-user-id';
import { editorFocused } from './editor-focused';
import { filterByUserId } from './filter-by-user-id';
import { inReplyTo } from './in-reply-to';
import { listsByUserId } from './lists-by-user-id';
import { nowsByUserId } from './nows-by-user-id';
Expand All @@ -20,6 +21,7 @@ const rootReducer = combineReducers({
activeAccountIndex,
activeListIdByUserId,
editorFocused,
filterByUserId,
inReplyTo,
listsByUserId,
nowsByUserId,
Expand Down
5 changes: 4 additions & 1 deletion src/utils/ipc-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ export default class IpcAction {
if (listId) this.dispatch(Actions.loadList(listId, account));

const query = this.state.activeSearchQuery();
if (query) this.dispatch(Actions.loadSearch(query, account));
if (query) {
this.dispatch(Actions.loadSearch(query, account));
this.dispatch(Actions.reconnectFilter(query, account));
}

this.dispatch(Actions.reconnectStreaming(account));
});
Expand Down
6 changes: 6 additions & 0 deletions src/utils/twitter-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export default class TwitterClient {
});
}

filterStream(track, callback) {
this.client.stream('statuses/filter', { track: track }, (stream) => {
callback(stream);
});
}

updateStatus(tweet, inReplyTo, callback) {
if (tweet === '') return;

Expand Down

0 comments on commit 22327cd

Please sign in to comment.