From 19f4db30385518772f00ddf14bd948f467624b24 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Sun, 26 Aug 2018 04:09:02 +0100 Subject: [PATCH] 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 --- resources/src/startup/mediawiki.js | 100 ++++++++++++++--------------- 1 file changed, 47 insertions(+), 53 deletions(-) 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 `