channel-js: add onChannelOpen hook
@@ -9,6 +9,9 @@ class Channel {
this.onChannelError = (err) => {
console.error('event source error: ', err);
};
+ this.onChannelOpen = (e) => {
+ console.log('open', e);
+ };
}
init() {
@@ -58,6 +61,10 @@ class Channel {
this.onChannelError = onError;
}
+ setOnChannelOpen(onOpen = (e) => {}) {
+ this.onChannelOpen = onOpen;
+ }
+
deleteOnUnload() {
window.addEventListener("unload", (event) => {
this.delete();
@@ -216,6 +223,8 @@ class Channel {
}
}
+ this.eventSource.onopen = this.onChannelOpen;
+
this.eventSource.onerror = e => {
this.delete();
this.init();
