Skip to content

Instantly share code, notes, and snippets.

@neurosnap
Created March 8, 2016 16:03
Show Gist options
  • Save neurosnap/29bf8dc5c2aa0fcb4909 to your computer and use it in GitHub Desktop.
Save neurosnap/29bf8dc5c2aa0fcb4909 to your computer and use it in GitHub Desktop.
function main(sources) {
const auth$ = Auth(sources);
const apiDomain = getAPIDomain();
const apiMailboxes = `${apiDomain}/mailboxes`;
const getMailboxes$ = auth$.state$.map(accInfo => {
console.log(accInfo);
return {
url: apiMailboxes,
method: 'GET',
headers: {
'Content-Type': 'application/vnd.api+json',
'X-Authorization': accInfo.token
}
};
});
const mailboxes$ = sources.HTTP
.filter(res$ => { return res$.request.url.indexOf(apiMailboxes) === 0 })
.mergeAll()
.map(res => JSON.parse(res.text))
.map(data => {
return data.mailboxes;
});
const getThreads$ = Rx.Observable.combineLatest(auth$.state$, mailboxes$,
(accInfo, mailboxes) => {
console.log(accInfo, mailboxes);
let mailHTTP = [];
for (let i = 0; i < mailboxes.length; i++) {
let mailbox = mailboxes[i];
let apiThreads = `${apiDomain}/mailboxes/${mailbox.id}/threads/folder/in`;
mailHTTP.push({
url: apiThreads,
method: 'GET',
header: {
'Content-Type': 'application/vnd.api+json',
'X-Authorization': accInfo.token
}
});
}
return mailHTTP;
});
const threads$ = sources.HTTP
.mergeAll()
.map(res => JSON.parse(res.text))
.map(data => {
return data.threads;
}).startWith(false);
const vtree$ = view(auth$, mailboxes$);
return {
DOM: vtree$,
HTTP: Rx.Observable.merge(auth$.HTTP, getMailboxes$, getThreads$)
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment