Skip to content

Commit ed5a1aa

Browse files
author
James Haley
committed
Reset function for further requests
1 parent 4916b41 commit ed5a1aa

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,26 @@ Batchelor.add({
135135
```
136136
This could be required, for example, when making multiple requests with different Auth data and then needing to make further requests with the same Auth data.
137137

138+
###### Resetting and Re-using
139+
Once Batchelor has been run, there are certain use-cases for running futher batch requests on response. This requires the variables in the module to be reset. This can be done using the `.reset()` call:
140+
``` node
141+
Batchelor.run(function(response){
142+
143+
// Reset Batchelor for further use
144+
Batchelor.reset();
145+
...
146+
147+
});
148+
```
149+
138150
## To Do List-ish
139151
These might get done if we end up needing them/have time:
140152
* Limit requests per batch request
141-
* Handle Media in API calls (no need for it here, feel free)
153+
* Handle Media in API calls (no need for it here, feel free to write it)
142154

143155
## Release History
144156

157+
* 0.0.7 Reset function for when you are using batchelor more than once in a script (ability for nested requests too)
145158
* 0.0.6 Bug fixes introduced in the last update and clean up was happening too soon. Moved it.
146159
* 0.0.5 Added the ability to passthrough specific information from the `.add()` options to the response object
147160
* 0.0.4 Authorization can now be set on each request (currently Bearer [...] only)

lib/batchelor.js

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ var request = require('request'),
3131

3232
(function (Batchelor) {
3333

34-
var _requests = [],
35-
_requestSpecificCallbacks = [],
36-
_requestExtensionData = [];
37-
3834
/**
3935
* Initialise Batchelor
4036
*
@@ -55,6 +51,11 @@ var request = require('request'),
5551
// Globalise the options
5652
this._globalOptions = options;
5753

54+
// Ready internal variables
55+
this._requests = [];
56+
this._requestSpecificCallbacks = [];
57+
this._requestExtensionData = [];
58+
5859
// Chaining baby
5960
return this;
6061

@@ -88,12 +89,12 @@ var request = require('request'),
8889

8990
// Save out request specific callback
9091
if (!!options.callback) {
91-
_requestSpecificCallbacks[requestId] = options.callback;
92+
this._requestSpecificCallbacks[requestId] = options.callback;
9293
}
9394

9495
// Any data thay needs to be passed through to the callback
9596
if (!!options.extend) {
96-
_requestExtensionData[requestId] = options.extend;
97+
this._requestExtensionData[requestId] = options.extend;
9798
}
9899

99100
// Set Defaults
@@ -115,7 +116,7 @@ var request = require('request'),
115116
}
116117

117118
// Push to _requests Array
118-
_requests.push(options);
119+
this._requests.push(options);
119120

120121
// Chaining baby
121122
return this;
@@ -130,10 +131,11 @@ var request = require('request'),
130131
*/
131132
Batchelor.run = function (callback) {
132133

133-
var _multiparts = [];
134+
var _multiparts = [],
135+
_self = this;
134136

135137
// Build multipart request
136-
_requests.forEach(function (requestPart) {
138+
this._requests.forEach(function (requestPart) {
137139

138140
var requestSettings = {
139141
'Content-Type': 'application/http',
@@ -213,8 +215,8 @@ var request = require('request'),
213215

214216
// Get the response id if exists
215217
var returnedContentId = part.header['content-id'],
216-
currentRequestCallback = null,
217-
currentRequestExtendData = null;
218+
currentRequestCallback,
219+
currentRequestExtendData;
218220

219221
if (!!returnedContentId) {
220222

@@ -226,10 +228,10 @@ var request = require('request'),
226228
part.data.headers['Content-ID'] = returnedContentId;
227229

228230
// As we have a content id, we can link to individual callback (if exists)
229-
currentRequestCallback = _requestSpecificCallbacks[returnedContentId];
231+
currentRequestCallback = _self._requestSpecificCallbacks[returnedContentId];
230232

231233
// If this request has extend data, retrieve it for callback
232-
currentRequestExtendData = _requestExtensionData[returnedContentId];
234+
currentRequestExtendData = _self._requestExtensionData[returnedContentId];
233235
}
234236

235237
// If this individual request has a callback, run it
@@ -247,13 +249,7 @@ var request = require('request'),
247249

248250
// All remaining responses get output
249251
// Extend data sent through as is for parse in callback
250-
callback(responseBody, _requestExtensionData);
251-
252-
// Clean up
253-
_requests = [];
254-
_requestExtensionData = [];
255-
_requestSpecificCallbacks = [];
256-
_multiparts = [];
252+
callback(responseBody, _self._requestExtensionData);
257253

258254
});
259255

@@ -262,4 +258,20 @@ var request = require('request'),
262258
});
263259
};
264260

261+
/**
262+
* Reset all internal options if a re-run is required
263+
*
264+
* @return {object} Batchelor object
265+
*/
266+
Batchelor.reset = function () {
267+
268+
this._requests = [];
269+
this._requestSpecificCallbacks = [];
270+
this._requestExtensionData = [];
271+
this._globalOptions.multipart = [];
272+
273+
return this;
274+
275+
};
276+
265277
}(exports));

0 commit comments

Comments
 (0)