'use strict';

var es6Promise = require('es6-promise').Promise;

// es6-promise asap should schedule microtasks via zone.scheduleMicrotask so that any
// user defined hooks are triggered
es6Promise._setAsap(function(fn, arg) {
  global.zone.scheduleMicrotask(function() {
    fn(arg);
  });
});

// The default implementation of scheduleMicrotask use the original es6-promise implementation
// to schedule a microtask
function scheduleMicrotask(fn) {
  es6Promise._asap(this.bind(fn));
}

function addMicrotaskSupport(zoneClass) {
  zoneClass.prototype.scheduleMicrotask = scheduleMicrotask;
  return zoneClass;
}

module.exports = {
  addMicrotaskSupport: addMicrotaskSupport
};

// TODO(vicb): Create a benchmark for the different methods & the usage of the queue
// see https://github.com/angular/zone.js/issues/97

var hasNativePromise = typeof Promise !== "undefined" &&
                       Promise.toString().indexOf("[native code]") !== -1;

var isFirefox = global.navigator &&
                global.navigator.userAgent.toLowerCase().indexOf('firefox') > -1;

// TODO(vicb): remove '!isFirefox' when the bug gets fixed:
// https://bugzilla.mozilla.org/show_bug.cgi?id=1162013
if (hasNativePromise && !isFirefox) {
  // When available use a native Promise to schedule microtasks.
  // When not available, es6-promise fallback will be used
  var resolvedPromise = Promise.resolve();
  es6Promise._setScheduler(function(fn) {
    resolvedPromise.then(fn);
  });
}


@@ -12,758 +12,345 @@
 ';%0A%0A
-var es6Promise = require('es6-promise').Promise;%0A%0A// es6-promise asap should schedule microtasks via zone.scheduleMicrotask so that any%0A// user defined hooks are triggered%0Aes6Promise._setAsap(function(fn, arg) %7B%0A  global.zone.scheduleMicrotask(function() %7B%0A    fn(arg);%0A  %7D);%0A%7D);%0A%0A// The default implementation of scheduleMicrotask use the original es6-promise implementation%0A// to schedule a microtask%0Afunction scheduleMicrotask(fn) %7B%0A 
+// TODO(vicb): Create a benchmark for the different methods & the usage of the queue%0A// see https://github.com/angular/zone.js/issues/97%0A%0A// It is required to initialize hasNativePromise before requiring es6-promise otherwise
  es6
-P
+-p
 romise
-._asap(this.bind(fn));%0A%7D%0A%0Afunction addMicrotaskSupport(zoneClass) %7B%0A  zoneClass.prototype.scheduleMicrotask = scheduleMicrotask;%0A  return zoneClass;%0A%7D%0A%0Amodule.exports = %7B%0A  addMicrotaskSupport: addMicrotaskSupport%0A%7D;%0A%0A// TODO(vicb): Create a benchmark for the different methods & the usage of the queue
+ would%0A// overwrite the native Promise implementation on v8 and the check would always return false.
 %0A// 
@@ -372,39 +372,49 @@
 hub.com/
-angular/zone.js
+jakearchibald/es6-promise
 /issues/
 97%0A%0Avar 
@@ -405,19 +405,19 @@
 /issues/
-97%0A
+140
 %0Avar has
@@ -466,35 +466,16 @@
 ned%22 &&%0A
-                   
     Prom
@@ -559,28 +559,16 @@
 ator &&%0A
-            
     glob
@@ -630,16 +630,38 @@
  %3E -1;%0A%0A
+var resolvedPromise;%0A%0A
 // TODO(
@@ -932,20 +932,16 @@
  used%0A  
-var 
 resolved
@@ -969,16 +969,92 @@
 olve();%0A
+%7D%0A%0Avar es6Promise = require('es6-promise').Promise;%0A%0Aif (resolvedPromise) %7B%0A
   es6Pro
@@ -1122,12 +1122,630 @@
 );%0A  %7D);%0A%7D%0A%0A
+// es6-promise asap should schedule microtasks via zone.scheduleMicrotask so that any%0A// user defined hooks are triggered%0Aes6Promise._setAsap(function(fn, arg) %7B%0A  global.zone.scheduleMicrotask(function() %7B%0A    fn(arg);%0A  %7D);%0A%7D);%0A%0A// The default implementation of scheduleMicrotask use the original es6-promise implementation%0A// to schedule a microtask%0Afunction scheduleMicrotask(fn) %7B%0A  es6Promise._asap(this.bind(fn));%0A%7D%0A%0Afunction addMicrotaskSupport(zoneClass) %7B%0A  zoneClass.prototype.scheduleMicrotask = scheduleMicrotask;%0A  return zoneClass;%0A%7D%0A%0Amodule.exports = %7B%0A  addMicrotaskSupport: addMicrotaskSupport%0A%7D;%0A%0A%0A%0A

Make sure we check for native Promise before es6-promise gets a chance to polyfill