Skip to content

Commit

Permalink
Fixed merge issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsamwell committed Mar 18, 2015
2 parents fc735f5 + a0e1338 commit 6054347
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 16 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ This is undoubtaly the most important option. As this module tries to be as tra

The default time in milliseconds the http batcher should wait to collection all request to this domain after the first http call that can be batched has been collect. This defaults to 100ms. Therefore if you send a HTTP GET call that can be batched the HTTP batcher will receive this call and wait a further 100ms before sending the call in order to wait for other calls to the same domain in order to add them to the current batch request. If no other calls are collected the initial HTTP call will be allowed to continue as normal and will not be batched unless the config property - **minimumBatchSize** is set to one.

<h4 id="flushing-all-requests">Immediately flushing all pending requests</h4>
In some instances you might want to immediately send all pending request regardless of if the request quota or timeout limit has been reached. To do this you can simply call the flush method on the httpBatcher service and optionally pass in the url of the batch endpoint you want to flush (if no parameter is passed in all pending requests to all endpoints are flushed).

```language-javascript
angular.module('myApp', ['jcs.angular-http-batch']);
.run([
'httpBatcher',
function (httpBatcher) {
httpBatcher.flush();
}
]);
```

<h3 id="angular-http-batcher-getting-started-with-asp-web-api">Configuring .Net Web API 2 for Batch Requests</h3>

This is **really** simple the web api team have done a really good job here. To enable batch request handling you just add a new route to your application and the rest is done for you! It's so easy I don't see any reason for you not to do it! See [this link](http://blogs.msdn.com/b/webdev/archive/2013/11/01/introducing-batch-support-in-web-api-and-web-api-odata.aspx) for a more detailed setup guide. Just add the below code to your web api configuration class and you are good to go!
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-http-batcher",
"version": "1.6.0",
"version": "1.7.0",
"description": "Enables transparent HTTP batch requests with Angular",
"main": "dist/angular-http-batch.min.js",
"keywords": [
Expand Down
5 changes: 4 additions & 1 deletion dist/ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
16/03/2015 V1.6.0
16/03/2015 V1.7.0
Fixed issue with parsing the response string into a json object with having more than two dashes '--' in the returned data.

12/01/2015 V1.6.0
Added a flush method to the httpBatcher service which will immediately send all the pending batch requests regardless of batch quota or time limits being hit.

19/11/2014 V1.5.0
When batching requests that have request bodies, such as "POST" requests, the standard angular request transforms convert objects into json strings.
When angular-http-batcher calls angular.toJson again, we end up with a single json string, instead of a json encoding of the object in the batched request.
Expand Down
27 changes: 21 additions & 6 deletions dist/angular-http-batch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* angular-http-batcher - v1.6.0 - 2015-03-16
* angular-http-batcher - v1.7.0 - 2015-03-18
* https://github.com/jonsamwell/angular-http-batcher
* Copyright (c) 2015 Jon Samwell
*/
Expand Down Expand Up @@ -214,6 +214,15 @@ angular.module(window.ahb.name).factory('httpBatcher', [
}

batchRequestManager.addRequest(request);
},

flush = function (batchEndpointUrl) {
angular.forEach(currentBatchedRequests, function (val, key) {
var shouldFlush = batchEndpointUrl === undefined || batchEndpointUrl && key.toLocaleLowerCase() === batchEndpointUrl.toLocaleLowerCase();
if (shouldFlush) {
val.flush();
}
});
};

BatchRequestPartParser.prototype = (function () {
Expand Down Expand Up @@ -379,14 +388,18 @@ angular.module(window.ahb.name).factory('httpBatcher', [
this.requests.push(request);

if (this.requests.length > this.config.maxBatchedRequestPerCall) {
$timeout.cancel(this.currentTimeoutToken);
this.currentTimeoutToken = undefined;
this.send();
this.flush();
}

return true;
},

flush = function () {
$timeout.cancel(this.currentTimeoutToken);
this.currentTimeoutToken = undefined;
this.send();
},

getUrlInfo = function (url) {
var protocol,
host,
Expand Down Expand Up @@ -432,13 +445,15 @@ angular.module(window.ahb.name).factory('httpBatcher', [

return {
addRequest: addRequest,
send: send
send: send,
flush: flush
};
}());

return {
canBatchRequest: canBatchRequest,
batchRequest: batchRequest
batchRequest: batchRequest,
flush: flush
};
}
]);
Expand Down
4 changes: 2 additions & 2 deletions dist/angular-http-batch.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-http-batcher",
"version": "1.6.0",
"version": "1.7.0",
"description": "Enables transparent HTTP batch requests with Angular",
"main": "angular-http-batcher.min.js",
"scripts": {
Expand Down
25 changes: 20 additions & 5 deletions src/services/httpBatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ angular.module(window.ahb.name).factory('httpBatcher', [
}

batchRequestManager.addRequest(request);
},

flush = function (batchEndpointUrl) {
angular.forEach(currentBatchedRequests, function (val, key) {
var shouldFlush = batchEndpointUrl === undefined || batchEndpointUrl && key.toLocaleLowerCase() === batchEndpointUrl.toLocaleLowerCase();
if (shouldFlush) {
val.flush();
}
});
};

BatchRequestPartParser.prototype = (function () {
Expand Down Expand Up @@ -227,14 +236,18 @@ angular.module(window.ahb.name).factory('httpBatcher', [
this.requests.push(request);

if (this.requests.length > this.config.maxBatchedRequestPerCall) {
$timeout.cancel(this.currentTimeoutToken);
this.currentTimeoutToken = undefined;
this.send();
this.flush();
}

return true;
},

flush = function () {
$timeout.cancel(this.currentTimeoutToken);
this.currentTimeoutToken = undefined;
this.send();
},

getUrlInfo = function (url) {
var protocol,
host,
Expand Down Expand Up @@ -280,13 +293,15 @@ angular.module(window.ahb.name).factory('httpBatcher', [

return {
addRequest: addRequest,
send: send
send: send,
flush: flush
};
}());

return {
canBatchRequest: canBatchRequest,
batchRequest: batchRequest
batchRequest: batchRequest,
flush: flush
};
}
]);
34 changes: 34 additions & 0 deletions tests/services/httpBatcher.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,40 @@
});
});
});

describe('flush', function () {
it('should send the batched request before the timeout to send the batch has been reached', function (done) {
var batchConfig = {
batchEndpointUrl: 'http://www.someservice.com/batch',
batchRequestCollectionDelay: 10000,
minimumBatchSize: 1
},
postData = '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=request\r\n\r\nGET /resource HTTP/1.1\r\nHost: www.gogle.com\r\n\r\n\r\n--some_boundary_mocked--',
responseData = '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=response\r\n\r\n' +
'HTTP/1.1 200 OK\r\nContent-Type: application/json; charset=utf-8\r\n\r\n' +
'[{"Name":"Product 1","Id":1,"StockQuantity":100},{"Name":"Product 2","Id":2,"StockQuantity":2},{"Name":"Product 3","Id":3,"StockQuantity":32432}]' +
'\r\n--some_boundary_mocked--\r\n';

$httpBackend.expectPOST(batchConfig.batchEndpointUrl, postData).respond(200, responseData, {
'content-type': 'multipart/mixed; boundary="some_boundary_mocked"'
}, 'OK');

sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('some_boundary_mocked');
sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);

httpBatcher.batchRequest({
url: 'http://www.gogle.com/resource',
method: 'GET',
callback: function () {
done();
}
});

httpBatcher.flush();

$httpBackend.flush();
});
});
});
});
}(angular, sinon));

0 comments on commit 6054347

Please sign in to comment.