Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch next window? #50

Closed
mdbraber opened this issue Jun 21, 2023 · 3 comments
Closed

Fetch next window? #50

mdbraber opened this issue Jun 21, 2023 · 3 comments

Comments

@mdbraber
Copy link

mdbraber commented Jun 21, 2023

@neilj thanks for building this in public! Overture is great for those (like me) who like to be able to tinker with Fastmail (e.g. help building Fastmate).

I'm trying to build a function that mimicks the scrolling of a MesageList (without the need for a physical scroll) so that everything in a WindowedQuery gets loaded. E.g. when in Fastmail I have 60 items initially loaded, so I thought that doing FastMail.router.getAppController('mail').mailboxMessageList().getObjectAt(60); would figure out how to load the 61th object by querying the source. But I can't figure out what the right way is to load the _storeKeys beyond the initial 60. Would you have any suggestions on how to achieve this (e.g. based on the Fastmail exampel)

@neilj
Copy link
Member

neilj commented Jun 22, 2023

Could you please explain a bit more about what you're trying to achieve overall? Why are you trying to load all the messages into memory? And are you trying to load just those directly referenced by the query, or also the other messages in the same conversations as messages in the query?

@mdbraber
Copy link
Author

mdbraber commented Jun 22, 2023

@neilj thanks for your reply. I'm trying to create a sort of "Unified Inbox" where it shows the messsage count for messages in the Inbox with a particular label in the sidebar. So e.g. when I have 8 messages labeled "Project" in my Inbox, it shows "Project (8)" in the sidebar.

I'm using this code to create an extra inboxEmails property on the Mailbox:

FastMail.classes.Mailbox.prototype.inboxEmails = 0;
const mailboxes = FastMail.store.getAll(FastMail.classes.Mailbox);
const inboxKey = FastMail.findMailbox(mailboxes,'Inbox').get('storeKey');

FastMail.store.getAll(FastMail.classes.Mailbox).forEach(mailbox => {
    mailbox.inboxEmails = FastMail.store.getQuery("inboxEmails", FastMail.classes.LocalQuery, { Type: FastMail.classes.Message, where: function (data) { return data.mailboxIds[inboxKey] && data.mailboxIds[mailbox.get('storeKey')] } }).get('[]').map(x => x.get('thread')).filter((item,index,arr) => { return arr.indexOf(item) == index }).length;
    mailbox.computedPropertyDidChange('badgeProperty')
});

The 'issue' is that the Inbox isn't always fully loaded, most often only the first 2 windows (60 messages) are loaded. I've found this hack as a workaround (which obviously only works if the Inbox is visible):

FastMail.activeViews.v66._renderRange.start = 0;
FastMail.activeViews.v66._renderRange.end = FastMail.activeViews.v66.contentLength;
FastMail.activeViews.v66.viewNeedsRedraw();

(btw - I noticed that this hack is actually botching up a few things, including the undo functionality...)

As you can see it's all quite hackish, but I'm pleased that at least this is possible at all because OvertureJS / Fastmail is built in a way I can actually follow (somewhat) what's going on. But I haven't cracked what is needed to load extra windows in a WindowedQuery. I thought that FastMail.router.getAppController('mail').mailboxMessageList().getObjectAt(60); would achieve that, but it doesn't seem to do anything (although I thought to see that that's how a MailboxView is actually populated(?)

Thanks in advance for any pointers in the right direction! I'm probably going this wrong way round, but happy to learn any pointers how to do this!

@neilj
Copy link
Member

neilj commented Jul 10, 2023

That's a non-trivial thing to do properly to be honest. I would probably start with something like this:

const { Mailbox, Message, MessageList, Obj } = FM.classes;
const { store } = FM;

const inbox = store.getOne(Mailbox, x => x.role === 'inbox');
const args = {
    accountId: inbox.get('accountId'),
    where: { inMailbox: inbox.get('id') },
};
const queryId = Message.getQueryId(args);
const query = store.getQuery(queryId, MessageList, args);
const inboxCounts = new Obj({
    counts: new Map(),
    recalculate() {
        query.getStoreKeysForAllObjects((storeKeys) => {
            getMessages(storeKeys, 0, (messages) => {
                const counts = new Map();
                messages.forEach(message => {
                    message.get('mailboxes').forEach(mailbox => {
                        const id = mailbox.get('id');
                        counts.set(id, (counts.get(id) || 0) + 1)
                    })
                })
                this.set('counts', counts);
            });
        });
    },
});
query.addObserverForRange({}, inboxCounts, recalculate);

… but I don't think we expose the getMessages function on our FM global at the moment (which is intended for our internal debugging purposes more than building stuff on top). And the above won't be quite right if you move stuff in or out of the project folders rather than the inbox.

Anyway, interesting idea, but I'm afraid I don't have time to help you further with it. One last thought though, I think if you get the message list (FM.router.getAppController('mail').get('mailboxMessageList')) you could then do list.set('optimiseFetching', false).set('prefetch', 1024) (or an even larger number) and that will probably get it to fetch everything in the query when you fetch one item, although I haven't tested it to confirm!

@neilj neilj closed this as completed Jul 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants