Skip to content

Instantly share code, notes, and snippets.

@n00ge
Last active July 6, 2017 17:05
Show Gist options
  • Save n00ge/61c4b8af242a8b744fffe8094daa22b8 to your computer and use it in GitHub Desktop.
Save n00ge/61c4b8af242a8b744fffe8094daa22b8 to your computer and use it in GitHub Desktop.
sample
import { Epic } from 'services/store';
import { Observable } from 'rxjs/Observable';
import * as api from 'services/api';
import * as apiStore from 'services/api-store';
import { getMessagesForDirectMessage } from 'services/messaging';
import * as reducer from './reducer';
function removeArchivedDirectMessages(resources: api.jsonApi.Resource<any, any>[]) {
return resources.filter(
resource =>
!(resource.type === 'direct_messages' && (resource as api.directMessages.DirectMessageResource).attributes.archived_at != null),
);
}
function getOldestMessageDate(state: reducer.GlobalState, channelUrl: string) {
const conversation = reducer.selectConversation(state, channelUrl);
if (conversation.messages.length === 0) {
return Date.now();
}
return Date.parse(conversation.messages[conversation.messages.length - 1].date);
}
const MESSAGE_LOAD_LIMIT = 20;
function getConversationActionsForDirectMessages(state: reducer.GlobalState, directMessages: api.directMessages.DirectMessageResource[]) {
return Promise.all(directMessages.filter(
directMessage => {
const conversation = reducer.selectConversation(state, directMessage.attributes.channel_url);
return conversation.messages.length === 0 && !conversation.loadedAllMessages;
},
).map(
(directMessage: api.directMessages.DirectMessageResource) =>
getMessagesForDirectMessage({ channelUrl: directMessage.attributes.channel_url, fromBefore: Date.now(), limit: MESSAGE_LOAD_LIMIT })
.then(conversationMessages =>
reducer.loadConversationSuccess({
channelUrl: directMessage.attributes.channel_url,
messages: conversationMessages,
allLoaded: conversationMessages.length < MESSAGE_LOAD_LIMIT,
}), error => reducer.loadConversationFailure({
channelUrl: directMessage.attributes.channel_url,
error,
})),
));
}
const epic: Epic<any> = (action$, store) =>
Observable.merge(
action$.filter(reducer.loadDirectMessages.check)
.switchMap(
() => api.directMessages.findAllForCurrentUser()
.then(response => {
if (!response.ok) {
return [reducer.loadDirectMessagesFailure(response)];
}
const directMessages = removeArchivedDirectMessages(response.data);
return getConversationActionsForDirectMessages(store.getState(), directMessages).then(conversationActions => {
return [
apiStore.loadApiResourcesSuccess([...directMessages, ...response.included]),
...conversationActions,
reducer.loadDirectMessagesSuccess(),
];
});
})
.catch((error: any) => [reducer.loadDirectMessagesFailure(error)]),
).flatMap(actions => [...actions]),
action$.filter(reducer.loadConversation.check)
.flatMap(
({ payload: channelUrl }) => getMessagesForDirectMessage({
channelUrl: channelUrl,
fromBefore: getOldestMessageDate(store.getState(), channelUrl),
limit: MESSAGE_LOAD_LIMIT,
})
.then(messages => reducer.loadConversationSuccess({ messages, channelUrl, allLoaded: messages.length < MESSAGE_LOAD_LIMIT }))
.catch((error: any) => reducer.loadConversationFailure({ error, channelUrl })),
),
);
export default epic;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment