Skip to content

Commit cea485a

Browse files
committed
fixup! Add pub-sub history rewind example
1 parent 64d642c commit cea485a

File tree

4 files changed

+47
-20
lines changed

4 files changed

+47
-20
lines changed

examples/pub-sub-rewind/javascript/src/script.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,21 @@ async function updateRandomOdds() {
168168
return;
169169
}
170170

171+
let running = true;
172+
window.addEventListener('beforeunload', () => {
173+
running = false;
174+
});
175+
171176
for (let i = 0; i < 20; i++) {
177+
if (!running) break;
172178
const delayTime = 5000;
173179
await new Promise((resolve) => {
174-
setTimeout(resolve, delayTime);
180+
const timeoutId = setTimeout(resolve, delayTime);
181+
// Allow for cleanup
182+
return () => {
183+
clearTimeout(timeoutId);
184+
running = false;
185+
};
175186
});
176187

177188
const markets = ['homeWin', 'draw', 'awayWin', 'nextGoal'];
@@ -190,6 +201,8 @@ async function updateRandomOdds() {
190201
});
191202

192203
newOdds.match.timestamp = new Date().toISOString();
193-
await channel.publish('odds', newOdds);
204+
if (running) {
205+
await channel.publish('odds', newOdds);
206+
}
194207
}
195208
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
/** @type {import('tailwindcss').Config} */
22
module.exports = {
3-
content: [
4-
"./index.html",
5-
"./src/**/*.{js,jsx,ts,tsx}",
6-
],
3+
content: ['./index.html', './src/**/*.{js,jsx,ts,tsx}'],
74
theme: {
85
extend: {},
96
},
107
plugins: [],
11-
}
12-
8+
};

examples/pub-sub-rewind/react/src/app/odds/page.tsx

+17-5
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,18 @@ function LiveMatch({ channelName }: { channelName: string }) {
5353
});
5454

5555
useEffect(() => {
56-
const updateRandomOdds = async () => {
57-
if (!matchData) return;
56+
if (!matchData) return;
57+
let isMounted = true;
5858

59+
const updateRandomOdds = async () => {
5960
for (let i = 0; i < 20; i++) {
61+
if (!isMounted) break;
62+
6063
const delay = Math.floor(Math.random() * 3000) + 2000;
61-
await new Promise(resolve => setTimeout(resolve, delay));
64+
await new Promise(resolve => {
65+
const timeoutId = setTimeout(resolve, delay);
66+
return () => clearTimeout(timeoutId);
67+
});
6268

6369
const markets = ['homeWin', 'draw', 'awayWin', 'nextGoal'];
6470
const numMarketsToUpdate = Math.floor(Math.random() * 3) + 1;
@@ -76,12 +82,18 @@ function LiveMatch({ channelName }: { channelName: string }) {
7682
});
7783

7884
newOdds.match.timestamp = new Date().toISOString();
79-
await channel.publish('odds', newOdds);
85+
if (isMounted) {
86+
await channel.publish('odds', newOdds);
87+
}
8088
}
8189
};
8290

8391
updateRandomOdds();
84-
}, [matchData?.match.homeTeam]);
92+
93+
return () => {
94+
isMounted = false;
95+
};
96+
}, [matchData, channel]);
8597

8698
return (
8799
<div className="min-h-screen bg-gray-100 p-8">

examples/pub-sub-rewind/react/src/app/page.tsx

+13-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useRouter, useSearchParams } from 'next/navigation';
66
import { useState } from 'react';
77

88
export default function Home() {
9+
const [alerts, setAlerts] = useState<Array<{id: number, message: string}>>([]);
910
const router = useRouter();
1011
const [isLoading, setIsLoading] = useState(false);
1112
const searchParams = useSearchParams();
@@ -59,16 +60,13 @@ export default function Home() {
5960
currentOdds.match.timestamp = new Date().toISOString();
6061
await channel.publish('odds', currentOdds);
6162

62-
// Show alert for each publish
63-
const alert = document.createElement('div');
64-
alert.className = 'fixed bottom-4 right-4 bg-green-500 text-white px-4 py-2 rounded shadow-lg transition-opacity duration-500';
65-
alert.textContent = `Update ${i + 1}/10: New odds published`;
66-
document.body.appendChild(alert);
63+
// Use React state for alerts
64+
const alertMessage = `Update ${i + 1}/10: New odds published`;
65+
setAlerts(prev => [...prev, { id: Date.now(), message: alertMessage }]);
6766

6867
// Remove alert after 2 seconds
6968
setTimeout(() => {
70-
alert.style.opacity = '0';
71-
setTimeout(() => alert.remove(), 500);
69+
setAlerts(prev => prev.filter(alert => alert.id !== Date.now()));
7270
}, 2000);
7371

7472
await new Promise(resolve => setTimeout(resolve, 1000));
@@ -97,6 +95,14 @@ export default function Home() {
9795
</button>
9896
</div>
9997
</div>
98+
{alerts.map(alert => (
99+
<div
100+
key={alert.id}
101+
className="fixed bottom-4 right-4 bg-green-500 text-white px-4 py-2 rounded shadow-lg transition-opacity duration-500"
102+
>
103+
{alert.message}
104+
</div>
105+
))}
100106
</div>
101107
);
102108
}

0 commit comments

Comments
 (0)