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

lib: add a library for cross-frame communication #2309

Merged
merged 5 commits into from
Sep 24, 2019

Conversation

stephanwlee
Copy link
Contributor

@stephanwlee stephanwlee commented Jun 5, 2019

TensorBoard, in the near future, will load plugin modules in respective
iframe as was discussed in RFC. This change adds a library that
facilitates communication between main TensorBoard and plugin frames.

@stephanwlee stephanwlee force-pushed the plugins branch 5 times, most recently from 39e1214 to b4e0bbd Compare June 10, 2019 15:58
@stephanwlee stephanwlee requested a review from psybuzz September 19, 2019 22:44
tensorboard/components/plugin_util/message.ts Show resolved Hide resolved
tensorboard/components/plugin_util/message.ts Outdated Show resolved Hide resolved

export interface Message {
type: string,
id: string,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps just number 'id'?

}


private postMessage(window: Window, message: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I get that "private" already denotes it is private, but can we be consistent about the '_' prefixes? e.g. either 'postMessage' and 'onMessage' both start with _, or both do not?

tensorboard/components/plugin_util/message.ts Outdated Show resolved Hide resolved
tensorboard/components/plugin_util/plugin-host.ts Outdated Show resolved Hide resolved
TensorBoard, in the near future, will load plugin modules in respective
iframe as was discussed in RFC. This change adds a library that
faciliates communication between main TensorBoard and plugin frames.
Copy link
Contributor

@psybuzz psybuzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

tensorboard/components/plugin_util/message.ts Outdated Show resolved Hide resolved
@@ -46,6 +54,9 @@ export abstract class IPC {
}

private async _onMessage(event: MessageEvent) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: drop '_'?

private id = 0;
private readonly responseWaits = new Map<string, PromiseResolver>();
private readonly listeners = new Map<MessageType, MessageCallback>();

constructor() {
window.addEventListener('message', this._onMessage.bind(this));
const randomArray = new Uint8Array(16);
window.crypto.getRandomValues(randomArray);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is creative!

Our API is good, so let's continue with this. In a followup, it would be neat to try using MessageChannels to replace the crypto-prefixing.
[1] https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel

@davidsoergel
Copy link
Member

Is there any doc about this design? It seems like plugin authors will need instructions to use it effectively (docstrings on public methods, at least).

@psybuzz
Copy link
Contributor

psybuzz commented Sep 24, 2019

There isn't a design doc for a plugin library just yet. When there is some idea on what to expose publicly, there should definitely be a design doc!

That said, this PR seems like a good building block that allows us to experiment. The HostIPC and GuestIPC are not intended to be exposed to plugin authors at all, these just make up a communication layer for passing messages between the main frame & iframes. With this PR, we can locally prototype how a "getRunsList()" command might be sent from a dynamic plugin's iframe.

@stephanwlee
Copy link
Contributor Author

Since nothing is really hooked up to this code, I will merge it. We can discuss in future PRs and possibly revert this change if necessary.

@stephanwlee stephanwlee merged commit fa0d5e2 into tensorflow:master Sep 24, 2019
@stephanwlee stephanwlee deleted the plugins branch September 24, 2019 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants