From da07d9f2e0885bdc90043b8402ca03a0bb23e67f Mon Sep 17 00:00:00 2001 From: thekingofcity <3353040+thekingofcity@users.noreply.github.com> Date: Thu, 2 Jan 2025 22:00:16 +0800 Subject: [PATCH] #911 Only notify RMP save change on user change --- src/index.tsx | 4 ++-- src/util/rmt-save.ts | 26 ++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index b585791e4..b6f119590 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,4 +1,4 @@ -import rmgRuntime from '@railmapgen/rmg-runtime'; +import rmgRuntime, { logger } from '@railmapgen/rmg-runtime'; import { MultiDirectedGraph } from 'graphology'; import React from 'react'; import { createRoot } from 'react-dom/client'; @@ -91,7 +91,7 @@ upgrade(param).then(param => { window.setInterval(() => requestToken(), 15 * 60 * 1000); // 15 mins const accountStateTimeout = window.setTimeout(() => { - // console.log('Account state timeout'); + logger.debug('Timeout 6s after start, logging out'); store.dispatch(setState('logged-out')); store.dispatch(setActiveSubscriptions(defaultActiveSubscriptions)); store.dispatch(setLoginStateTimeout(undefined)); diff --git a/src/util/rmt-save.ts b/src/util/rmt-save.ts index 9d7f262e3..59a031337 100644 --- a/src/util/rmt-save.ts +++ b/src/util/rmt-save.ts @@ -1,3 +1,5 @@ +import { logger } from '@railmapgen/rmg-runtime'; +import { MultiDirectedGraph } from 'graphology'; import { SerializedGraph } from 'graphology-types'; import { EdgeAttributes, GraphAttributes, LocalStorageKey, NodeAttributes } from '../constants/constants'; import { subscription_endpoint } from '../constants/server'; @@ -30,11 +32,25 @@ export const saveManagerChannel = new BroadcastChannel(SAVE_MANAGER_CHANNEL_NAME * Notify RMT only if the graph changes. */ let previousGraphHash: string | undefined; +/** + * The default graph on state initialization will be an empty graph. + * Of course we shouldn't notify RMT if the graph is empty so we record + * and sent the message only if the previousGraphHash is not the defaultGraphHash. + */ +let defaultGraphHash: string | undefined; // Notify rmt to update save when the state is changed. export const onRMPSaveUpdate = async (graph: SerializedGraph) => { + if (!defaultGraphHash) { + // top-level await is not supported so we are computing it in the first call + const emptyGraph = new MultiDirectedGraph().export(); + defaultGraphHash = await createHash(JSON.stringify(emptyGraph)); + logger.debug(`Default graph hash: ${defaultGraphHash}`); + } + const graphHash = await createHash(JSON.stringify(graph)); - if (previousGraphHash && previousGraphHash !== graphHash) { + if (previousGraphHash && previousGraphHash !== defaultGraphHash && previousGraphHash !== graphHash) { + logger.debug(`Notify RMP save change, hash: ${graphHash}`); saveManagerChannel.postMessage({ type: SaveManagerEventType.SAVE_CHANGED, key: LocalStorageKey.PARAM, @@ -46,6 +62,7 @@ export const onRMPSaveUpdate = async (graph: SerializedGraph { + logger.debug('Requesting token from RMT'); saveManagerChannel.postMessage({ type: SaveManagerEventType.TOKEN_REQUEST, from: 'rmp', @@ -62,13 +79,16 @@ export const registerOnRMTTokenResponse = async (store: ReturnType) => { const { type, token, from } = ev.data; if (type === SaveManagerEventType.TOKEN_REQUEST && from === 'rmt') { + logger.debug(`Received token from RMT: ${token}`); + if (store.getState().account.timeout) { - // console.log('Clearing login state timeout'); + logger.debug('Clear login state timeout'); window.clearTimeout(store.getState().account.timeout); store.dispatch(setLoginStateTimeout(undefined)); } if (!token) { + logger.debug('Token is empty, logging out'); store.dispatch(setState('logged-out')); store.dispatch(setActiveSubscriptions(defaultActiveSubscriptions)); return; @@ -82,6 +102,7 @@ export const registerOnRMTTokenResponse = async (store: ReturnType