Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
[courier/fetch/segmented] infer the totalSize from the flattened sear…
Browse files Browse the repository at this point in the history
…ch source
  • Loading branch information
Spencer Alger committed Dec 19, 2014
1 parent 668b5cf commit 1fdf755
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
22 changes: 16 additions & 6 deletions src/kibana/components/courier/fetch/request/_segmented_handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@ define(function (require) {
var _ = require('lodash');
var Events = Private(require('factories/events'));


/**
* Simple class for creating an object to send to the
* requester of a SegmentedRequest. Since the SegmentedRequest
* extends AbstractRequest, it wasn't able to be the event
* emitter it was born to be. This provides a channel for
* setting values on the segmented request, and an event
* emitter for the request to speak outwardly
*
* @param {SegmentedRequest} - req - the requst this handle relates to
*/
_(SegmentedHandle).inherits(Events);
function SegmentedHandle(req) {
SegmentedHandle.Super.call(this);
this._req = req;
}

SegmentedHandle.prototype.setTotalSize = function (totalSize) {
var req = this._req;
totalSize = _.parseInt(totalSize);
return (req._totalSize = totalSize || false);
};

/**
* Set the sort direction for the request.
*
* @param {string} dir - one of 'asc' or 'desc'
*/
SegmentedHandle.prototype.setDirection = function (dir) {
switch (dir) {
case 'asc':
Expand Down
20 changes: 17 additions & 3 deletions src/kibana/components/courier/fetch/request/segmented.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ define(function (require) {
// segmented request specific state
this._initFn = initFn;
this._totalSize = false;
this._remainingSize = false;
this._direction = 'desc';
this._handle = new SegmentedHandle(this);

// give the request consumer a chance to receive each segment and set
// parameters via the handle
if (_.isFunction(initFn)) initFn(this._handle);

this._remainingSize = this._totalSize !== false ? this._totalSize : false;
this._createQueue();
this._all = this._queue.slice(0);
this._complete = [];
Expand All @@ -42,7 +42,7 @@ define(function (require) {
};

// prevent the source from changing between requests
this._flattenSource = _.once(function () { return this.source._flatten(); });
this._getFlattenedSource = _.once(this._getFlattenedSource);

// send out the initial status
this._reportStatus();
Expand All @@ -65,7 +65,7 @@ define(function (require) {
SegmentedReq.prototype.getFetchParams = function () {
var self = this;

return self._flattenSource()
return self._getFlattenedSource()
.then(function (flatSource) {
var params = _.cloneDeep(flatSource);
var index = self._active = self._queue.shift();
Expand Down Expand Up @@ -121,6 +121,20 @@ define(function (require) {
});
};

SegmentedReq.prototype._getFlattenedSource = function () {
var self = this;

return self.source._flatten()
.then(function (flatSource) {
var size = _.parseInt(_.deepGet(flatSource, 'body.size'));
if (_.isNumber(size)) {
self._totalSize = self._remainingSize = size;
}

return flatSource;
});
};

SegmentedReq.prototype._consumeSegment = function (seg) {
this._segments.push(seg);

Expand Down

0 comments on commit 1fdf755

Please sign in to comment.