resourceloader: Don't create redundant timers in addEmbeddedCSS()
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 19 Nov 2015 21:44:36 +0000 (21:44 +0000)
committerOri.livneh <ori@wikimedia.org>
Tue, 24 Nov 2015 19:39:33 +0000 (19:39 +0000)
commit0d7ef032b71e9eaef02489e70f443f9776b2bebf
treefc6afbd59dfce1da2dcdf8363065574b6f373539
parent8732532d47866f75d9d145d877e3e41bbd3a29d2
resourceloader: Don't create redundant timers in addEmbeddedCSS()

There's is typically two ways one might implement an eventloop-based
timeout buffer in JavaScript.

Note that "tick" refers to an iteration of the eventloop. Each iteration starts
after all other sychronous code has finished in the current script execution.

1. Early-bird
   The first call to 'addBuffer' schedules a flush in the next tick.
   Any subsequent call (before the next tick) keeps adding to same buffer.
   As scheduled, in the next 'tick' all buffered items will be flushed.

   The next call to 'addBuffer' will a new cycle again.
   Buffered items are flushed within the next tick.

2. Idle delay
   Calls to 'addBuffer' always schedule a new flush. One cancels any existing
   flush that may've been scheduled. This effectively keeps pushing the flush
   to a future tick until a tick does not buffer new items.
   Buffered items may remain in buffer for a long time.

For the CSS buffer in ResourceLoader we want the first model. And while the
implementation already behaves like that, it was creating new timers in each
call regardless. This resulted in 10 buffered items creating 10 timers.
The first timer flushes the buffer. And the remaining 9 timers all try to
flush an empty buffer.

Change-Id: I49c64fedb0ad0362e91bb782fecf3cdda7dc61fc
resources/src/mediawiki/mediawiki.js