Skip to content

Instantly share code, notes, and snippets.

@KhodeN
Created October 12, 2015 04:01
Show Gist options
  • Save KhodeN/22ec48f9c9cab22db31e to your computer and use it in GitHub Desktop.
Save KhodeN/22ec48f9c9cab22db31e to your computer and use it in GitHub Desktop.
// шлет запрос в сокет
_sendToMediator: function (query) {
var self = this;
return self._getMediator()
.then(function () {
if ( !self.mediator.connected ) {
// пытаемся еще раз подключиться к медиатору
self._getMediator(true);
return self.$q.reject(null);
}
var requestId = _.uniqueId('parse_');
self.mediator.send({
request_id: requestId,
query: query
});
var deferred = self.$q.defer();
self._mediatorRequests[requestId] = deferred;
return deferred.promise;
});
},
// слушает все события с сокета, отправленные этому клиенту
_handleMediatorResponse: function (response) {
if ( !_.has(response, 'request_id') ) {
return this._flushMediatorRequests();
}
var requestId = response.request_id;
var deferred = this._mediatorRequests[requestId];
if ( !deferred ) return;
delete this._mediatorRequests[requestId];
if ( _.has(response, 'data') && !_.isNull(response.data) ) {
deferred.resolve(response.data);
}
else {
deferred.reject(response.error);
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment