Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to MutationObserver #198

Merged
merged 2 commits into from
Sep 23, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions when.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ define(function (require) {

var reduceArray, slice, fcall, nextTick, handlerQueue,
setTimeout, funcProto, call, arrayProto, monitorApi,
cjsRequire, undef;
cjsRequire, MutationObserver, undef;

cjsRequire = require;

Expand Down Expand Up @@ -830,17 +830,21 @@ define(function (require) {
// Allow attaching the monitor to when() if env has no console
monitorApi = typeof console != 'undefined' ? console : when;

// Prefer setImmediate or MessageChannel, cascade to node,
// vertx and finally setTimeout
/*global setImmediate,MessageChannel,process*/
if (typeof setImmediate === 'function') {
nextTick = setImmediate.bind(global);
} else if(typeof MessageChannel !== 'undefined') {
var channel = new MessageChannel();
channel.port1.onmessage = drainQueue;
nextTick = function() { channel.port2.postMessage(0); };
} else if (typeof process === 'object' && process.nextTick) {
// Sniff "best" async scheduling option
// Prefer process.nextTick or MutationObserver, then check for
// vertx and finally fall back to setTimeout
/*global process*/
if (typeof process === 'object' && process.nextTick) {
nextTick = process.nextTick;
} else if(MutationObserver = global.MutationObserver || global.WebKitMutationObserver) {
nextTick = (function(document, MutationObserver, drainQueue) {
var el = document.createElement('div');
new MutationObserver(drainQueue).observe(el, { attributes: true });

return function() {
el.setAttribute('x', 'x');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this works multiple times? The 'x' attribute doesn't have to change its value?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently, yeah. I've run some tests in Chrome and it works. I plan to let Travis run the full gamut of tests on all platforms when this lands in dev as well. Also, this is how RSVP implements it

};
}(document, MutationObserver, drainQueue));
} else {
try {
// vert.x 1.x || 2.x
Expand Down