Skip to content

Commit

Permalink
fix stale rejection (#365)
Browse files Browse the repository at this point in the history
* fix stale rejection

* test for stale fulfillments & rejections
  • Loading branch information
mbostock authored Oct 23, 2023
1 parent 5116a31 commit a3b54b2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
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"]);
});

0 comments on commit a3b54b2

Please sign in to comment.