2019-07-10 04:25:57 +00:00
|
|
|
import getServer, { Server } from '@/api/server/getServer';
|
|
|
|
import { action, Action, createContextStore, thunk, Thunk } from 'easy-peasy';
|
|
|
|
import socket, { SocketStore } from './socket';
|
2019-07-27 22:17:50 +00:00
|
|
|
import { ServerDatabase } from '@/api/server/getServerDatabases';
|
2019-08-04 21:58:31 +00:00
|
|
|
import files, { ServerFileStore } from '@/state/server/files';
|
2019-07-10 04:25:57 +00:00
|
|
|
|
|
|
|
export type ServerStatus = 'offline' | 'starting' | 'stopping' | 'running';
|
|
|
|
|
|
|
|
interface ServerDataStore {
|
|
|
|
data?: Server;
|
2019-08-04 21:58:31 +00:00
|
|
|
getServer: Thunk<ServerDataStore, string, {}, ServerStore, Promise<void>>;
|
2019-07-10 04:25:57 +00:00
|
|
|
setServer: Action<ServerDataStore, Server>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const server: ServerDataStore = {
|
|
|
|
getServer: thunk(async (actions, payload) => {
|
|
|
|
const server = await getServer(payload);
|
|
|
|
actions.setServer(server);
|
|
|
|
}),
|
|
|
|
setServer: action((state, payload) => {
|
|
|
|
state.data = payload;
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
interface ServerStatusStore {
|
|
|
|
value: ServerStatus;
|
|
|
|
setServerStatus: Action<ServerStatusStore, ServerStatus>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const status: ServerStatusStore = {
|
|
|
|
value: 'offline',
|
|
|
|
setServerStatus: action((state, payload) => {
|
|
|
|
state.value = payload;
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
2019-07-27 22:17:50 +00:00
|
|
|
interface ServerDatabaseStore {
|
|
|
|
items: ServerDatabase[];
|
|
|
|
setDatabases: Action<ServerDatabaseStore, ServerDatabase[]>;
|
|
|
|
appendDatabase: Action<ServerDatabaseStore, ServerDatabase>;
|
|
|
|
removeDatabase: Action<ServerDatabaseStore, ServerDatabase>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const databases: ServerDatabaseStore = {
|
|
|
|
items: [],
|
|
|
|
setDatabases: action((state, payload) => {
|
|
|
|
state.items = payload;
|
|
|
|
}),
|
|
|
|
appendDatabase: action((state, payload) => {
|
|
|
|
state.items = state.items.filter(item => item.id !== payload.id).concat(payload);
|
|
|
|
}),
|
|
|
|
removeDatabase: action((state, payload) => {
|
2019-08-04 21:58:31 +00:00
|
|
|
state.items = state.items.filter(item => item.id !== payload.id);
|
2019-07-27 22:17:50 +00:00
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
2019-07-10 04:25:57 +00:00
|
|
|
export interface ServerStore {
|
|
|
|
server: ServerDataStore;
|
2019-07-27 22:17:50 +00:00
|
|
|
databases: ServerDatabaseStore;
|
2019-08-04 21:58:31 +00:00
|
|
|
files: ServerFileStore;
|
2019-07-10 04:25:57 +00:00
|
|
|
socket: SocketStore;
|
|
|
|
status: ServerStatusStore;
|
|
|
|
clearServerState: Action<ServerStore>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ServerContext = createContextStore<ServerStore>({
|
|
|
|
server,
|
|
|
|
socket,
|
|
|
|
status,
|
2019-07-27 22:17:50 +00:00
|
|
|
databases,
|
2019-08-04 21:58:31 +00:00
|
|
|
files,
|
2019-07-10 04:25:57 +00:00
|
|
|
clearServerState: action(state => {
|
|
|
|
state.server.data = undefined;
|
2019-07-27 22:17:50 +00:00
|
|
|
state.databases.items = [];
|
2019-07-10 04:25:57 +00:00
|
|
|
|
|
|
|
if (state.socket.instance) {
|
|
|
|
state.socket.instance.removeAllListeners();
|
|
|
|
state.socket.instance.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
state.socket.instance = null;
|
|
|
|
state.socket.connected = false;
|
|
|
|
}),
|
|
|
|
}, { name: 'ServerStore' });
|