Rework subscription on webapp.
ConfigHandler is more optional now.
Connection is treated as a service to be injected.
Change-Id: I7efea116fe7ebb895ffdc42aedfe0f1f7b05e874
diff --git a/aos/network/www/proxy.ts b/aos/network/www/proxy.ts
index 704fc85..9eb0bb6 100644
--- a/aos/network/www/proxy.ts
+++ b/aos/network/www/proxy.ts
@@ -49,8 +49,11 @@
private rtcPeerConnection: RTCPeerConnection|null = null;
private dataChannel: DataChannel|null = null;
private webSocketUrl: string;
- private configHandler: ConfigHandler|null = null;
- private config: Configuration|null = null;
+
+ private configInternal: Configuration|null = null;
+ // A set of functions that accept the config to handle.
+ private readonly configHandlers = new Set<(config: Configuration) => void>();
+
private readonly handlerFuncs = new Map<string, (data: Uint8Array) => void>();
private readonly handlers = new Set<Handler>();
@@ -59,6 +62,10 @@
this.webSocketUrl = `ws://${server}/ws`;
}
+ addConfigHandler(handler: (config: Configuration) => void): void {
+ this.configHandlers.add(handler);
+ }
+
addHandler(id: string, handler: (data: Uint8Array) => void): void {
this.handlerFuncs.set(id, handler);
}
@@ -72,17 +79,17 @@
'message', (e) => this.onWebSocketMessage(e));
}
+ get config() {
+ return this.config_internal;
+ }
+
// Handle messages on the DataChannel. Handles the Configuration message as
// all other messages are sent on specific DataChannels.
onDataChannelMessage(e: MessageEvent): void {
const fbBuffer = new flatbuffers.ByteBuffer(new Uint8Array(e.data));
- // TODO(alex): handle config updates if/when required
- if (!this.configHandler) {
- const config = Configuration.getRootAsConfiguration(fbBuffer);
- this.config = config;
- this.configHandler = new ConfigHandler(config, this.dataChannel);
- this.configHandler.printConfig();
- return;
+ this.configInternal = Configuration.getRootAsConfiguration(fbBuffer);
+ for (handler of this.configHandlers) {
+ handler(this.configInternal);
}
}
@@ -174,4 +181,13 @@
break;
}
}
+
+ /**
+ * Subscribes to messages.
+ * @param a Finished flatbuffer.Builder containing a Connect message to send.
+ */
+ sendConnectMessage(builder: any) {
+ const array = builder.assUint8Array();
+ this.dataChannel.send(array.buffer.slice(array.byteOffset));
+ }
}