Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix stale rejection #365

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ function variable_compute(variable) {
variable._value = value;
variable._fulfilled(value);
}, (error) => {
if (error === variable_stale) return;
if (error === variable_stale || variable._version !== version) return;
variable._value = undefined;
variable._rejected(error);
});
Expand Down
46 changes: 46 additions & 0 deletions test/variable/define-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,49 @@ it("variable.define allows other variables to begin computation before a generat
assert.strictEqual(await gen._promise, 3, "gen cell 3");
assert.strictEqual(await val._promise, 3, "val cell 3");
});

it("variable.define does not report stale fulfillments", async () => {
const runtime = new Runtime();
const module = runtime.module();
const values = [];
const errors = [];
const variable = module.variable({
fulfilled(value) {
values.push(value);
},
rejected(error) {
errors.push(error);
}
});
const promise = new Promise((resolve) => setTimeout(() => resolve("value1"), 250));
variable.define(() => promise);
await runtime._computing;
variable.define(() => "value2");
await promise;
assert.deepStrictEqual(await valueof(variable), {value: "value2"});
assert.deepStrictEqual(values, ["value2"]);
assert.deepStrictEqual(errors, []);
});

it("variable.define does not report stale rejections", async () => {
const runtime = new Runtime();
const module = runtime.module();
const values = [];
const errors = [];
const variable = module.variable({
fulfilled(value) {
values.push(value);
},
rejected(error) {
errors.push(error);
}
});
const promise = new Promise((resolve, reject) => setTimeout(() => reject("error1"), 250));
variable.define(() => promise);
await runtime._computing;
variable.define(() => Promise.reject("error2"));
await promise.catch(() => {});
assert.deepStrictEqual(await valueof(variable), {error: "error2"});
assert.deepStrictEqual(values, []);
assert.deepStrictEqual(errors, ["error2"]);
});