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

messages: pass all messages through "message" event #2205

Closed
wants to merge 4 commits into from

Conversation

rigrig
Copy link
Contributor

@rigrig rigrig commented Oct 28, 2022

Instead of directly calling from the lib into the app/widget, always use "message" events.
Also adds some optimization so we only read messages from storage once per event.

This also gets rid of the update method, see https://forum.espruino.com/comments/16740860/

Marking this as draft because it's friday night, and I don't have time to properly test it, but other than that it is finished ;-)

@@ -51,5 +53,6 @@ WIDGETS["messages"]={area:"tl", width:0, draw:function(recall) {
/* We might have returned here if we were in the Messages app for a
message but then the watch was never viewed. */
if (global.MESSAGES===undefined)
WIDGETS["messages"].update(require("messages").getMessages());
WIDGETS["messages"].onMsg('',{});
Bangle.on("message", WIDGETS["messages"].onMsg);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If MESSAGES is defined and we're in the app, don't we still want to register our Bangle.on("message" handler?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm not really sure what's going on here actually...
Do we even want to load the widget while in the messages app?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question, maybe not... I just pushed a change to the messages lib that makes it 'fast load' the app if it can (which is really neat because it makes it much more responsive). But, that means that if the messages widget was already loaded, it'll stay loaded - so would have to cope with being around for the messages app anyway.

@gfwilliams
Copy link
Member

Marking this as draft because it's friday night, and I don't have time to properly test it, but other than that it is finished ;-)

How are you feeling about this now? Did you have a chance to test it properly?

Apart from that event handler registering I just posted, this looks good to me

@rigrig
Copy link
Contributor Author

rigrig commented Nov 15, 2022

Did you have a chance to test it properly?

Whoops, I totally forgot, sorry. I'll have a look at it tonight.

@rigrig rigrig marked this pull request as ready for review November 15, 2022 20:03
@rigrig
Copy link
Contributor Author

rigrig commented Nov 15, 2022

Ran into a bug where msgs wasn't set, no idea when I broke that, but fixed now.
Also rebased, and added a __FILE__ check to hide the widget while the app is open

@rigrig rigrig marked this pull request as draft November 16, 2022 09:53
@rigrig
Copy link
Contributor Author

rigrig commented Nov 16, 2022

Converted back to draft: this morning I didn't get any music notifications, so I'll have to look into that first :-(

@gfwilliams
Copy link
Member

Thanks! This is looking good though

@rigrig rigrig marked this pull request as ready for review November 16, 2022 18:56
@rigrig
Copy link
Contributor Author

rigrig commented Nov 16, 2022

Fixed it: there was a race when two events arrived in a row(e.g. musicinfo+musicstate), causing the first one to clean up MESSAGES while the second was still using it. Changed it so that a) that doesn't happen anymore, and b) it won't break even if it does happen.

Basically always keep MESSAGES in memory until event handlers have had a
chance to run, so they don't all cause Storage.read()s when they call
require("messages").getMessages()
Happens for music: we get both a musicinfo and a musicstate event, and
when the first one finishes processing, it cleans up MESSAGES before the
next event is done.
Also just use exports.getMessages() to make sure it won't break.
@rigrig
Copy link
Contributor Author

rigrig commented Nov 16, 2022

One thing I just thought about: it now saves all messages before emitting the event.
Should it maybe not do that? Something like if any listener sets event.handled, only store the message if it also sets event.save?
That could be a bit tricky though, as other listeners (like the widget) will assume the message now exists...

@gfwilliams
Copy link
Member

One thing I just thought about: it now saves all messages before emitting the event. Should it maybe not do that?

Argh, yes - that's a pain. That could be problematic then.

I thought the idea was that stuff like the music app could intercept the message before it got to be stored, so it didn't end up in the list of messages.

What about updating the message list, and then instead of having it as a global MESSAGES we just pass it as an argument to the event (it might be better to make it a one-argument event that passes an object, so it's more readable/flexible?).

Then we only write the message to storage if the message isn't handled, but things like the widget still get access to the full (new) list of messages?

@rigrig rigrig marked this pull request as draft November 17, 2022 09:22
@rigrig rigrig marked this pull request as ready for review November 17, 2022 09:31
@rigrig
Copy link
Contributor Author

rigrig commented Nov 17, 2022

I thought the idea was that stuff like the music app could intercept the message before it got to be stored, so it didn't end up in the list of messages.

It was, but apparently I lost track of that somewhere.
(And it's a bit tricky, because we do need to store the first one before launching the app)

What about updating the message list, and (...) pass it as an argument to the event

I guess that makes sense, as we're loading MESSAGES anyway.
But ideally we wouldn't need to load them all to handle incoming events: usually a listener is going to launch the app or ignore the event anyway. (Widgets could load them once at startup, and then keep track of event srcs internally, instead of re-filtering them all every time.)

make it a one-argument event that passes an object, so it's more readable/flexible?

I could do that if you want, although I'm not really a fan of the idea. I think it makes sense for arguments with optional fields, but if we're always passing (type,event,all) arguments, I don't see the benefit of wrapping those in an object?

How about for now I'll adjust this PR so

  • it passes the full message list in events
  • if an event is handled, it won't be saved unless store is also set
  • music events are not stored by default, unless we are going to launch the app

And a follow-up PR to try and separate the "default" code from the library.

@gfwilliams
Copy link
Member

if we're always passing (type,event,all) arguments, I don't see the benefit of wrapping those in an object?

It's just for readability really - but yes, let's keep it with args for now.

How about for now I'll adjust this PR so

That sounds great. but...

music events are not stored by default, unless we are going to launch the app

You mean the messages app? Because at the moment the messages app needs to store them...

@rigrig
Copy link
Contributor Author

rigrig commented Nov 17, 2022

at the moment the messages app needs to store them...

Urgh, juggling too many message apps in my head.
I guess for now no special-casing for music events. (But aiming for the app to eventually mark them as handled and keep them only in memory until kill.)

@rigrig
Copy link
Contributor Author

rigrig commented Nov 21, 2022

Closing in favour of #2297
(Seemed cleaner to start a new branch)

@rigrig rigrig closed this Nov 21, 2022
@rigrig rigrig deleted the message-events branch November 28, 2022 21:40
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

Successfully merging this pull request may close these issues.

None yet

2 participants