Handle connecting to websocket instance

Very beta code for handling sockets
This commit is contained in:
Dane Everitt 2019-06-29 16:14:32 -07:00
parent 6618a124e7
commit f0ca8bc3a3
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
15 changed files with 297 additions and 30 deletions

View file

@ -0,0 +1,34 @@
import getServer, { Server } from '@/api/server/getServer';
import { action, Action, thunk, Thunk } from 'easy-peasy';
import socket, { SocketState } from './socket';
export interface ServerState {
data?: Server;
socket: SocketState;
getServer: Thunk<ServerState, string, {}, any, Promise<void>>;
setServer: Action<ServerState, Server>;
clearServerState: Action<ServerState>;
}
const server: ServerState = {
socket,
getServer: thunk(async (actions, payload) => {
const server = await getServer(payload);
actions.setServer(server);
}),
setServer: action((state, payload) => {
state.data = payload;
}),
clearServerState: action(state => {
state.data = undefined;
if (state.socket.instance) {
state.socket.instance.close();
}
state.socket.instance = null;
state.socket.connected = false;
}),
};
export default server;

View file

@ -0,0 +1,22 @@
import { Action, action } from 'easy-peasy';
import Sockette from 'sockette';
export interface SocketState {
instance: Sockette | null;
connected: boolean;
setInstance: Action<SocketState, Sockette | null>;
setConnectionState: Action<SocketState, boolean>;
}
const socket: SocketState = {
instance: null,
connected: false,
setInstance: action((state, payload) => {
state.instance = payload;
}),
setConnectionState: action((state, payload) => {
state.connected = payload;
}),
};
export default socket;