/**
* @module flitter-socket/ServerClientTransaction
*/
const Transaction = require('./Transaction')
/**
* Specific transaction that is sent from the server to the client,
* waiting for data from the client.
* @extends module:flitter-socket/Transaction~Transaction
*/
class ServerClientTransaction extends Transaction {
/**
* Initialize the transaction. Set the outgoing data and the endpoint.
* @param {{data: object, endpoint: string}} data - information to load into this transaction
* @param {module:flitter-socket/ConnectionManager~ConnectionManager} cm - the associated connection manager
*/
constructor(data, cm) {
data.outgoing = data.data
super(data, cm)
/**
* The endpoint of this transaction. Typically, corresponds to the name of
* a method in a controller somewhere.
* @type {string}
*/
this.endpoint = data.endpoint
/**
* Callback function called when the client sends a valid response.
* Use this.handler() to get/set.
* @type {function}
* @private
*/
this._handler = function(){}
this.type = 'response'
}
/**
* Get or set the callback function. If no function is provided, returns the current callback function.
* If one is specified, set this._handler and return this for chaining.
* @param {function} [fn]
* @returns {ServerClientTransaction|function} - if fn is specified, this; otherwise, the current callback function
*/
handler(fn){
if ( !fn ) return this._handler
this._handler = fn
return this
}
/**
* Called when data is received from the client. Sets this.received to true and calls
* the callback handler.
* @param {object} data
*/
receipt(data){
this.received = true
return this._handler(this, this.socket, data)
}
}
module.exports = exports = ServerClientTransaction