-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(be): feat(fe): add websocket for task logs
Additionally: - Added link for running task, #481 - Added class Listenable and unit tests for it
- Loading branch information
Showing
21 changed files
with
1,080 additions
and
106 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -95,6 +95,7 @@ export default { | |
EventBus.$emit(this.eventName, e); | ||
} | ||
} | ||
this.$emit('close'); | ||
}, | ||
clearFlags() { | ||
|
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
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
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,31 @@ | ||
export default class Listenable { | ||
constructor() { | ||
this.listeners = {}; | ||
} | ||
|
||
addListener(callback) { | ||
// eslint-disable-next-line symbol-description | ||
const id = Symbol(); | ||
this.listeners[id] = callback; | ||
return id; | ||
} | ||
|
||
removeListener(id) { | ||
if (this.listeners[id] == null) { | ||
return false; | ||
} | ||
delete this.listeners[id]; | ||
return true; | ||
} | ||
|
||
callListeners(data) { | ||
Object.getOwnPropertySymbols(this.listeners).forEach((id) => { | ||
const listener = this.listeners[id]; | ||
listener(data); | ||
}); | ||
} | ||
|
||
hasListeners() { | ||
return Object.keys(this.listeners).length > 0; | ||
} | ||
} |
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,30 @@ | ||
import Listenable from '@/lib/Listenable'; | ||
|
||
export default class PubSub { | ||
constructor() { | ||
this.topics = {}; | ||
} | ||
|
||
subscribe(topic, callback) { | ||
if (this.topics[topic] == null) { | ||
this.topics[topic] = new Listenable(); | ||
} | ||
return this.topics[topic].addListener(callback); | ||
} | ||
|
||
unsubscribe(id) { | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const topic in this.topics) { | ||
if (this.topics[topic].removeListener(id)) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
publish(topic, data) { | ||
if (!this.topics[topic]) { | ||
return; | ||
} | ||
this.topics[topic].callListeners(data); | ||
} | ||
} |
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,48 @@ | ||
import Listenable from '@/lib/Listenable'; | ||
|
||
export default class Socket extends Listenable { | ||
constructor(websocketCreator) { | ||
super(); | ||
this.websocketCreator = websocketCreator; | ||
} | ||
|
||
start() { | ||
if (this.ws != null) { | ||
throw new Error('Websocket already started. Please stop it before starting.'); | ||
} | ||
this.ws = this.websocketCreator(); | ||
this.ws.onclose = () => { | ||
setTimeout(() => { | ||
this.start(); | ||
}, 2000); | ||
}; | ||
this.ws.onmessage = ({ data }) => { | ||
try { | ||
this.callListeners(JSON.parse(data)); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
} | ||
|
||
stop() { | ||
this.ws.close(); | ||
delete this.ws; | ||
} | ||
|
||
addListener(callback) { | ||
const isFirstListener = !this.hasListeners(); | ||
const listenerId = super.addListener(callback); | ||
if (isFirstListener) { | ||
this.start(); | ||
} | ||
return listenerId; | ||
} | ||
|
||
removeListener(id) { | ||
super.removeListener(id); | ||
if (!this.hasListeners()) { | ||
this.stop(); | ||
} | ||
} | ||
} |
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,6 @@ | ||
import Socket from '@/lib/Socket'; | ||
|
||
export default new Socket(() => { | ||
const protocol = document.location.protocol === 'https:' ? 'wss' : 'ws'; | ||
return new WebSocket(`${protocol}://${document.location.host}/api/ws`); | ||
}); |
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
Oops, something went wrong.