Add vuex actions/getters for chat messages
This commit is contained in:
parent
ad4d654abd
commit
9b9904adbd
4 changed files with 41 additions and 9 deletions
|
@ -1,7 +1,14 @@
|
||||||
import { ActionTree } from "vuex";
|
import { ActionTree } from "vuex";
|
||||||
import { AppState } from "../types";
|
import { AppState } from "../types";
|
||||||
import { NetworkState, StartServerOptions, ConnectOptions } from "./types";
|
import { NetworkState, StartServerOptions, ConnectOptions } from "./types";
|
||||||
import { PeerServer, LocalClient, PeerClient } from "@/network";
|
import {
|
||||||
|
PeerServer,
|
||||||
|
LocalClient,
|
||||||
|
PeerClient,
|
||||||
|
NetworkMessage,
|
||||||
|
Client,
|
||||||
|
ChatMessage
|
||||||
|
} from "@/network";
|
||||||
|
|
||||||
const actions: ActionTree<NetworkState, AppState> = {
|
const actions: ActionTree<NetworkState, AppState> = {
|
||||||
startServer({ commit }, options: StartServerOptions) {
|
startServer({ commit }, options: StartServerOptions) {
|
||||||
|
@ -23,6 +30,21 @@ const actions: ActionTree<NetworkState, AppState> = {
|
||||||
commit("connectionError", err);
|
commit("connectionError", err);
|
||||||
});
|
});
|
||||||
client.connect(options.serverID);
|
client.connect(options.serverID);
|
||||||
|
},
|
||||||
|
|
||||||
|
sendChatMessage({ commit, dispatch, getters }, message: ChatMessage) {
|
||||||
|
if (getters.connectionType == "none") {
|
||||||
|
throw new Error("not connected");
|
||||||
|
}
|
||||||
|
dispatch("sendMessage", message);
|
||||||
|
commit("receivedChatMessage", message);
|
||||||
|
},
|
||||||
|
|
||||||
|
sendMessage({ getters }, message: NetworkMessage) {
|
||||||
|
if (getters.connectionType == "none") {
|
||||||
|
throw new Error("not connected");
|
||||||
|
}
|
||||||
|
(getters.client as Client).send(message);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { GetterTree } from "vuex";
|
import { GetterTree } from "vuex";
|
||||||
import { AppState } from "../types";
|
import { AppState } from "../types";
|
||||||
import { NetworkState } from "./types";
|
import { NetworkState } from "./types";
|
||||||
|
import { Client } from "@/network";
|
||||||
|
|
||||||
const getters: GetterTree<NetworkState, AppState> = {
|
const getters: GetterTree<NetworkState, AppState> = {
|
||||||
peerID(state): string | null {
|
peerID(state): string | null {
|
||||||
|
@ -23,6 +24,16 @@ const getters: GetterTree<NetworkState, AppState> = {
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
client(state): Client | null {
|
||||||
|
switch (state.peerType) {
|
||||||
|
case "server":
|
||||||
|
return state.local;
|
||||||
|
case "client":
|
||||||
|
return state.peer;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
|
||||||
connectionType(state): "client" | "server" | "none" {
|
connectionType(state): "client" | "server" | "none" {
|
||||||
return state.peerType;
|
return state.peerType;
|
||||||
},
|
},
|
||||||
|
|
|
@ -5,7 +5,7 @@ import {
|
||||||
ClientNetworkState,
|
ClientNetworkState,
|
||||||
ConnectionStatus
|
ConnectionStatus
|
||||||
} from "./types";
|
} from "./types";
|
||||||
import { LocalClient, PeerServer, PeerClient } from "@/network";
|
import { LocalClient, PeerServer, PeerClient, ChatMessage } from "@/network";
|
||||||
|
|
||||||
const mutations: MutationTree<NetworkState> = {
|
const mutations: MutationTree<NetworkState> = {
|
||||||
becomeServer(state, payload: { local: LocalClient; server: PeerServer }) {
|
becomeServer(state, payload: { local: LocalClient; server: PeerServer }) {
|
||||||
|
@ -28,6 +28,10 @@ const mutations: MutationTree<NetworkState> = {
|
||||||
connectionError(state, error) {
|
connectionError(state, error) {
|
||||||
(state as ClientNetworkState).connectionStatus = "error";
|
(state as ClientNetworkState).connectionStatus = "error";
|
||||||
(state as ClientNetworkState).connectionError = error;
|
(state as ClientNetworkState).connectionError = error;
|
||||||
|
},
|
||||||
|
|
||||||
|
receivedChatMessage(state, message: ChatMessage) {
|
||||||
|
state.chatLog.push(message);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,8 @@ import {
|
||||||
PeerServer,
|
PeerServer,
|
||||||
LocalClient,
|
LocalClient,
|
||||||
RoomInfo,
|
RoomInfo,
|
||||||
PeerMetadata
|
PeerMetadata,
|
||||||
|
ChatMessage
|
||||||
} from "@/network";
|
} from "@/network";
|
||||||
import Peer from "peerjs";
|
import Peer from "peerjs";
|
||||||
|
|
||||||
|
@ -13,12 +14,6 @@ export type ConnectionStatus =
|
||||||
| "disconnected"
|
| "disconnected"
|
||||||
| "error";
|
| "error";
|
||||||
|
|
||||||
export interface ChatMessage {
|
|
||||||
who: string;
|
|
||||||
to: string;
|
|
||||||
message: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface SharedNetworkState {
|
export interface SharedNetworkState {
|
||||||
chatLog: ChatMessage[];
|
chatLog: ChatMessage[];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue