react-broadcast-sync 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [1.1.1](https://github.com/IdanShalem/react-broadcast-sync/compare/v1.1.0...v1.1.1) (2025-06-03)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * fixed sync problem in counter demo app ([481bda7](https://github.com/IdanShalem/react-broadcast-sync/commit/481bda79f55b3b902861877d4446bc4adaa76941))
7
+
1
8
  # [1.1.0](https://github.com/IdanShalem/react-broadcast-sync/compare/v1.0.0...v1.1.0) (2025-06-03)
2
9
 
3
10
 
@@ -26,6 +26,7 @@ export const useCounterBroadcast = () => {
26
26
  const [count, setCount] = useState<number>(0);
27
27
  const [isSyncing, setIsSyncing] = useState(false);
28
28
  const [showCheckmark, setShowCheckmark] = useState(false);
29
+ const lastTimestampRef = useRef<number>(0);
29
30
 
30
31
  const syncTimeoutRef = useRef<NodeJS.Timeout>();
31
32
  const checkmarkTimeoutRef = useRef<NodeJS.Timeout>();
@@ -33,7 +34,7 @@ export const useCounterBroadcast = () => {
33
34
  // Update count when new messages arrive
34
35
  useEffect(() => {
35
36
  const lastMessage = messages[messages.length - 1];
36
- if (lastMessage?.message !== undefined) {
37
+ if (lastMessage?.message !== undefined && lastMessage.timestamp > lastTimestampRef.current) {
37
38
  // Clear any existing timeouts
38
39
  if (syncTimeoutRef.current) {
39
40
  clearTimeout(syncTimeoutRef.current);
@@ -46,8 +47,9 @@ export const useCounterBroadcast = () => {
46
47
  setIsSyncing(true);
47
48
  setShowCheckmark(false);
48
49
 
49
- // Update count
50
+ // Update count and timestamp
50
51
  setCount(lastMessage.message);
52
+ lastTimestampRef.current = lastMessage.timestamp;
51
53
 
52
54
  // Show checkmark after a short delay
53
55
  syncTimeoutRef.current = setTimeout(() => {
@@ -59,17 +61,17 @@ export const useCounterBroadcast = () => {
59
61
  setShowCheckmark(false);
60
62
  }, 1000);
61
63
  }, 500);
62
-
63
- // Cleanup
64
- return () => {
65
- if (syncTimeoutRef.current) {
66
- clearTimeout(syncTimeoutRef.current);
67
- }
68
- if (checkmarkTimeoutRef.current) {
69
- clearTimeout(checkmarkTimeoutRef.current);
70
- }
71
- };
72
64
  }
65
+
66
+ // Cleanup
67
+ return () => {
68
+ if (syncTimeoutRef.current) {
69
+ clearTimeout(syncTimeoutRef.current);
70
+ }
71
+ if (checkmarkTimeoutRef.current) {
72
+ clearTimeout(checkmarkTimeoutRef.current);
73
+ }
74
+ };
73
75
  }, [messages]);
74
76
 
75
77
  const increment = () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-broadcast-sync",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "A lightweight React hook and provider for syncing state across browser tabs using BroadcastChannel API.",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",