Fix offline mode and move to optional chaining for session socket

This commit is contained in:
Mitchell McCaffrey 2021-01-01 12:44:56 +11:00
parent b13b46b17d
commit b9876c6a38
5 changed files with 14 additions and 34 deletions

View File

@ -15,14 +15,10 @@ export function PartyProvider({ session, children }) {
}
}
if (session.socket) {
session.socket.on("party_state", handleSocketPartyState);
}
session.socket?.on("party_state", handleSocketPartyState);
return () => {
if (session.socket) {
session.socket.off("party_state", handleSocketPartyState);
}
session.socket?.off("party_state", handleSocketPartyState);
};
});

View File

@ -73,19 +73,13 @@ export function PlayerProvider({ session, children }) {
}
session.on("connected", handleSocketConnect);
if (session.socket) {
session.socket.on("connect", handleSocketConnect);
session.socket.on("reconnect", handleSocketConnect);
}
session.socket?.on("connect", handleSocketConnect);
session.socket?.on("reconnect", handleSocketConnect);
return () => {
session.off("connected", handleSocketConnect);
if (session.socket) {
session.socket.off("connect", handleSocketConnect);
session.socket.off("reconnect", handleSocketConnect);
}
session.socket?.off("connect", handleSocketConnect);
session.socket?.off("reconnect", handleSocketConnect);
};
});

View File

@ -23,13 +23,9 @@ function useNetworkedState(defaultState, session, eventName) {
_setState(data);
}
if (session.socket) {
session.socket.on(eventName, handleSocketEvent);
}
session.socket?.on(eventName, handleSocketEvent);
return () => {
if (session.socket) {
session.socket.off(eventName, handleSocketEvent);
}
session.socket?.off(eventName, handleSocketEvent);
};
}, [session.socket, eventName]);

View File

@ -174,9 +174,9 @@ function NetworkedMapAndTokens({ session }) {
if (newMap && newMap.type === "file") {
const { file, resolutions, ...rest } = newMap;
session.socket.emit("map", rest);
session.socket?.emit("map", rest);
} else {
session.socket.emit("map", newMap);
session.socket?.emit("map", newMap);
}
if (!newMap || !newMapState) {
@ -395,16 +395,12 @@ function NetworkedMapAndTokens({ session }) {
}
session.on("peerData", handlePeerData);
session.on("peerDataProgress", handlePeerDataProgress);
if (session.socket) {
session.socket.on("map", handleSocketMap);
}
session.socket?.on("map", handleSocketMap);
return () => {
session.off("peerData", handlePeerData);
session.off("peerDataProgress", handlePeerDataProgress);
if (session.socket) {
session.socket.off("map", handleSocketMap);
}
session.socket?.off("map", handleSocketMap);
};
});

View File

@ -114,12 +114,10 @@ function NetworkedMapPointer({ session, active, gridSize }) {
}
}
if (session.socket) {
session.socket.on("player_pointer", handleSocketPlayerPointer);
}
session.socket?.on("player_pointer", handleSocketPlayerPointer);
return () => {
session.socket.off("player_pointer", handleSocketPlayerPointer);
session.socket?.off("player_pointer", handleSocketPlayerPointer);
};
}, [session]);