Files
leporello-js/src/patch_promise.js

38 lines
806 B
JavaScript
Raw Normal View History

2022-12-02 04:13:32 +08:00
export const patch_promise = window => {
2022-12-16 21:43:07 +08:00
2022-12-23 02:14:38 +08:00
if(window.Promise.__patched) {
2022-12-07 05:06:15 +08:00
// already patched
return
2022-12-02 04:13:32 +08:00
}
2022-12-23 03:34:09 +08:00
const _then = window.Promise.prototype.then
2022-12-22 22:14:36 +08:00
2022-12-23 03:34:09 +08:00
window.Promise.prototype.then = function then(on_resolve, on_reject) {
2022-12-23 02:14:38 +08:00
let children = window.get_children()
if(children == null) {
children = []
window.set_children(children)
2022-12-22 22:14:36 +08:00
}
2022-12-23 02:14:38 +08:00
const make_callback = cb => cb == null
? null
: value => {
const current = window.get_children()
window.set_children(children)
try {
return cb(value)
} finally {
window.set_children(current)
}
2022-12-16 21:43:07 +08:00
}
2022-12-02 04:13:32 +08:00
2022-12-23 02:14:38 +08:00
return _then.call(
this,
make_callback(on_resolve),
make_callback(on_reject),
)
}
2022-12-02 04:13:32 +08:00
2022-12-23 02:14:38 +08:00
window.Promise.__patched = true
2022-12-02 04:13:32 +08:00
}