Mailspring/internal_packages/thread-list/lib/thread-list-data-source.es6
Juan Tejada 44a7fa7117 fix(reminders): Add missing assets, ui cleanup, restore dead code
Summary:
This commit adds a couple of missing assets, including the icon for the
plugin list and other misc icons. It also switches to the new UI where
we use the thread timestamps to display the reminder date in the
Reminders perspective instead of using mail labels. It also adds a
header to the threads inside the reminders perspective to indicate that
a reminder will be triggered if no one replies to their email.

It also adds a header to indicate when a message has been brought back
to the inbox due to a reminder based on sdw's designs.

Finally it restores some code that magically disappeared when landing
reminders + other misc cleanup

Test Plan: Manual

Reviewers: bengotow, halla

Reviewed By: bengotow, halla

Differential Revision: https://phab.nylas.com/D3388
2016-11-01 11:39:50 -07:00

91 lines
3.6 KiB
JavaScript

import Rx from 'rx-lite';
import {
ObservableListDataSource,
DatabaseStore,
Message,
QueryResultSet,
QuerySubscription,
} from 'nylas-exports';
const _observableForThreadMessages = (id, initialModels) => {
const subscription = new QuerySubscription(DatabaseStore.findAll(Message, {threadId: id}), {
initialModels: initialModels,
emitResultSet: true,
});
return Rx.Observable.fromNamedQuerySubscription(`message-${id}`, subscription);
};
const _flatMapJoiningMessages = ($threadsResultSet) => {
// DatabaseView leverages `QuerySubscription` for threads /and/ for the
// messages on each thread, which are passed to out as `thread.__messages`.
let $messagesResultSets = {};
// 2. when we receive a set of threads, we check to see if we have message
// observables for each thread. If threads have been added to the result set,
// we make a single database query and load /all/ the message metadata for
// the new threads at once. (This is a performance optimization -it's about
// ~80msec faster than making 100 queries for 100 new thread ids separately.)
return $threadsResultSet.flatMapLatest((threadsResultSet) => {
const missingIds = threadsResultSet.ids().filter(id => !$messagesResultSets[id]);
let promise = null;
if (missingIds.length === 0) {
promise = Promise.resolve([threadsResultSet, []]);
} else {
promise = DatabaseStore.findAll(Message, {threadId: missingIds}).then((messages) => {
return Promise.resolve([threadsResultSet, messages]);
});
}
return Rx.Observable.fromPromise(promise);
})
// 3. when that finishes, we group the loaded messsages by threadId and create
// the missing observables. Creating a query subscription would normally load
// an initial result set. To avoid that, we just hand new subscriptions the
// results we loaded in #2.
.flatMapLatest(([threadsResultSet, messagesForNewThreads]) => {
const messagesGrouped = {};
for (const message of messagesForNewThreads) {
if (messagesGrouped[message.threadId] == null) { messagesGrouped[message.threadId] = []; }
messagesGrouped[message.threadId].push(message);
}
const oldSets = $messagesResultSets;
$messagesResultSets = {};
const sets = threadsResultSet.ids().map(id => {
$messagesResultSets[id] = oldSets[id] || _observableForThreadMessages(id, messagesGrouped[id]);
return $messagesResultSets[id];
});
sets.unshift(Rx.Observable.from([threadsResultSet]));
// 4. We use `combineLatest` to merge the message observables into a single
// stream (like Promise.all). When /any/ of them emit a new result set, we
// trigger.
return Rx.Observable.combineLatest(sets);
})
.flatMapLatest(([threadsResultSet, ...messagesResultSets]) => {
const threadsWithMessages = {};
threadsResultSet.models().forEach((thread, idx) => {
const clone = new thread.constructor(thread);
clone.__messages = messagesResultSets[idx] ? messagesResultSets[idx].models() : [];
clone.__messages = clone.__messages.filter((m) => !m.isHidden())
threadsWithMessages[clone.id] = clone;
});
return Rx.Observable.from([
QueryResultSet.setByApplyingModels(threadsResultSet, threadsWithMessages),
]);
});
};
class ThreadListDataSource extends ObservableListDataSource {
constructor(subscription) {
let $resultSetObservable = Rx.Observable.fromNamedQuerySubscription('thread-list', subscription);
$resultSetObservable = _flatMapJoiningMessages($resultSetObservable);
super($resultSetObservable, subscription.replaceRange.bind(subscription));
}
}
export default ThreadListDataSource;