mirror of
https://github.com/leporello-js/leporello-js
synced 2026-01-13 21:14:28 -08:00
fix deferred calls bug
This commit is contained in:
2
src/effects.js
vendored
2
src/effects.js
vendored
@@ -183,7 +183,7 @@ export const render_common_side_effects = (prev, next, command, ui) => {
|
|||||||
next.eval_modules_state != null
|
next.eval_modules_state != null
|
||||||
) {
|
) {
|
||||||
const s = next.eval_modules_state
|
const s = next.eval_modules_state
|
||||||
s.promise.then(result => {
|
s.promise.__original_then(result => {
|
||||||
exec('eval_modules_finished',
|
exec('eval_modules_finished',
|
||||||
next, /* becomes prev_state */
|
next, /* becomes prev_state */
|
||||||
result,
|
result,
|
||||||
|
|||||||
@@ -362,7 +362,7 @@ export const eval_modules = (
|
|||||||
})
|
})
|
||||||
|
|
||||||
if(is_async) {
|
if(is_async) {
|
||||||
return result.then(make_result)
|
return result.__original_then(make_result)
|
||||||
} else {
|
} else {
|
||||||
return make_result(result)
|
return make_result(result)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ const io_patch = (path, use_context = false) => {
|
|||||||
: original.apply(this, args)
|
: original.apply(this, args)
|
||||||
|
|
||||||
if(value instanceof cxt.window.Promise) {
|
if(value instanceof cxt.window.Promise) {
|
||||||
// TODO use cxt.promise_then, not finally which calls
|
// TODO use __original_then, not finally which calls
|
||||||
// patched 'then'?
|
// patched 'then'?
|
||||||
value = value.finally(() => {
|
value = value.finally(() => {
|
||||||
if(cxt_copy != cxt) {
|
if(cxt_copy != cxt) {
|
||||||
|
|||||||
@@ -16,8 +16,7 @@ const gen_to_promise = gen_fn => {
|
|||||||
} else {
|
} else {
|
||||||
// If promise
|
// If promise
|
||||||
if(result.value?.then != null) {
|
if(result.value?.then != null) {
|
||||||
return result.value.then.__original.call(
|
return result.value.__original_then(
|
||||||
result.value,
|
|
||||||
value => next(gen.next(value)),
|
value => next(gen.next(value)),
|
||||||
error => next(gen.throw(error)),
|
error => next(gen.throw(error)),
|
||||||
)
|
)
|
||||||
@@ -113,6 +112,10 @@ const apply_promise_patch = cxt => {
|
|||||||
|
|
||||||
cxt.promise_then = cxt.window.Promise.prototype.then
|
cxt.promise_then = cxt.window.Promise.prototype.then
|
||||||
|
|
||||||
|
if(cxt.window.Promise.prototype.__original_then == null) {
|
||||||
|
cxt.window.Promise.prototype.__original_then = cxt.window.Promise.prototype.then
|
||||||
|
}
|
||||||
|
|
||||||
cxt.window.Promise.prototype.then = function then(on_resolve, on_reject) {
|
cxt.window.Promise.prototype.then = function then(on_resolve, on_reject) {
|
||||||
|
|
||||||
if(cxt.children == null) {
|
if(cxt.children == null) {
|
||||||
@@ -135,14 +138,11 @@ const apply_promise_patch = cxt => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return cxt.promise_then.call(
|
return this.__original_then(
|
||||||
this,
|
|
||||||
make_callback(on_resolve, true),
|
make_callback(on_resolve, true),
|
||||||
make_callback(on_reject, false),
|
make_callback(on_reject, false),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
cxt.window.Promise.prototype.then.__original = cxt.promise_then
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const remove_promise_patch = cxt => {
|
const remove_promise_patch = cxt => {
|
||||||
@@ -266,7 +266,7 @@ const __do_await = async (cxt, value) => {
|
|||||||
}
|
}
|
||||||
const children_copy = cxt.children
|
const children_copy = cxt.children
|
||||||
if(value instanceof cxt.window.Promise) {
|
if(value instanceof cxt.window.Promise) {
|
||||||
cxt.promise_then.call(value,
|
value.__original_then(
|
||||||
v => {
|
v => {
|
||||||
value.status = {ok: true, value: v}
|
value.status = {ok: true, value: v}
|
||||||
},
|
},
|
||||||
|
|||||||
25
test/test.js
25
test/test.js
@@ -3050,6 +3050,31 @@ const y = x()`
|
|||||||
assert_equal(moved_state.active_calltree_node.fn.name, 'fn2')
|
assert_equal(moved_state.active_calltree_node.fn.name, 'fn2')
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
test('async/await async deferred call', async () => {
|
||||||
|
const code = `
|
||||||
|
await new Object()
|
||||||
|
export const fn = () => 1
|
||||||
|
`
|
||||||
|
const {state: i, on_deferred_call} = test_deferred_calls_state(code)
|
||||||
|
|
||||||
|
await i.eval_modules_state.promise.__original_then(result => {
|
||||||
|
const s = COMMANDS.eval_modules_finished(
|
||||||
|
i,
|
||||||
|
i,
|
||||||
|
result,
|
||||||
|
i.eval_modules_state.node,
|
||||||
|
i.eval_modules_state.toplevel
|
||||||
|
)
|
||||||
|
|
||||||
|
// Make deferred call
|
||||||
|
s.modules[''].fn()
|
||||||
|
const state = on_deferred_call(s)
|
||||||
|
assert_equal(get_deferred_calls(state).length, 1)
|
||||||
|
assert_equal(get_deferred_calls(state)[0].value, 1)
|
||||||
|
})
|
||||||
|
|
||||||
|
}),
|
||||||
|
|
||||||
test('async/await await argument bug', async () => {
|
test('async/await await argument bug', async () => {
|
||||||
await assert_code_evals_to_async(
|
await assert_code_evals_to_async(
|
||||||
`
|
`
|
||||||
|
|||||||
Reference in New Issue
Block a user