Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/store/synchronizationEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import { useStore } from './stateManager';

const CHANNEL_NAME = 'teachlink_state_sync';

/** Shallow equality: primitives compared by value, objects by reference-per-key. */
function shallowEqual(a: any, b: any): boolean {
if (a === b) return true;
if (typeof a !== 'object' || a === null || typeof b !== 'object' || b === null) return false;
const keysA = Object.keys(a);
if (keysA.length !== Object.keys(b).length) return false;
return keysA.every(k => a[k] === b[k]);
}

const SYNC_KEYS = ['user', 'preferences', 'offlineMode', 'lastSynced'] as const;

/**
Expand Down Expand Up @@ -36,9 +45,7 @@ export class SynchronizationEngine {
useStore.subscribe((state: any, prevState: any) => {
if (this.isProcessingSync) return;

const hasChanged = SYNC_KEYS.some(key =>
JSON.stringify((state as any)[key]) !== JSON.stringify((prevState as any)[key])
);
const hasChanged = SYNC_KEYS.some(key => !shallowEqual(state[key], prevState[key]));

if (hasChanged) {
this.broadcastState(state);
Expand Down
Loading