Skip to content

Commit

Permalink
Merge pull request #822 from finos/fix-callbacks
Browse files Browse the repository at this point in the history
Properly remove `on_delete` and `on_update` callbacks that fail.
  • Loading branch information
texodus authored Nov 24, 2019
2 parents a04ebf8 + aa9ca1d commit df05d2a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,6 @@ python/cpp
python/perspective/perspective_python.egg-info
python/perspective/perspective/tests/table/psp_test
python/perspective/perspective/node/assets/*
/__pycache__
docs/static/js/logo.js
docs/static/js/logo.js.map
5 changes: 3 additions & 2 deletions packages/perspective/src/js/api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ export class Server {

this.post(result);
} catch (e) {
console.error("Removing callback after failed on_update() (presumably due to closed connection)");
obj["remove_update"](callback);
console.error(`Removing failed callback to \`${msg.method}()\` (presumably due to failed connection)`);
const remove_method = msg.method.substring(3);
obj[`remove_${remove_method}`](callback);
}
};
if (msg.callback_id) {
Expand Down
26 changes: 26 additions & 0 deletions packages/perspective/test/js/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,31 @@ module.exports = perspective => {

await table.delete();
});

it("properly removes a failed delete callback on a table", async function(done) {
const table = perspective.table([{x: 1}]);

// when a callback throws, it should delete that callback
table.on_delete(() => {
throw new Error("something went wrong!");
});

table.delete();
done();
});

it("properly removes a failed delete callback on a view", async function(done) {
const table = perspective.table([{x: 1}]);
const view = table.view();

// when a callback throws, it should delete that callback
view.on_delete(() => {
throw new Error("something went wrong!");
});

view.delete();
table.delete();
done();
});
});
};
33 changes: 33 additions & 0 deletions packages/perspective/test/js/updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,39 @@ module.exports = perspective => {
);
table1.update(data);
});

it("properly removes a failed update callback on a table", async function(done) {
const table = perspective.table({x: "integer"});
const view = table.view();
let size = await table.size();
let counter = 0;

// when a callback throws, it should delete that callback
view.on_update(() => {
counter++;
throw new Error("something went wrong!");
});

view.on_update(async () => {
// failed callback gets removed; non-failing callback gets called
let sz = await table.size();
expect(counter).toEqual(1);
expect(sz).toEqual(size++);
});

table.update([{x: 1}]);
table.update([{x: 2}]);
table.update([{x: 3}]);

const view2 = table.view(); // create a new view to make sure we process every update transacation.
const final_size = await table.size();
expect(final_size).toEqual(3);

view2.delete();
view.delete();
table.delete();
done();
});
});

describe("Limit", function() {
Expand Down

0 comments on commit df05d2a

Please sign in to comment.