From: Timo Tijhof Date: Tue, 10 Nov 2015 22:09:21 +0000 (+0000) Subject: mw.loader: Use requestIdleCallback to update module store X-Git-Tag: 1.31.0-rc.0~9013^2 X-Git-Url: https://git.heureux-cyclage.org/?a=commitdiff_plain;h=4174b662f623b5d3dd2482f2207c4128c6a60cd2;p=lhc%2Fweb%2Fwiklou.git mw.loader: Use requestIdleCallback to update module store .. instead of setTimeout. Also adapt the code accordingly since it is no longer needed to cancel and re-schedule an update every time update() is called. This is already handled by requestIdleCallback. We can let the already-scheduled callback survive. The data that will be saved is serialised during the flush anyway, not when scheduling. Change-Id: Iea48919b4baba7647040b6ed9ff4b6d0d387b962 --- diff --git a/resources/src/mediawiki/mediawiki.js b/resources/src/mediawiki/mediawiki.js index 3ffe55ffe6..db07ec9c0e 100644 --- a/resources/src/mediawiki/mediawiki.js +++ b/resources/src/mediawiki/mediawiki.js @@ -2277,15 +2277,13 @@ }, /** - * Sync modules to localStorage. + * Sync in-memory store back to localStorage. * - * This function debounces localStorage updates. When called multiple times in - * quick succession, the calls are coalesced into a single update operation. - * This allows us to call #update without having to consider the module load - * queue; the call to localStorage.setItem will be naturally deferred until the - * page is quiescent. + * This function debounces updates. When called with a flush already pending, + * the call is coalesced into the pending update. The call to + * localStorage.setItem will be naturally deferred until the page is quiescent. * - * Because localStorage is shared by all pages with the same origin, if multiple + * Because localStorage is shared by all pages from the same origin, if multiple * pages are loaded with different module sets, the possibility exists that * modules saved by one page will be clobbered by another. But the impact would * be minor and the problem would be corrected by subsequent page views. @@ -2293,16 +2291,16 @@ * @method */ update: ( function () { - var timer; + var hasPendingWrite = false; - function flush() { - var data, - key = mw.loader.store.getStoreKey(); - - if ( !mw.loader.store.enabled ) { - return false; + function flushWrites() { + var data, key; + if ( !hasPendingWrite || !mw.loader.store.enabled ) { + return; } + mw.loader.store.prune(); + key = mw.loader.store.getStoreKey(); try { // Replacing the content of the module store might fail if the new // contents would exceed the browser's localStorage size limit. To @@ -2314,11 +2312,15 @@ } catch ( e ) { mw.track( 'resourceloader.exception', { exception: e, source: 'store-localstorage-update' } ); } + + hasPendingWrite = false; } return function () { - clearTimeout( timer ); - timer = setTimeout( flush, 2000 ); + if ( !hasPendingWrite ) { + hasPendingWrite = true; + mw.requestIdleCallback( flushWrites ); + } }; }() ) }