Skip to content

Commit

Permalink
Merge branch 'unregisterMessageListener' of github.com:gunet/wallet-f…
Browse files Browse the repository at this point in the history
…rontend into reliable-storage
  • Loading branch information
pstamatop committed Sep 22, 2023
2 parents d716d2d + ffd6535 commit 89615e7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/components/Notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const Notification = () => {
}, [notification]);

useEffect(() => {
const unregisterMessageListener = onMessageListener()
const unregisterMessageListener = () => onMessageListener()
.then((payload) => {
setNotification({ title: payload?.notification?.title, body: payload?.notification?.body });
})
Expand Down
16 changes: 12 additions & 4 deletions src/hoc/handleServerMessagesGuard.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function handleServerMessagesGuard(Component) {
const socket = new WebSocket(REACT_APP_WS_URL);
const keystore = new useLocalStorageKeystore();
const signingRequestHandlerService = SigningRequestHandlerService();
const [isSocketOpen, setIsSocketOpen] = useState(false);


socket.addEventListener('open', (event) => {
Expand All @@ -24,17 +25,20 @@ export default function handleServerMessagesGuard(Component) {
}
console.log("Sending...")
// send handshake request
socket.send(JSON.stringify({ type: "INIT", appToken: appToken }))
socket.send(JSON.stringify({ type: "INIT", appToken: appToken }));
setIsSocketOpen(true); // Set the state to indicate that the connection is open
});

const waitForHandshake = async () => {
return new Promise((resolve, reject) => {
socket.onmessage = event => {
try {
console.log('--->',event.data.toString());
const { type } = JSON.parse(event.data.toString());
if (type == "FIN_INIT") {
console.log("init fin")
setHandshakeEstablished(true);

resolve({});
}
}
Expand All @@ -46,8 +50,11 @@ export default function handleServerMessagesGuard(Component) {
}

useEffect(() => {
waitForHandshake();
}, []);
if (isSocketOpen) {
waitForHandshake();
// You can perform other actions that depend on the socket being open here.
}
}, [isSocketOpen]);

socket.addEventListener('message', async (event) => {
try {
Expand All @@ -64,14 +71,15 @@ export default function handleServerMessagesGuard(Component) {
}
}
catch(e) {
console.log('failed to parse message')
}
})

console.log('->',handshakeEstablished,appToken);
if (handshakeEstablished === true || !appToken) {
return (<Component {...props} />);
}
else {

return (<Spinner />); // loading component
}
}
Expand Down

0 comments on commit 89615e7

Please sign in to comment.