Skip to content

Commit

Permalink
#911 Only notify RMP save change on user change
Browse files Browse the repository at this point in the history
  • Loading branch information
thekingofcity committed Jan 2, 2025
1 parent 85391f6 commit da07d9f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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));
Expand Down
26 changes: 24 additions & 2 deletions src/util/rmt-save.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<NodeAttributes, EdgeAttributes, GraphAttributes>) => {
if (!defaultGraphHash) {
// top-level await is not supported so we are computing it in the first call
const emptyGraph = new MultiDirectedGraph<NodeAttributes, EdgeAttributes, GraphAttributes>().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,
Expand All @@ -46,6 +62,7 @@ export const onRMPSaveUpdate = async (graph: SerializedGraph<NodeAttributes, Edg

// Token returned from will be handled in registerOnRMTTokenResponse.
export const requestToken = async () => {
logger.debug('Requesting token from RMT');
saveManagerChannel.postMessage({
type: SaveManagerEventType.TOKEN_REQUEST,
from: 'rmp',
Expand All @@ -62,13 +79,16 @@ export const registerOnRMTTokenResponse = async (store: ReturnType<typeof create
const eventHandler = async (ev: MessageEvent<SaveManagerEvent>) => {
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;
Expand All @@ -82,6 +102,7 @@ export const registerOnRMTTokenResponse = async (store: ReturnType<typeof create
},
});
if (rep.status !== 200) {
logger.debug('Token is invalid, expiring the login state');
store.dispatch(setState('expired'));
store.dispatch(setActiveSubscriptions(defaultActiveSubscriptions));
return;
Expand All @@ -99,6 +120,7 @@ export const registerOnRMTTokenResponse = async (store: ReturnType<typeof create
}
}
store.dispatch(setActiveSubscriptions(activeSubscriptions));
logger.debug(`Token is valid, setting active subscriptions: ${JSON.stringify(activeSubscriptions)}`);
}
};
saveManagerChannel.addEventListener('message', eventHandler);
Expand Down

0 comments on commit da07d9f

Please sign in to comment.