LoopBackTransport class
Transport mechanism used on server, that directly uses RequestHandler,
used by createLoopBackConnection.
class LoopBackTransport extends Transport {
/**
* RequestFactory is a function like LoopBackRequest.request() that returns
* [Future<LoopBackRequest>].
*/
final _sendLoopBackRequest;
/**
* Id of the user currently authenticated.
*/
final _authenticatedUserId;
/**
* Indicates whether a [LoopBackRequest] is currently on the way.
*/
bool _isRunning = false;
bool _isDirty;
LoopBackTransport(this._sendLoopBackRequest, [this._authenticatedUserId = null]);
markDirty() {
_isDirty = true;
performRequest();
}
bool _shouldSendLoopBackRequest() {
return !_isRunning &&
_isDirty;
}
void _openRequest() {
_isRunning = true;
_isDirty = false;
}
void _closeRequest() {
_isRunning = false;
performRequest();
}
/**
* Begins performing LoopBackRequest. Is not launched if another request is
* already running or the request Queue is empty. Sets [_isRunning] as true
* for the time this request is running and hooks up another request
* after this one.
*/
void performRequest() {
if (!_shouldSendLoopBackRequest()) {
return;
}
_openRequest();
_sendLoopBackRequest(JSON.encode(_prepareRequest()), _authenticatedUserId)
.then((response) {
_handleResponse({'responses': response, 'authenticatedUserId': _authenticatedUserId});
_closeRequest();
}).catchError((e) => _handleError(new FailedRequestException()));
}
}
Extends
Transport > LoopBackTransport
Constructors
new LoopBackTransport(_sendLoopBackRequest, [_authenticatedUserId = null]) #
Methods
dynamic markDirty() #
markDirty() {
_isDirty = true;
performRequest();
}
void performRequest() #
Begins performing LoopBackRequest. Is not launched if another request is already running or the request Queue is empty. Sets _isRunning as true for the time this request is running and hooks up another request after this one.
void performRequest() {
if (!_shouldSendLoopBackRequest()) {
return;
}
_openRequest();
_sendLoopBackRequest(JSON.encode(_prepareRequest()), _authenticatedUserId)
.then((response) {
_handleResponse({'responses': response, 'authenticatedUserId': _authenticatedUserId});
_closeRequest();
}).catchError((e) => _handleError(new FailedRequestException()));
}
dynamic setHandlers(prepareRequest, handleResponse, handleError, [handleDisconnect = null, handleReconnect = null]) #
inherited from Transport
setHandlers(prepareRequest, handleResponse, handleError, [handleDisconnect = null, handleReconnect = null]) {
_prepareRequest = prepareRequest;
_handleResponse = handleResponse;
_handleError = handleError;
_disconnectConnection = handleDisconnect == null ? (){} : handleDisconnect;
_reconnectConnection = handleReconnect == null ? (){} : handleReconnect;
}