react-broadcast-sync 1.1.0 → 1.1.2
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,17 @@
|
|
|
1
|
+
## [1.1.2](https://github.com/IdanShalem/react-broadcast-sync/compare/v1.1.1...v1.1.2) (2025-06-03)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* added demo gif presentation ([23781aa](https://github.com/IdanShalem/react-broadcast-sync/commit/23781aa4b61ab30ae9c37fd2b017446bc9cf039e))
|
|
7
|
+
|
|
8
|
+
## [1.1.1](https://github.com/IdanShalem/react-broadcast-sync/compare/v1.1.0...v1.1.1) (2025-06-03)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* fixed sync problem in counter demo app ([481bda7](https://github.com/IdanShalem/react-broadcast-sync/commit/481bda79f55b3b902861877d4446bc4adaa76941))
|
|
14
|
+
|
|
1
15
|
# [1.1.0](https://github.com/IdanShalem/react-broadcast-sync/compare/v1.0.0...v1.1.0) (2025-06-03)
|
|
2
16
|
|
|
3
17
|
|
package/README.md
CHANGED
|
@@ -32,6 +32,10 @@
|
|
|
32
32
|
</a>
|
|
33
33
|
</p>
|
|
34
34
|
|
|
35
|
+
<p align="center">
|
|
36
|
+
<img src="https://raw.githubusercontent.com/IdanShalem/react-broadcast-sync/main/demo/react-broadcast-sync-demo/public/assets/react-broadcast-sync-demo-video.gif" alt="React Broadcast Sync Demo" width="600" />
|
|
37
|
+
</p>
|
|
38
|
+
|
|
35
39
|
Easily sync UI state or user events across browser tabs in React apps — notifications, presence, forms, and more. This package provides a clean and type-safe abstraction over the native API, enabling efficient, scoped, and reliable cross-tab messaging.
|
|
36
40
|
|
|
37
41
|
## 📚 Table of Contents
|
|
Binary file
|
|
@@ -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