Skip to content

Commit

Permalink
Add API to give number of views into table
Browse files Browse the repository at this point in the history
  • Loading branch information
sinistersnare committed Jun 23, 2023
1 parent 46809f3 commit 30df5e3
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/perspective/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ declare module "@finos/perspective" {
make_port(): number;
get_index(): Promise<string | null>;
get_limit(): Promise<number | null>;
get_num_views(): Promise<number>;
};

/**** perspective ****/
Expand Down
2 changes: 2 additions & 0 deletions packages/perspective/src/js/api/table_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ table.prototype.get_index = async_queue("get_index", "table_method");

table.prototype.get_limit = async_queue("get_limit", "table_method");

table.prototype.get_num_views = async_queue("get_num_views", "table_method");

table.prototype.make_port = async_queue("make_port", "table_method");

table.prototype.remove_port = async_queue("remove_port", "table_method");
Expand Down
10 changes: 10 additions & 0 deletions packages/perspective/src/js/perspective.js
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,16 @@ export default function (Module) {
this._Table.reset_gnode(this.gnode_id);
};

/**
* @returns The number of views associated to this table.
* Note that this may be more than what is visible on a screen.
* As views are created to manage various internal
* state of the application.
*/
table.prototype.get_num_views = function () {
return this.views.length;
};

/**
* Replace all rows in this {@link module:perspective~table} the input data.
*/
Expand Down
20 changes: 20 additions & 0 deletions packages/perspective/test/js/updates.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,26 @@ async function match_delta(perspective, delta, expected) {
table.delete();
});

test.only("Table.get_num_views", async function () {
const table = await perspective.table({
a: "integer",
});
const view1 = await table.view();
expect(await table.get_num_views()).toBe(1);
const view2 = await table.view();
const view3 = await table.view();
const view4 = await table.view();
const view5 = await table.view();
expect(await table.get_num_views()).toBe(5);
view1.delete();
view2.delete();
view3.delete();
view4.delete();
view5.delete();
expect(await table.get_num_views()).toBe(0);
table.delete();
});

test("schema constructor indexed, then arrow update() with more columns than in the Table", async function () {
const table = await perspective.table(
{
Expand Down
3 changes: 3 additions & 0 deletions python/perspective/perspective/client/table_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def get_index(self):
def get_limit(self):
return self._async_queue("get_limit", "table_method")

def get_num_views(self):
return self._async_queue("get_num_views", "table_method")

def clear(self):
return self._async_queue("clear", "table_method")

Expand Down
4 changes: 4 additions & 0 deletions python/perspective/perspective/table/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ def get_limit(self):
specified by the user."""
return self._limit

def get_num_views(self):
"""Returns the number of views associated to this table."""
return len(self._views)

def clear(self):
"""Removes all the rows in the :class:`~perspective.Table`, but
preserves everything else including the schema and any callbacks or
Expand Down
25 changes: 25 additions & 0 deletions python/perspective/perspective/tests/table/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,31 @@ def test_table_get_limit_none(self):
tbl = Table(data)
assert tbl.get_limit() is None

# num_views

def test_table_get_num_views(self):
data = [{"a": 1, "b": 2}, {"a": 3, "b": 4}]
tbl = Table(data)
assert tbl.get_num_views() == 0
v1 = tbl.view()
v2 = tbl.view()
v3 = tbl.view()
assert tbl.get_num_views() == 3

v1.delete()
v2.delete()
assert tbl.get_num_views() == 1
failed = False
try:
tbl.delete()
except PerspectiveError:
failed = True
assert failed
v3.delete()
assert tbl.get_num_views() == 0
tbl.delete()


# clear

def test_table_clear(self):
Expand Down
1 change: 0 additions & 1 deletion rust/perspective-viewer/src/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#![feature(lazy_cell)]
#![feature(macro_metavar_expr)]
#![feature(let_chains)]
#![feature(anonymous_lifetime_in_impl_trait)]
#![feature(stmt_expr_attributes)]
#![warn(
clippy::all,
Expand Down

0 comments on commit 30df5e3

Please sign in to comment.