@state-sync/js-client
Version:
133 lines (132 loc) • 3.92 kB
TypeScript
import SyncAreaConfig from './SyncAreaConfig';
/**
* Generic representation of messages flying between client and server
*/
export interface Message {
/**
* All messages are identified by type. Read about discriminated unions first
* @link https://www.typescriptlang.org/docs/handbook/advanced-types.html#discriminated-unions
*/
type: string;
}
/**
* Event message is come form server side and signals about some model changes do not related to user actions.
*/
export interface EventMessage extends Message {
}
/**
* Request message sent by client. Server must respond with one of response messages.
*/
export declare abstract class RequestMessage implements Message {
/**
* See Message#type description
*/
type: string;
/**
* Identifier of request in the scope of current session
*/
id: number;
/**
* Construct new request message
* @param {number} id request identifier
* @param {string} type request type
*/
constructor(id: number, type: string);
}
/**
* Response message for user request.
* Response message to not provide any other information except request id and message type.
* Basic flow to all request/response are:
* - client sent request
* - server respond with json path to all user sessions (to sync data across browsers)
* - server respond with response message
*/
export interface ResponseMessage extends Message {
/**
* Identifier of original request
*/
forId: number;
}
/**
* Event received as request of client connect to stat-sync server.
*/
export interface InitSessionResponse extends EventMessage {
/**
* Event id
*/
type: 'init';
/**
* Session token is issued by server and used by client to securely subscribe to changes relevant to session.
*/
sessionToken: string;
/**
* User token is issued by server and used by client to securely subscribe to changes
* relevant to user in any connected session.
*/
userToken: string;
/**
* Version of protocol
*/
protocolVersion: string;
}
export declare class SubscribeAreaRequest extends RequestMessage {
area: string;
constructor(id: number, area: string);
}
export interface SubscribeAreaResponse extends ResponseMessage {
type: 'areaSubscription';
area: string;
config: SyncAreaConfig;
model: object;
}
/**
*
*/
export interface PatchAreaError {
type: 'patchAreaError';
area: string;
error: string;
}
export declare class PatchAreaRequest extends RequestMessage {
area: string;
patch: Array<object>;
constructor(id: number, area: string, patch: Array<object>);
}
export interface PatchAreaEvent extends EventMessage {
type: 'p';
area: string;
patch: Array<object>;
}
export interface PatchAreaResponse extends ResponseMessage {
type: 's';
area: string;
}
export interface SubscribeAreaError extends ResponseMessage {
type: 'areaSubscriptionError';
area: string;
error: string;
}
export declare class SignalRequest extends RequestMessage {
area: string;
private signal;
private parameters;
constructor(id: number, area: string, signal: string, parameters?: any);
}
export interface SignalResponse extends ResponseMessage {
type: 'signalResponse';
area: string;
}
export interface SignalError extends ResponseMessage {
type: 'signalError';
area: string;
error: string;
}
export declare class UnsubscribeAreaRequest extends RequestMessage {
area: string;
constructor(id: number, area: string);
}
export interface UnsubscribeAreaResponse extends ResponseMessage {
type: 'areaUnsubscription';
area: string;
}
export declare type IncomingEvents = InitSessionResponse | PatchAreaError | PatchAreaResponse | PatchAreaEvent | SignalResponse | SignalError | SubscribeAreaError | SubscribeAreaResponse | UnsubscribeAreaResponse;