MDL-62678 core: Improve efficiency of notifications and avoid ajax call

This commit is contained in:
Brendan Heywood
2020-09-30 13:55:30 +10:00
parent e049d30613
commit f3844e595e
6 changed files with 16 additions and 13 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+1 -7
View File
@@ -292,9 +292,8 @@ export const exception = async ex => {
*
* @param {Number} contextId
* @param {Array} notificationList
* @param {Boolean} userLoggedIn
*/
export const init = (contextId, notificationList, userLoggedIn) => {
export const init = (contextId, notificationList) => {
currentContextId = contextId;
// Setup the message target region if it isn't setup already
@@ -303,11 +302,6 @@ export const init = (contextId, notificationList, userLoggedIn) => {
// Add provided notifications.
addNotifications(notificationList);
// If the user is not logged in then we can not fetch anything for them.
if (userLoggedIn) {
// Perform an initial poll for any new notifications.
fetchNotifications();
}
};
// To maintain backwards compatability we export default here.
+2 -1
View File
@@ -58,7 +58,8 @@ class notification {
public static function add($message, $level = null) {
global $PAGE, $SESSION;
if ($PAGE && $PAGE->state === \moodle_page::STATE_IN_BODY) {
if ($PAGE && ($PAGE->state === \moodle_page::STATE_IN_BODY
|| $PAGE->state === \moodle_page::STATE_DONE)) {
// Currently in the page body - just render and exit immediately.
// We insert some code to immediately insert this into the user-notifications created by the header.
$id = uniqid();
+1 -2
View File
@@ -1425,8 +1425,7 @@ class core_renderer extends renderer_base {
if (!empty($this->page->context->id)) {
$this->page->requires->js_call_amd('core/notification', 'init', array(
$this->page->context->id,
\core\notification::fetch_as_array($this),
isloggedin()
\core\notification::fetch_as_array($this)
));
}
$footer = str_replace($this->unique_end_html_token, $this->page->requires->get_end_code(), $footer);
+10 -1
View File
@@ -86,7 +86,16 @@ class core_notification_testcase extends advanced_testcase {
$PAGE->set_state(\moodle_page::STATE_DONE);
\core\notification::add('Example after page', \core\notification::INFO);
$this->assertCount(3, $SESSION->notifications);
$this->assertCount(2, $SESSION->notifications);
$this->expectOutputRegex('/Example after page/');
\core\session\manager::write_close();
\core\notification::add('Example after write_close', \core\notification::INFO);
$this->assertCount(2, $SESSION->notifications);
$this->expectOutputRegex('/Example after write_close/');
// Simulate shutdown handler which calls fetch.
$this->assertCount(2, \core\notification::fetch());
}
/**