From 90fcc6f1fe040ec4667125f215c7c8986e3b98a7 Mon Sep 17 00:00:00 2001 From: dfahlander Date: Thu, 29 Apr 2021 12:50:03 +0200 Subject: [PATCH] Don't patch Promise.all, Promise.race, etc. This commit fails for 2 tests representing apps that imports Promise from a promise polyfill instead of just using the global Promise. Many apps and libs are still importing Promise from a polyfill to make them work on IE11. If these apps are doing Promise.resolve() or similar using the imported Promise constructor, we will not be able to track the ongoing transaction or liveQuery context if this commit is released. Those apps will need to drag down the promise polyfill in a plain script tag instead of importing from the polyfill. The webpacked code must use the global Promise and not an imported one at any time within Dexie transactions and liveQuery querier functions. This commit should be released in a new major version of Dexie with clear instrutions of how to migrate Promise polyfill dependent code. We could also make an intermediate fix that is backward compliant but warns in console if they are using import polyfill-pattern (not part of this commit). The upsides with this commit are: * Optimization: We only switch self.Promise between zones. * Simplicity: Less code * Unobtrusiveness --- src/helpers/promise.js | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/src/helpers/promise.js b/src/helpers/promise.js index b0f98f0ae..0d17bb408 100644 --- a/src/helpers/promise.js +++ b/src/helpers/promise.js @@ -656,13 +656,7 @@ export function newScope (fn, props, a1, a2) { var globalEnv = globalPSD.env; psd.env = patchGlobalPromise ? { Promise: DexiePromise, // Changing window.Promise could be omitted for Chrome and Edge, where IDB+Promise plays well! - PromiseProp: {value: DexiePromise, configurable: true, writable: true}, - all: DexiePromise.all, - race: DexiePromise.race, - allSettled: DexiePromise.allSettled, - any: DexiePromise.any, - resolve: DexiePromise.resolve, - reject: DexiePromise.reject, + PromiseProp: {value: DexiePromise, configurable: true, writable: true} } : {}; if (props) extend(psd, props); @@ -762,15 +756,6 @@ function switchToZone (targetZone, bEnteringZone) { // 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; - if (targetEnv.allSettled) GlobalPromise.allSettled = targetEnv.allSettled; - if (targetEnv.any) GlobalPromise.any = targetEnv.any; } } } @@ -779,13 +764,7 @@ function snapShot () { var GlobalPromise = _global.Promise; return patchGlobalPromise ? { Promise: GlobalPromise, - PromiseProp: Object.getOwnPropertyDescriptor(_global, "Promise"), - all: GlobalPromise.all, - race: GlobalPromise.race, - allSettled: GlobalPromise.allSettled, - any: GlobalPromise.any, - resolve: GlobalPromise.resolve, - reject: GlobalPromise.reject, + PromiseProp: Object.getOwnPropertyDescriptor(_global, "Promise") } : {}; }