Fix/retain notification reference#118
Open
gwleuverink wants to merge 5 commits into
Open
Conversation
notifications could be garbage collected before the user interacts with it, after which the click / action / reply / close handlers silently never fire
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Keep a reference to Notifications so their events fire
Main process notifications stop emitting
click,close,actionandreplyevents after a while. It is most noticeable on macOS when the app has been idle or in the background: the notification still shows, the app may even come to the foreground when you click it, but the handler we registered never runs, so nothing is sent back to PHP.The cause is garbage collection. Electron only keeps a weak reference to a main process
Notification, so if nothing on the JS side holds onto it, V8 can collect the wrapper before the user interacts with it. Once that happens the native notification has no JS object left to dispatch through and the event is silently dropped. This is documented behaviour, see electron/electron#16922.In
api/notification.tstheNotificationwas a localconstthat went out of scope as soon as the request handler returned, so it was a prime candidate for collection.The fix
Hold onto each notification until it is dismissed. This mirrors how
state.windowsandstate.processesare already kept alive instate.ts:notificationsrecord tostate.show().clickandclosehandlers.Tests
This route had no tests, so I added coverage for it in
tests/notification.test.ts. It mounts the real router and exercises the POST endpoint:stateafter it is shownclick,action,replyandcloseare forwarded to Laravel with the right payloadsclickandcloserelease the retained referenceNotes
dist/is left to CI to rebuild.