Skip to content

Commit

Permalink
Merge branch 'MDL-79194-402' of https://github.com/laurentdavid/moodle
Browse files Browse the repository at this point in the history
…into MOODLE_402_STABLE
  • Loading branch information
junpataleta committed Oct 27, 2023
2 parents 3b0433e + 41f098e commit be729eb
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 15 deletions.
2 changes: 1 addition & 1 deletion course/format/amd/build/local/content.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion course/format/amd/build/local/content.min.js.map

Large diffs are not rendered by default.

49 changes: 42 additions & 7 deletions course/format/amd/src/local/content.js
Expand Up @@ -235,6 +235,8 @@ export default class Component extends BaseComponent {
{watch: `transaction:start`, handler: this._startProcessing},
{watch: `course.sectionlist:updated`, handler: this._refreshCourseSectionlist},
{watch: `section.cmlist:updated`, handler: this._refreshSectionCmlist},
// Section visibility.
{watch: `section.visible:updated`, handler: this._reloadSection},
// Reindex sections and cms.
{watch: `state:updated`, handler: this._indexContents},
];
Expand Down Expand Up @@ -522,12 +524,12 @@ export default class Component extends BaseComponent {
if (debouncedReload) {
return debouncedReload;
}
const pendingReload = new Pending(pendingKey);
const reload = () => {
const pendingReload = new Pending(pendingKey);
this.debouncedReloads.delete(pendingKey);
const cmitem = this.getElement(this.selectors.CM, cmId);
if (!cmitem) {
return;
return pendingReload.resolve();
}
const promise = Fragment.loadFragment(
'core_courseformat',
Expand All @@ -540,17 +542,45 @@ export default class Component extends BaseComponent {
}
);
promise.then((html, js) => {
// Other state change can reload the CM or the section before this one.
if (!document.contains(cmitem)) {
pendingReload.resolve();
return false;
}
Templates.replaceNode(cmitem, html, js);
this._indexContents();
pendingReload.resolve();
return;
}).catch();
return true;
}).catch(() => {
pendingReload.resolve();
});
return pendingReload;
};
debouncedReload = debounce(reload, 200);
debouncedReload = debounce(
reload,
200,
{
cancel: true, pending: true
}
);
this.debouncedReloads.set(pendingKey, debouncedReload);
return debouncedReload;
}

/**
* Cancel the active reload CM debounced function, if any.
* @param {Number} cmId
*/
_cancelDebouncedReloadCm(cmId) {
const pendingKey = `courseformat/content:reloadCm_${cmId}`;
const debouncedReload = this.debouncedReloads.get(pendingKey);
if (!debouncedReload) {
return;
}
debouncedReload.cancel();
this.debouncedReloads.delete(pendingKey);
}

/**
* Reload a course section contents.
*
Expand All @@ -564,6 +594,10 @@ export default class Component extends BaseComponent {
const pendingReload = new Pending(`courseformat/content:reloadSection_${element.id}`);
const sectionitem = this.getElement(this.selectors.SECTION, element.id);
if (sectionitem) {
// Cancel any pending reload because the section will reload cms too.
for (const cmId of element.cmlist) {
this._cancelDebouncedReloadCm(cmId);
}
const promise = Fragment.loadFragment(
'core_courseformat',
'section',
Expand All @@ -578,8 +612,9 @@ export default class Component extends BaseComponent {
Templates.replaceNode(sectionitem, html, js);
this._indexContents();
pendingReload.resolve();
return;
}).catch();
}).catch(() => {
pendingReload.resolve();
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/amd/build/utils.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/amd/build/utils.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 46 additions & 4 deletions lib/amd/src/utils.js
Expand Up @@ -21,6 +21,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

import Pending from 'core/pending';

/**
* Create a wrapper function to throttle the execution of the given
*
Expand Down Expand Up @@ -70,6 +72,11 @@ export const throttle = (func, wait) => {
return run;
};

/**
* @property {Map} debounceMap A map of functions to their debounced pending promises.
*/
const debounceMap = new Map();

/**
* Create a wrapper function to debounce the execution of the given
* function. Each attempt to execute the function will reset the cooldown
Expand All @@ -78,14 +85,49 @@ export const throttle = (func, wait) => {
* @method
* @param {Function} func The function to debounce
* @param {Number} wait The number of milliseconds to wait after the final attempt to execute
* @param {Object} [options]
* @param {boolean} [options.pending=false] Whether to wrap the debounced method in a pending promise
* @param {boolean} [options.cancel=false] Whether to add a cancel method to the debounced function
* @return {Function}
*/
export const debounce = (func, wait) => {
export const debounce = (
func,
wait,
{
pending = false,
cancel = false,
} = {},
) => {
let timeout = null;
return function(...args) {

const returnedFunction = (...args) => {
if (pending && !debounceMap.has(returnedFunction)) {
debounceMap.set(returnedFunction, new Pending('core/utils:debounce'));
}
clearTimeout(timeout);
timeout = setTimeout(() => {
func.apply(this, args);
timeout = setTimeout(async() => {
// Get the current pending promise and immediately empty it.
// This is important to allow the function to be debounced again as soon as possible.
// We do not resolve it until later - but that's fine because the promise is appropriately scoped.
const pendingPromise = debounceMap.get(returnedFunction);
debounceMap.delete(returnedFunction);

// Allow the debounced function to return a Promise.
// This ensures that Behat will not continue until the function has finished executing.
await func.apply(this, args);

// Resolve the pending promise if it exists.
pendingPromise?.resolve();
}, wait);
};

if (cancel) {
returnedFunction.cancel = () => {
const pendingPromise = debounceMap.get(returnedFunction);
pendingPromise?.resolve();
clearTimeout(timeout);
};
}

return returnedFunction;
};
5 changes: 5 additions & 0 deletions lib/upgrade.txt
@@ -1,6 +1,11 @@
This files describes API changes in core libraries and APIs,
information provided here is intended especially for developers.

=== 4.2.4 ===
* Add a new parameter to the debounce (core/utils) function allow it to create its own own Pending promise.
(via options.pending). This is a backport of patch MDL-78779.
* Add a new parameter to the debounce (core/utils) function to allow for cancellation.

=== 4.2.3 ===
* \moodle_page::set_title() has been updated to append the site name depending on the value of $CFG->sitenameintitle and whether
the site's fullname/shortname has been set. So there's no need to manually add the site name whenever calling $PAGE->set_title().
Expand Down

0 comments on commit be729eb

Please sign in to comment.