Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DO NOT MERGE IN STABLE] web: remove the unhandledrejection polyfill
the unhandledrejection polyfill has been added for firefox to show promises rejected with errors (in odoo, we never reject promises with error and not cought, so we could assume that if a promise is rejected with an error, that error must be shown to the user). However, in the SIP library, there are some promises that are rejected with an error and handled at a later stage in the process : typically when requested access to the microphone has been denied. With the polyfill it shows the error, if we take the polyfill out, the error is correctly handled by the library. In the meantime, this polyfill can be overlooked, but do not merge this in master
- Loading branch information
1 parent
a3357d4
commit 47ccfb8
Showing
1 changed file
with
75 additions
and
75 deletions.
There are no files selected for viewing
150 changes: 75 additions & 75 deletions
150
addons/web/static/lib/unhandled-rejection-polyfill/unhandled-rejection-polyfill.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,75 @@ | ||
// based on https://github.com/ustccjw/unhandled-rejection-polyfill | ||
(function () { | ||
"use strict"; | ||
|
||
var self = window; | ||
var OriginalPromise = self.Promise; | ||
|
||
function dispatchUnhandledRejectionEvent(promise, reason) { | ||
var event = document.createEvent('Event'); | ||
Object.defineProperties(event, { | ||
promise: { | ||
value: promise, | ||
writable: false, | ||
}, | ||
reason: { | ||
value: reason, | ||
writable: false, | ||
}, | ||
}); | ||
event.initEvent('unhandledrejection', false, true); | ||
window.dispatchEvent(event); | ||
} | ||
|
||
function MyPromise(resolver) { | ||
if (!(this instanceof MyPromise)) { | ||
throw new TypeError('Cannot call a class as a function'); | ||
} | ||
var promise = new OriginalPromise(function (resolve, reject) { | ||
var customReject = function (reason) { | ||
// macro-task (setTimeout) will execute after micro-task (promise) | ||
setTimeout(function () { | ||
if (promise.handled !== true) { | ||
dispatchUnhandledRejectionEvent(promise, reason); | ||
} | ||
}, 0); | ||
return reject(reason); | ||
}; | ||
try { | ||
return resolver(resolve, customReject); | ||
} catch (err) { | ||
return customReject(err); | ||
} | ||
}); | ||
promise.__proto__ = MyPromise.prototype; | ||
return promise; | ||
} | ||
|
||
MyPromise.__proto__ = OriginalPromise; | ||
MyPromise.prototype.__proto__ = OriginalPromise.prototype; | ||
|
||
|
||
MyPromise.prototype.then = function (resolve, reject) { | ||
var self = this; | ||
return OriginalPromise.prototype.then.call(this, resolve, reject && (function (reason) { | ||
self.handled = true; | ||
return reject(reason); | ||
})); | ||
}; | ||
|
||
MyPromise.prototype.catch = function (reject) { | ||
var self = this; | ||
return OriginalPromise.prototype.catch.call(this, reject && (function (reason) { | ||
self.handled = true; | ||
return reject(reason); | ||
})); | ||
}; | ||
|
||
MyPromise.polyfill = function () { | ||
if (typeof PromiseRejectionEvent === 'undefined') { | ||
window.Promise = MyPromise; | ||
} | ||
}; | ||
MyPromise.polyfill(); | ||
|
||
})(); | ||
// // based on https://github.com/ustccjw/unhandled-rejection-polyfill | ||
// (function () { | ||
// "use strict"; | ||
// | ||
// var self = window; | ||
// var OriginalPromise = self.Promise; | ||
// | ||
// function dispatchUnhandledRejectionEvent(promise, reason) { | ||
// var event = document.createEvent('Event'); | ||
// Object.defineProperties(event, { | ||
// promise: { | ||
// value: promise, | ||
// writable: false, | ||
// }, | ||
// reason: { | ||
// value: reason, | ||
// writable: false, | ||
// }, | ||
// }); | ||
// event.initEvent('unhandledrejection', false, true); | ||
// window.dispatchEvent(event); | ||
// } | ||
// | ||
// function MyPromise(resolver) { | ||
// if (!(this instanceof MyPromise)) { | ||
// throw new TypeError('Cannot call a class as a function'); | ||
// } | ||
// var promise = new OriginalPromise(function (resolve, reject) { | ||
// var customReject = function (reason) { | ||
// // macro-task (setTimeout) will execute after micro-task (promise) | ||
// setTimeout(function () { | ||
// if (promise.handled !== true) { | ||
// dispatchUnhandledRejectionEvent(promise, reason); | ||
// } | ||
// }, 0); | ||
// return reject(reason); | ||
// }; | ||
// try { | ||
// return resolver(resolve, customReject); | ||
// } catch (err) { | ||
// return customReject(err); | ||
// } | ||
// }); | ||
// promise.__proto__ = MyPromise.prototype; | ||
// return promise; | ||
// } | ||
// | ||
// MyPromise.__proto__ = OriginalPromise; | ||
// MyPromise.prototype.__proto__ = OriginalPromise.prototype; | ||
// | ||
// | ||
// MyPromise.prototype.then = function (resolve, reject) { | ||
// var self = this; | ||
// return OriginalPromise.prototype.then.call(this, resolve, reject && (function (reason) { | ||
// self.handled = true; | ||
// return reject(reason); | ||
// })); | ||
// }; | ||
// | ||
// MyPromise.prototype.catch = function (reject) { | ||
// var self = this; | ||
// return OriginalPromise.prototype.catch.call(this, reject && (function (reason) { | ||
// self.handled = true; | ||
// return reject(reason); | ||
// })); | ||
// }; | ||
// | ||
// MyPromise.polyfill = function () { | ||
// if (typeof PromiseRejectionEvent === 'undefined') { | ||
// window.Promise = MyPromise; | ||
// } | ||
// }; | ||
// MyPromise.polyfill(); | ||
// | ||
// })(); |