One more socket listener moved to Portal (#1507)

* room-user-change listener moved to Portal

* Eliminate useless checks

* Update src/components/App.tsx

Co-Authored-By: Lipis <lipiridis@gmail.com>

Co-authored-by: Lipis <lipiridis@gmail.com>
This commit is contained in:
Kent Beck 2020-04-28 09:49:00 -07:00 committed by GitHub
parent 77f76a263b
commit dd1dfc5950
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 18 deletions

View File

@ -971,10 +971,19 @@ class App extends React.Component<any, AppState> {
} }
initialize(); initialize();
}); });
this.portal.socket!.on("room-user-change", (clients: string[]) => {
this.setState({
isCollaborating: true,
isLoading: opts.showLoadingState ? true : this.state.isLoading,
});
}
};
// Portal-only
setCollaborators(sockets: string[]) {
this.setState((state) => { this.setState((state) => {
const collaborators: typeof state.collaborators = new Map(); const collaborators: typeof state.collaborators = new Map();
for (const socketID of clients) { for (const socketID of sockets) {
if (state.collaborators.has(socketID)) { if (state.collaborators.has(socketID)) {
collaborators.set(socketID, state.collaborators.get(socketID)!); collaborators.set(socketID, state.collaborators.get(socketID)!);
} else { } else {
@ -986,14 +995,7 @@ class App extends React.Component<any, AppState> {
collaborators, collaborators,
}; };
}); });
});
this.setState({
isCollaborating: true,
isLoading: opts.showLoadingState ? true : this.state.isLoading,
});
} }
};
private broadcastMouseLocation = (payload: { private broadcastMouseLocation = (payload: {
pointerCoords: SocketUpdateDataSource["MOUSE_LOCATION"]["payload"]["pointerCoords"]; pointerCoords: SocketUpdateDataSource["MOUSE_LOCATION"]["payload"]["pointerCoords"];

View File

@ -21,16 +21,19 @@ class Portal {
this.roomKey = key; this.roomKey = key;
// Initialize socket listeners (moving from App) // Initialize socket listeners (moving from App)
this.socket!.on("init-room", () => { this.socket.on("init-room", () => {
if (this.socket) { if (this.socket) {
this.socket.emit("join-room", this.roomID); this.socket.emit("join-room", this.roomID);
this.app.restoreUserName(); this.app.restoreUserName();
} }
}); });
this.socket!.on("new-user", async (_socketID: string) => { this.socket.on("new-user", async (_socketID: string) => {
this.app.broadcastScene(SCENE.INIT); this.app.broadcastScene(SCENE.INIT);
}); });
this.socket.on("room-user-change", (clients: string[]) => {
this.app.setCollaborators(clients);
});
} }
close() { close() {