You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
178 lines
7.2 KiB
178 lines
7.2 KiB
import {BaseAction} from "./BaseAction";
|
|
import {AppAction} from "./AppAction";
|
|
import {CellColorAction, isCellColorAction} from "./CellAction";
|
|
import {SyncedState} from "../state/SyncedState";
|
|
import {isUserActiveColorAction, UserActiveColorAction} from "./UserAction";
|
|
|
|
// TODO: Split into ServerAction and ClientAction files
|
|
|
|
export const SERVER_HELLO = "SERVER_HELLO"
|
|
/** Sent in response to the client's ClientHelloAction when the client has been accepted. */
|
|
export interface ServerHelloAction extends BaseAction {
|
|
readonly type: typeof SERVER_HELLO
|
|
/** The protocol version the server is running on. */
|
|
readonly version: number
|
|
/** The current state of the server as of when the client connected. */
|
|
readonly state: SyncedState
|
|
}
|
|
export function isServerHelloAction(action: AppAction): action is ServerHelloAction {
|
|
return action.type === SERVER_HELLO
|
|
}
|
|
|
|
export const SERVER_GOODBYE = "SERVER_GOODBYE"
|
|
/** Synthesized when the server closes the connection. */
|
|
export interface ServerGoodbyeAction extends BaseAction {
|
|
readonly type: typeof SERVER_GOODBYE
|
|
/** The error code sent with the close message by the server, or -1 if our side crashed.*/
|
|
readonly code: number
|
|
/** The text of the server's goodbye message, or the exception message if our side crashed. */
|
|
readonly reason: string
|
|
/** The current time when this close message was received. */
|
|
readonly currentTime: Date
|
|
}
|
|
export function isServerGoodbyeAction(action: AppAction): action is ServerGoodbyeAction {
|
|
return action.type === SERVER_GOODBYE
|
|
}
|
|
|
|
export const SERVER_REFRESH = "SERVER_REFRESH"
|
|
/** Sent in response to the client's ClientRefreshAction. */
|
|
export interface ServerRefreshAction extends BaseAction {
|
|
readonly type: typeof SERVER_REFRESH
|
|
/** The current state of the server as of when the client's refresh request was processed. */
|
|
readonly state: SyncedState
|
|
}
|
|
export function isServerRefreshAction(action: AppAction): action is ServerRefreshAction {
|
|
return action.type === SERVER_REFRESH
|
|
}
|
|
|
|
export const SERVER_OK = "SERVER_OK"
|
|
/** Sent in response to the client's ClientNestedAction, if it succeeds and is applied to the server map. */
|
|
export interface ServerOKAction extends BaseAction {
|
|
readonly type: typeof SERVER_OK
|
|
/**
|
|
* The IDs of the successful actions.
|
|
* These will always be sent in sequential order - the order in which the server received and applied them.
|
|
* This allows the client to apply that set of actions to its server-state copy and then refresh its local copy.
|
|
*/
|
|
readonly ids: readonly number[]
|
|
}
|
|
export function isServerOKAction(action: AppAction): action is ServerOKAction {
|
|
return action.type === SERVER_OK
|
|
}
|
|
|
|
export interface ActionFailure {
|
|
readonly id: number
|
|
readonly error: string
|
|
}
|
|
|
|
export const SERVER_FAILED = "SERVER_FAILED"
|
|
/** Sent in response to the client's ClientNestedAction, if it fails and has not been applied to the server map. */
|
|
export interface ServerFailedAction extends BaseAction {
|
|
readonly type: typeof SERVER_FAILED
|
|
/** The IDs of the failed actions, all of which failed with the same error. */
|
|
readonly ids: readonly number[]
|
|
/** The error the above actions failed with. */
|
|
readonly error: string
|
|
}
|
|
export function isServerFailedAction(action: AppAction): action is ServerFailedAction {
|
|
return action.type === SERVER_FAILED
|
|
}
|
|
|
|
export enum SocketState {
|
|
CONNECTING = "CONNECTING",
|
|
OPEN = "OPEN",
|
|
}
|
|
export const SERVER_SOCKET_STARTUP = "SERVER_SOCKET_STARTUP"
|
|
/** Synthesized when the websocket begins connecting, i.e., enters the Connecting or Open states.. */
|
|
export interface ServerSocketStartupAction extends BaseAction {
|
|
readonly type: typeof SERVER_SOCKET_STARTUP
|
|
readonly state: SocketState
|
|
}
|
|
export function isServerSocketStartupAction(action: AppAction): action is ServerSocketStartupAction {
|
|
return action.type === SERVER_SOCKET_STARTUP
|
|
}
|
|
|
|
export const SERVER_ACT = "SERVER_ACT"
|
|
/** Sent by the server when another client has performed an action. Never sent for the client's own actions. */
|
|
export interface ServerActAction extends BaseAction {
|
|
readonly type: typeof SERVER_ACT
|
|
readonly actions: readonly SyncableAction[]
|
|
}
|
|
export function isServerActAction(action: AppAction): action is ServerActAction {
|
|
return action.type === SERVER_ACT
|
|
}
|
|
|
|
export type SyncableAction = SendableAction
|
|
export function isSyncableAction(action: AppAction): action is SyncableAction {
|
|
return isSendableAction(action)
|
|
}
|
|
|
|
export type ServerAction =
|
|
ServerHelloAction | ServerGoodbyeAction | ServerRefreshAction |
|
|
ServerOKAction | ServerFailedAction |
|
|
ServerSocketStartupAction | ServerActAction
|
|
export function isServerAction(action: AppAction) {
|
|
return isServerHelloAction(action) || isServerGoodbyeAction(action) || isServerRefreshAction(action)
|
|
|| isServerOKAction(action) || isServerFailedAction(action)
|
|
|| isServerSocketStartupAction(action) || isServerActAction(action)
|
|
}
|
|
|
|
export const CLIENT_HELLO = "CLIENT_HELLO"
|
|
/** Sent when the client connects. */
|
|
export interface ClientHelloAction extends BaseAction {
|
|
readonly type: typeof CLIENT_HELLO
|
|
/** The protocol version the client is running on */
|
|
readonly version: number
|
|
}
|
|
export function isClientHelloAction(action: AppAction): action is ClientHelloAction {
|
|
return action.type === CLIENT_HELLO
|
|
}
|
|
|
|
export const CLIENT_REFRESH = "CLIENT_REFRESH"
|
|
/** Sent when the user requests a refresh, or if a malformed action comes through. */
|
|
export interface ClientRefreshAction extends BaseAction {
|
|
readonly type: typeof CLIENT_REFRESH
|
|
}
|
|
export function isClientRefreshAction(action: AppAction): action is ClientRefreshAction {
|
|
return action.type === CLIENT_REFRESH
|
|
}
|
|
|
|
export const CLIENT_PENDING = "CLIENT_PENDING"
|
|
/** Synthesized when a user action is ready to be sent to the server. */
|
|
export interface ClientPendingAction extends BaseAction {
|
|
readonly type: typeof CLIENT_PENDING
|
|
readonly pending: SendableAction
|
|
}
|
|
export function isClientPendingAction(action: AppAction): action is ClientPendingAction {
|
|
return action.type === CLIENT_PENDING
|
|
}
|
|
|
|
export interface SentAction {
|
|
readonly id: number
|
|
readonly action: SendableAction
|
|
}
|
|
|
|
export const CLIENT_ACT = "CLIENT_ACT"
|
|
/** Sent to the server when the user performs an action. */
|
|
export interface ClientActAction extends BaseAction {
|
|
readonly type: typeof CLIENT_ACT
|
|
readonly actions: readonly SentAction[]
|
|
}
|
|
export function isClientActAction(action: AppAction): action is ClientActAction {
|
|
return action.type === CLIENT_ACT
|
|
}
|
|
|
|
export type SendableAction = CellColorAction | UserActiveColorAction
|
|
export function isSendableAction(action: AppAction): action is SendableAction {
|
|
return isCellColorAction(action) || isUserActiveColorAction(action)
|
|
}
|
|
|
|
export type ClientAction = ClientHelloAction | ClientRefreshAction | ClientPendingAction | ClientActAction
|
|
export function isClientAction(action: AppAction): action is ClientAction {
|
|
return isClientHelloAction(action) || isClientRefreshAction(action) || isClientPendingAction(action) || isClientActAction(action)
|
|
}
|
|
|
|
export type NetworkAction = ServerAction | ClientAction;
|
|
export function isNetworkAction(action: AppAction): action is NetworkAction {
|
|
return isServerAction(action) || isClientAction(action)
|
|
} |