Last active
August 29, 2015 14:23
-
-
Save asakusuma/07dc535034152e1277fc to your computer and use it in GitHub Desktop.
This backburner extension on the Ember run loop provides an API for attaching a callback to the end of any run loop that utilizes a certain queue.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Usage: | |
BackburnerEvents.on('afterRender', () => { | |
// called after any run loop that utilizes the afterRender queue | |
}); | |
*/ | |
import Ember from "ember"; | |
let BackburnerEvents = Ember.Object.createWithMixins(Ember.Evented); | |
let backburner = Ember.run.backburner; | |
let oldOnBegin = backburner.options.onBegin; | |
let oldOnEnd = backburner.options.onEnd; | |
function forEachQueue(runloop, func) { | |
let queues = runloop.queues; | |
let keys = Object.keys(queues); | |
for (let i = 0; i < keys.length; i++) { | |
let queueName = keys[i]; | |
let queue = queues[queueName]; | |
func.call(this, queue, queueName); | |
} | |
} | |
// At the start of every run loop, go through every queue | |
// and override queue.push to set a flag on that queue. | |
// This gives us a flag that is true whenever the queue | |
// was actually utilized during the runloop | |
backburner.options.onBegin = function(runloop) { | |
forEachQueue(runloop, queue => { | |
let oldPush = queue._queue.push; | |
queue._queue.push = function() { | |
queue.wasFired = true; | |
queue._queue.push = oldPush; | |
return oldPush.apply(this, arguments); | |
}; | |
}); | |
oldOnBegin.apply(this, arguments); | |
}; | |
// When the run loop completes, check each flag to see | |
// that particular queue was used. If so, fire the | |
// appropriate event. | |
backburner.options.onEnd = function(runloop) { | |
forEachQueue(runloop, (queue, queueName) => { | |
if (queue.wasFired) { | |
BackburnerEvents.trigger(queueName); | |
} | |
}); | |
oldOnEnd.apply(this, arguments); | |
}; | |
export default BackburnerEvents; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment