mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
refactor: update socket types
This commit is contained in:
1 parent
bb2a1a145f
commit
ebd713200c
6 files changed
+120
-19
No files matched your search
@@ -18,6 +18,7 @@ function newSession() {
|
||||
discovered: false,
|
||||
})
|
||||
return {
|
||||
_id: '',
|
||||
status: 'inactive',
|
||||
graph,
|
||||
pwd: '',
|
||||
@@ -100,18 +101,10 @@ function updateGraph(event: { discover: FileObject[]; pwd: string }) {
|
||||
}
|
||||
}
|
||||
|
||||
function updateHints(hints: never) {
|
||||
store.session.hints.push(hints)
|
||||
}
|
||||
|
||||
function updateStatus(status: string) {
|
||||
store.session.status = status
|
||||
}
|
||||
|
||||
function setTasks(tasks: never[]) {
|
||||
store.session.tasks = tasks
|
||||
}
|
||||
|
||||
export function useTerminal() {
|
||||
return term
|
||||
}
|
||||
@@ -150,12 +143,6 @@ function setup() {
|
||||
socket.on('graph', (event: { discover: FileObject[]; pwd: string }) => {
|
||||
updateGraph(event)
|
||||
})
|
||||
socket.on('hints', (event: never) => {
|
||||
updateHints(event)
|
||||
})
|
||||
socket.on('tasks', (tasks: never[]) => {
|
||||
setTasks(tasks)
|
||||
})
|
||||
socket.on('status', (event: string) => {
|
||||
updateStatus(event)
|
||||
})
|
||||
|
||||
@@ -7,6 +7,7 @@ export interface Task {
|
||||
}
|
||||
|
||||
export interface Session {
|
||||
_id: string
|
||||
status: string
|
||||
graph: FileGraph
|
||||
pwd: string
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
import { io, Socket } from 'socket.io-client'
|
||||
import { Session } from '../types'
|
||||
|
||||
type SocketCallback = (data: any) => void
|
||||
|
||||
class SocketWrapper {
|
||||
socket: Socket | null
|
||||
listeners: { event: any; callback: any }[]
|
||||
listeners: { event: string; callback: SocketCallback }[]
|
||||
constructor() {
|
||||
this.socket = null
|
||||
this.listeners = []
|
||||
}
|
||||
|
||||
connect(session: any) {
|
||||
connect(session: Session) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
this.disconnect()
|
||||
|
||||
@@ -30,7 +33,7 @@ class SocketWrapper {
|
||||
})
|
||||
}
|
||||
|
||||
on(event: any, callback: any) {
|
||||
on(event: string, callback: SocketCallback) {
|
||||
this.listeners.push({ event, callback })
|
||||
if (this.socket) {
|
||||
this.socket.on(event, callback)
|
||||
@@ -39,13 +42,13 @@ class SocketWrapper {
|
||||
|
||||
bindListeners() {
|
||||
this.listeners.forEach(
|
||||
({ event, callback }: { event: any; callback: any }) => {
|
||||
({ event, callback }: { event: string; callback: SocketCallback }) => {
|
||||
if (this.socket !== null) this.socket.on(event, callback)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
emit(event: any, data: any) {
|
||||
emit(event: string, data: any) {
|
||||
if (!this.socket) {
|
||||
console.warn('Socket is not connected!')
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user