aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/App.js
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2019-04-17 16:30:29 +0300
committerGravatar Vitaly Takmazov2023-01-13 10:37:54 +0300
commit2f9a1f07798f2ed24993034012330ff7893c9716 (patch)
tree5987c11c5731ea218b935559252e76d15fa31d1c /vnext/src/App.js
parent24b390cc7b2fb675daaad9ba904cf87b201b0542 (diff)
Fix EventSource
Diffstat (limited to 'vnext/src/App.js')
-rw-r--r--vnext/src/App.js35
1 files changed, 21 insertions, 14 deletions
diff --git a/vnext/src/App.js b/vnext/src/App.js
index 4e40f9f1..e461152b 100644
--- a/vnext/src/App.js
+++ b/vnext/src/App.js
@@ -39,25 +39,32 @@ export default function App() {
});
};
- const [es, setEs] = useState();
+ const [es, setEs] = useState({});
+
useEffect(() => {
const { hash } = visitor;
if (hash) {
me().then(visitor => auth(visitor));
- }
- const eventParams = { hash: visitor.hash };
- let url = new URL(`https://api.juick.com/events?${qs.stringify(eventParams)}`);
- let es = new EventSource(url);
- es.onopen = () => {
- console.log('online');
- es.addEventListener('read', updateStatus);
- };
- es.onerror = () => {
- es.removeEventListener('read', updateStatus);
+ if ('EventSource' in window) {
+ const eventParams = { hash: hash };
+ let url = new URL(`https://juick.com/api/events?${qs.stringify(eventParams)}`);
+ let es = new EventSource(url);
+ es.onopen = () => {
+ console.log('online');
+ };
+ es.onerror = () => {
+ es.removeEventListener('read', updateStatus);
+ };
+ es.addEventListener('read', updateStatus);
+ setEs(es);
+ }
};
- setEs(es);
- }, [visitor]);
-
+ return (() => {
+ if (es.removeEventListener) {
+ es.removeEventListener('read', updateStatus);
+ }
+ });
+ }, [visitor.hash]);
let search = (history, pathname, searchString) => {
let location = {};