From: Timo Tijhof Date: Sun, 26 Aug 2018 03:09:02 +0000 (+0100) Subject: resourceloader: Simplify addEmbeddedCSS by using object refs X-Git-Tag: 1.34.0-rc.0~4269^2~1 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/comptes/journal.php?a=commitdiff_plain;h=19f4db30385518772f00ddf14bd948f467624b24;p=lhc%2Fweb%2Fwiklou.git resourceloader: Simplify addEmbeddedCSS by using object refs * Previously, the same function was used both for adding to the buffer, and flushing the buffer (via self-calling alternate signatures). The flushing logic was split off to a flushCssBuffer function. * Previously, when encountering an '@import' statement, it performed a synchronous flush, instead of the usual asynchronous ones. There was no reason for this, other than my laziness. I suspect because I was using strings, which can't be passed by reference, and I didn't think of another way. I'm now storing the string in an object, which can be passed by reference to the flush function. This means, as before, we can keep appending to its string after the flush is scheduled. But, unlike before, it also means we can reset our local reference and start a new buffer at any time and schedule that one, too. Bug: T202703 Change-Id: Ifc6dd59e9e8885d65ba425bc579ecbfb09f2ac64 --- diff --git a/resources/src/startup/mediawiki.js b/resources/src/startup/mediawiki.js index 95dde2dd98..99cb31c085 100644 --- a/resources/src/startup/mediawiki.js +++ b/resources/src/startup/mediawiki.js @@ -633,10 +633,8 @@ */ marker = document.querySelector( 'meta[name="ResourceLoaderDynamicStyles"]' ), - // For addEmbeddedCSS() - cssBuffer = '', - cssBufferTimer = null, - cssCallbacks = [], + // For #addEmbeddedCSS() + nextCssBuffer, rAF = window.requestAnimationFrame || setTimeout; /** @@ -659,66 +657,62 @@ return el; } + /** + * @private + * @param {Object} cssBuffer + */ + function flushCssBuffer( cssBuffer ) { + var i; + // Mark this object as inactive now so that further calls to addEmbeddedCSS() from + // the callbacks go to a new buffer instead of this one (T105973) + cssBuffer.active = false; + newStyleTag( cssBuffer.cssText, marker ); + for ( i = 0; i < cssBuffer.callbacks.length; i++ ) { + cssBuffer.callbacks[ i ](); + } + } + /** * Add a bit of CSS text to the current browser page. * - * The CSS will be appended to an existing ResourceLoader-created `