Dart Documentationclean_ajax.clientLoopBackTransport

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]) #

Creates a new Object instance.

Object instances have no meaningful state, and are only useful through their identity. An Object instance is equal to itself only.

docs inherited from Object
LoopBackTransport(this._sendLoopBackRequest, [this._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;
}