Skip to content

Commit 52fbefe

Browse files
committed
core: implement experimental API for runs.
With tb.plugin.lib.run, you can now fetch list of runs and subscribe to changes. Do note that this change includes changes to the BUILD where we no longer use modules for the library. We will revisit this in near future to make this more usuable for non-TB Polymer projects
1 parent d6d8967 commit 52fbefe

File tree

20 files changed

+502
-255
lines changed

20 files changed

+502
-255
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package(default_visibility = ["//tensorboard:internal"])
2+
3+
load("//tensorboard/defs:web.bzl", "tf_web_library")
4+
5+
licenses(["notice"]) # Apache 2.0
6+
7+
# TODO(stephanwlee): figure out how this tf_web_library can be used to create
8+
# maybe a NPM package.
9+
tf_web_library(
10+
name = "plugin_lib",
11+
srcs = [
12+
"runs.ts",
13+
"tf-plugin-lib.html",
14+
],
15+
path = "/",
16+
deps = [
17+
"//tensorboard/components/plugin_util:plugin_guest",
18+
],
19+
)
20+
21+
tf_web_library(
22+
name = "host_impls",
23+
srcs = [
24+
"runs-host-impl.ts",
25+
"tf-plugin-host-impls.html",
26+
],
27+
path = "/tf-plugin-lib",
28+
deps = [
29+
"//tensorboard/components/plugin_util:plugin_host",
30+
"//tensorboard/components/tf_backend",
31+
],
32+
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
/**
16+
* Implements run related plugin APIs.
17+
*/
18+
tb.plugin.lib.host.listen('experimental.GetRuns', () => {
19+
return tf_backend.runsStore.getRuns();
20+
});
21+
22+
tf_backend.runsStore.addListener(() => {
23+
return tb.plugin.lib.host.broadcast(
24+
'experimental.RunsChange',
25+
tf_backend.runsStore.getRuns()
26+
);
27+
});

tensorboard/components/plugin_util/test/iframe.ts renamed to tensorboard/components/plugin_lib/runs.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License.
1414
==============================================================================*/
15+
namespace tb.plugin.lib.run {
16+
export async function getRuns() {
17+
return tb.plugin.lib.internal.sendMessage('experimental.GetRuns');
18+
}
1519

16-
import {sendMessage, listen, unlisten, _guestIPC} from '../plugin-guest.js';
17-
18-
const win = window as any;
19-
win.test = {sendMessage, listen, unlisten, _guestIPC};
20+
export function addRunsChangeListener(callback: (runs: string[]) => void) {
21+
return tb.plugin.lib.internal.listen('experimental.RunsChange', callback);
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package(default_visibility = ["//tensorboard:internal"])
2+
3+
load("//tensorboard/defs:web.bzl", "tf_web_library")
4+
5+
licenses(["notice"]) # Apache 2.0
6+
7+
tf_web_library(
8+
name = "test",
9+
testonly = True,
10+
srcs = [
11+
"test.html",
12+
"test.ts",
13+
"testable-iframe.html",
14+
],
15+
path = "/tf-plugin-lib/test",
16+
deps = [
17+
"//tensorboard/components/plugin_lib",
18+
"//tensorboard/components/plugin_lib:host_impls",
19+
"//tensorboard/components/tf_backend",
20+
"//tensorboard/components/tf_imports:web_component_tester",
21+
],
22+
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!--
2+
@license
3+
Copyright 2019 The TensorFlow Authors. All Rights Reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<meta charset="utf-8" />
18+
<script src="../../web-component-tester/browser.js"></script>
19+
20+
<link rel="import" href="../../tf-backend/tf-backend.html" />
21+
<link rel="import" href="../tf-plugin-host-impls.html" />
22+
23+
<script src="test.js"></script>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
16+
async function createIframe(): Promise<HTMLIFrameElement> {
17+
return new Promise<HTMLIFrameElement>((resolve) => {
18+
const iframe = document.createElement('iframe') as HTMLIFrameElement;
19+
document.body.appendChild(iframe);
20+
iframe.src = './testable-iframe.html';
21+
iframe.onload = () => resolve(iframe);
22+
});
23+
}
24+
25+
describe('plugin lib integration', () => {
26+
const {expect} = chai;
27+
28+
beforeEach(async function() {
29+
this.sandbox = sinon.sandbox.create({useFakeServer: true});
30+
this.sandbox.server.respondImmediately = true;
31+
this.iframe = await createIframe();
32+
});
33+
34+
afterEach(function() {
35+
document.body.removeChild(this.iframe);
36+
this.sandbox.restore();
37+
});
38+
39+
describe('tb.plugin.lib.run', () => {
40+
describe('#getRuns', () => {
41+
it('returns list of runs', async function() {
42+
this.sandbox
43+
.stub(tf_backend.runsStore, 'getRuns')
44+
.returns(['foo', 'bar', 'baz']);
45+
46+
const runs = await (this.iframe.contentWindow as any).getRuns();
47+
48+
expect(runs).to.deep.equal(['foo', 'bar', 'baz']);
49+
});
50+
});
51+
describe('#addRunsChangeListener', () => {
52+
it('lets plugins to subscribe to runs change', async function() {
53+
const runsChanged = this.sandbox.stub();
54+
const promise = new Promise((resolve) => {
55+
(this.iframe.contentWindow as any).addRunsChangeListener(resolve);
56+
}).then(runsChanged);
57+
this.sandbox.server.respondWith([
58+
200,
59+
{'Content-Type': 'application/json'},
60+
'["foo", "bar"]',
61+
]);
62+
63+
await tf_backend.runsStore.refresh();
64+
await promise;
65+
66+
expect(runsChanged).to.have.been.calledOnce;
67+
expect(runsChanged).to.have.been.calledWith(['foo', 'bar']);
68+
});
69+
});
70+
});
71+
});
72+
1;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!--
2+
@license
3+
Copyright 2019 The TensorFlow Authors. All Rights Reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<link rel="import" href="../../tf-plugin-lib.html" />
18+
<script>
19+
window.getRuns = tb.plugin.lib.run.getRuns;
20+
window.addRunsChangeListener = tb.plugin.lib.run.addRunsChangeListener;
21+
</script>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!--
2+
@license
3+
Copyright 2019 The TensorFlow Authors. All Rights Reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<link rel="import" href="../tf-backend/tf-backend.html" />
18+
<link rel="import" href="plugin-host.html" />
19+
20+
<script src="runs-host-impl.ts"></script>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!--
2+
@license
3+
Copyright 2019 The TensorFlow Authors. All Rights Reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<link rel="import" href="tf-plugin-lib/plugin-guest.html" />
18+
19+
<script src="runs.js"></script>

tensorboard/components/plugin_util/BUILD

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,12 @@ load("//tensorboard/defs:web.bzl", "tf_web_library")
55
licenses(["notice"]) # Apache 2.0
66

77
tf_web_library(
8-
name = "plugin_host",
9-
srcs = [
10-
"plugin-host.html",
11-
"plugin-host.ts",
12-
],
13-
path = "/tf-plugin",
14-
deps = [
15-
":plugin_lib",
16-
"//tensorboard/components/tf_backend",
17-
],
18-
)
19-
20-
tf_web_library(
21-
name = "plugin_lib",
8+
name = "message",
229
srcs = [
10+
"message.html",
2311
"message.ts",
2412
],
25-
path = "/tf-plugin",
13+
path = "/tf-plugin-lib",
2614
)
2715

2816
tf_web_library(
@@ -31,9 +19,20 @@ tf_web_library(
3119
"plugin-guest.html",
3220
"plugin-guest.ts",
3321
],
34-
path = "/tf-plugin",
35-
visibility = ["//visibility:public"],
36-
deps = [
37-
":plugin_lib",
22+
path = "/tf-plugin-lib",
23+
visibility = [
24+
"//tensorboard/components/plugin_lib:__subpackages__",
25+
"//tensorboard/components/plugin_util/test:__subpackages__",
26+
],
27+
deps = [":message"],
28+
)
29+
30+
tf_web_library(
31+
name = "plugin_host",
32+
srcs = [
33+
"plugin-host.html",
34+
"plugin-host.ts",
3835
],
36+
path = "/tf-plugin-lib",
37+
deps = [":message"],
3938
)

0 commit comments

Comments
 (0)