Skip to content

Commit

Permalink
[DO NOT MERGE IN STABLE] web: remove the unhandledrejection polyfill
Browse files Browse the repository at this point in the history
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
VincentSchippefilt authored and fleodoo committed Jul 8, 2019
1 parent a3357d4 commit 47ccfb8
Showing 1 changed file with 75 additions and 75 deletions.
@@ -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();
//
// })();

0 comments on commit 47ccfb8

Please sign in to comment.