Feature: Notification polling on the client #397
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.
What?
This PR adds notification polling, currently based on the heartbeat API from WordPress.
Why?
This attempts to solve the following issue: #306
How?
This PR adds a new Poller class that abstracts how/when/how often the notifications are polled.
It makes sense to use the existing Heartbeat API from WordPress since it is reliable. The Heartbeat API is a simple server polling API built into WordPress, allowing near-real-time frontend updates.
Heartbeat API Docs
The Poller class hooks into the
heartbeat.tick
event, which is triggered on a successful heartbeat (in case the user session expired or there is another error, we also skip useless polls to the API).There is a parameter that is passed to the Poller class named
shouldSkipFirstBeat
which solves the following edge-case: we load all notifications on page load, but also on some pages, the heartbeat API runs very early on the page load (eg: on post edit/create screen, on post list).To avoid double, repeated polls to fetch notifications, the first
Poller.SKIP_FIRST_INTERVAL
milliseconds, any heartbeat tick events are ignored.The Poller instance is added to the
notifications
(window.wp.notifications
) object, so you can easily do:window.wp.notifications.poller.stop()
- Stop pollingwindow.wp.notifications.poller.start()
- Start/resume pollingTesting Instructions
wp-admin
I am open to suggestions and ideas, also if you want to use a setInterval/setTimeout instead of the Heartbeats API, let pe know. I think it is appropriate though.
Closes #306