Begin moving socket listeners to Portal (#1504)

This commit is contained in:
Kent Beck 2020-04-27 10:56:08 -07:00 committed by GitHub
parent c5d412f20a
commit 0baabff41b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 19 deletions

View File

@ -166,7 +166,7 @@ const gesture: Gesture = {
class App extends React.Component<any, AppState> { class App extends React.Component<any, AppState> {
canvas: HTMLCanvasElement | null = null; canvas: HTMLCanvasElement | null = null;
rc: RoughCanvas | null = null; rc: RoughCanvas | null = null;
portal: Portal = new Portal(); portal: Portal = new Portal(this);
lastBroadcastedOrReceivedSceneVersion: number = -1; lastBroadcastedOrReceivedSceneVersion: number = -1;
removeSceneCallback: SceneStateCallbackRemover | null = null; removeSceneCallback: SceneStateCallbackRemover | null = null;
@ -915,19 +915,7 @@ class App extends React.Component<any, AppState> {
roomMatch[2], roomMatch[2],
); );
this.portal.socket!.on("init-room", () => { // All socket listeners are moving to Portal
if (this.portal.socket) {
const username = restoreUsernameFromLocalStorage();
this.portal.socket.emit("join-room", this.portal.roomID);
if (username !== null) {
this.setState({
username,
});
}
}
});
this.portal.socket!.on( this.portal.socket!.on(
"client-broadcast", "client-broadcast",
async (encryptedData: ArrayBuffer, iv: Uint8Array) => { async (encryptedData: ArrayBuffer, iv: Uint8Array) => {
@ -999,9 +987,6 @@ class App extends React.Component<any, AppState> {
}; };
}); });
}); });
this.portal.socket!.on("new-user", async (_socketID: string) => {
this.broadcastScene(SCENE.INIT);
});
this.setState({ this.setState({
isCollaborating: true, isCollaborating: true,
@ -1032,7 +1017,8 @@ class App extends React.Component<any, AppState> {
} }
}; };
private broadcastScene = (sceneType: SCENE.INIT | SCENE.UPDATE) => { // maybe should move to Portal
broadcastScene = (sceneType: SCENE.INIT | SCENE.UPDATE) => {
const data: SocketUpdateDataSource[typeof sceneType] = { const data: SocketUpdateDataSource[typeof sceneType] = {
type: sceneType, type: sceneType,
payload: { payload: {
@ -1059,6 +1045,16 @@ class App extends React.Component<any, AppState> {
}, },
); );
restoreUserName() {
const username = restoreUsernameFromLocalStorage();
if (username !== null) {
this.setState({
username,
});
}
}
// Input handling // Input handling
private onKeyDown = withBatchedUpdates((event: KeyboardEvent) => { private onKeyDown = withBatchedUpdates((event: KeyboardEvent) => {

View File

@ -1,18 +1,36 @@
import { encryptAESGEM } from "../data"; import { encryptAESGEM } from "../data";
import { SocketUpdateData } from "../types"; import { SocketUpdateData } from "../types";
import { BROADCAST } from "../constants"; import { BROADCAST, SCENE } from "../constants";
import App from "./App";
class Portal { class Portal {
app: App;
socket: SocketIOClient.Socket | null = null; socket: SocketIOClient.Socket | null = null;
socketInitialized: boolean = false; // we don't want the socket to emit any updates until it is fully initialized socketInitialized: boolean = false; // we don't want the socket to emit any updates until it is fully initialized
roomID: string | null = null; roomID: string | null = null;
roomKey: string | null = null; roomKey: string | null = null;
constructor(app: App) {
this.app = app;
}
open(socket: SocketIOClient.Socket, id: string, key: string) { open(socket: SocketIOClient.Socket, id: string, key: string) {
this.socket = socket; this.socket = socket;
this.roomID = id; this.roomID = id;
this.roomKey = key; this.roomKey = key;
// Initialize socket listeners (moving from App)
this.socket!.on("init-room", () => {
if (this.socket) {
this.socket.emit("join-room", this.roomID);
this.app.restoreUserName();
}
});
this.socket!.on("new-user", async (_socketID: string) => {
this.app.broadcastScene(SCENE.INIT);
});
} }
close() { close() {