@awcrotwell/motion
Version:
Motion allows you to build reactive, real-time frontend UI components in your Amber application using pure Crystal that are reusable, testable & encapsulated. For brevity, we will call them MotionComponents.
99 lines (98 loc) • 3.08 kB
TypeScript
/**
* Class for channel related functions (joining, leaving, subscribing and sending messages)
*/
export declare class Channel {
/**
* @param {String} topic - topic to subscribe to
* @param {Socket} socket - A Socket instance
*/
constructor(topic: any, socket: any);
/**
* Join a channel, subscribe to all channels messages
*/
join(): void;
/**
* Leave a channel, stop subscribing to channel messages
*/
leave(): void;
/**
* Calls all message handlers with a matching subject
*/
handleMessage(msg: any): void;
/**
* Subscribe to a channel subject
* @param {String} subject - subject to listen for: `msg:new`
* @param {function} callback - callback function when a new message arrives
*/
on(subject: any, callback: any): void;
/**
* Send a new message to the channel
* @param {String} subject - subject to send message to: `msg:new`
* @param {Object} payload - payload object: `{message: 'hello'}`
*/
push(subject: any, payload: any): void;
}
/**
* Class for maintaining connection with server and maintaining channels list
*/
export declare class Socket {
/**
* @param {String} endpoint - Websocket endpont used in routes.cr file
*/
constructor(endpoint: any);
/**
* Returns whether or not the last received ping has been past the threshold
*/
_connectionIsStale(): boolean;
/**
* Tries to reconnect to the websocket server using a recursive timeout
*/
_reconnect(): void;
/**
* Returns an incrementing timeout interval based around the number of reconnection retries
*/
_reconnectInterval(): number;
/**
* Sets a recursive timeout to check if the connection is stale
*/
_poll(): void;
/**
* Clear polling timeout and start polling
*/
_startPolling(): void;
/**
* Sets `lastPing` to the curent time
*/
_handlePing(): void;
/**
* Clears reconnect timeout, resets variables an starts polling
*/
_reset(): void;
/**
* Connect the socket to the server, and binds to native ws functions
* @param {Object} params - Optional parameters
* @param {String} params.location - Hostname to connect to, defaults to `window.location.hostname`
* @param {String} parmas.port - Port to connect to, defaults to `window.location.port`
* @param {String} params.protocol - Protocol to use, either 'wss' or 'ws'
*/
connect(params: any): Promise<unknown>;
/**
* Closes the socket connection permanently
*/
disconnect(): void;
/**
* Adds a new channel to the socket channels list
* @param {String} topic - Topic for the channel: `chat_room:123`
*/
channel(topic: any): Channel;
/**
* Message handler for messages received
* @param {MessageEvent} msg - Message received from ws
*/
handleMessage(msg: any): void;
}
declare const _default: {
Channel: typeof Channel;
Socket: typeof Socket;
};
export default _default;