function switchToZone()

in public/dexie.js [1373:1411]


  function switchToZone(targetZone, bEnteringZone) {
    var currentZone = PSD;
    if (
      bEnteringZone
        ? task.echoes && (!zoneEchoes++ || targetZone !== PSD)
        : zoneEchoes && (!--zoneEchoes || targetZone !== PSD)
    ) {
      // Enter or leave zone asynchronically as well, so that tasks initiated during current tick
      // will be surrounded by the zone when they are invoked.
      enqueueNativeMicroTask(
        bEnteringZone ? zoneEnterEcho.bind(null, targetZone) : zoneLeaveEcho
      );
    }
    if (targetZone === PSD) return;
    PSD = targetZone; // The actual zone switch occurs at this line.
    // Snapshot on every leave from global zone.
    if (currentZone === globalPSD) globalPSD.env = snapShot();
    if (patchGlobalPromise) {
      // Let's patch the global and native Promises (may be same or may be different)
      var GlobalPromise = globalPSD.env.Promise;
      // Swich environments (may be PSD-zone or the global zone. Both apply.)
      var targetEnv = targetZone.env;
      // Change Promise.prototype.then for native and global Promise (they MAY differ on polyfilled environments, but both can be accessed)
      // Must be done on each zone change because the patched method contains targetZone in its closure.
      nativePromiseProto.then = targetEnv.nthen;
      GlobalPromise.prototype.then = targetEnv.gthen;
      if (currentZone.global || targetZone.global) {
        // Leaving or entering global zone. It's time to patch / restore global Promise.
        // Set this Promise to window.Promise so that transiled async functions will work on Firefox, Safari and IE, as well as with Zonejs and angular.
        Object.defineProperty(_global, "Promise", targetEnv.PromiseProp);
        // Support Promise.all() etc to work indexedDB-safe also when people are including es6-promise as a module (they might
        // not be accessing global.Promise but a local reference to it)
        GlobalPromise.all = targetEnv.all;
        GlobalPromise.race = targetEnv.race;
        GlobalPromise.resolve = targetEnv.resolve;
        GlobalPromise.reject = targetEnv.reject;
      }
    }
  }