-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
70a3f55
commit 986e7e3
Showing
2 changed files
with
40 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* If the consumers setup is to use webpack-hot-middleware with a custom express server | ||
* we want to bind onto the EventSource for our updates. | ||
* | ||
* The EventSource is not attached immediately, so we have a timeout + retry mechanism. | ||
* This is also a safe approach for consumers who are not using webpack-hot-middleware. It'll be a noop after 3s. | ||
*/ | ||
const MAX_RETRIES = 3; | ||
let retries = 0; | ||
|
||
function tryLoadingWHMEventSource() { | ||
setTimeout(() => { | ||
if (!window.__whmEventSourceWrapper) { | ||
if (retries >= MAX_RETRIES) return; | ||
retries++; | ||
tryLoadingWHMEventSource(); | ||
return; | ||
} | ||
|
||
const eventSource = Object.values(window.__whmEventSourceWrapper)[0]; | ||
eventSource.addMessageListener(event => { | ||
try { | ||
const message = JSON.parse(event.data); | ||
messageHandler(message); | ||
} catch (e) { | ||
// heartbeat | ||
if (event.data === '💓') { | ||
return; | ||
} | ||
|
||
console.error(e); | ||
} | ||
}); | ||
}, 1000); | ||
} | ||
|
||
module.exports = tryLoadingWHMEventSource; |