Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Добавить аккумулирование get запросов #47

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions lib/apic.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ define(function (require) {
variables = {},
augments = {},
language,
lastResponse;
lastResponse,
pending = {};

var scheme = 'http:';

Expand Down Expand Up @@ -110,7 +111,7 @@ define(function (require) {
root.getLastResponse = function () {
return lastResponse;
};

root.getBaseHost = function () {
return base;
};
Expand Down Expand Up @@ -138,6 +139,7 @@ define(function (require) {
representation = last(args) === 'object' ? args.pop() : undefined,
query = last(args) === 'object' ? args.pop() : {},
path = uri,
request,
_;

if (safeMethods[verb]) {
Expand Down Expand Up @@ -214,6 +216,18 @@ define(function (require) {

}

if (safe) {
request = verb + path + urlencode.encode(query) + JSON.stringify(headers);

if (pending[request]) {
callback && pending[request].push(callback);
return;
}

pending[request] = [];
callback && pending[request].push(callback);
}

transport.request({
uri: protocol + base + path + urlencode.encode(query),
method: verb,
Expand All @@ -224,20 +238,34 @@ define(function (require) {
var token;

function cb(err) {
var self = this;

function apply(callback) {
/**
* Pass xhr object only if explicitly expected
* otherwise some libraries using 'arguments' object
* can work incorrectly
*/
callback.length === 3 && args.push(xhr);
callback.apply(self, args);
}

if (!callback) {
return;
}

var args = [err, body];

/**
* Pass xhr object only if explicitly expected
* otherwise some libraries using 'arguments' object
* can work incorrectly
*/
callback.length === 3 && args.push(xhr);
callback.apply(this, args);
if (pending[request]) {
pending[request].forEach(function(callback) {
apply(callback);
});

delete pending[request];
} else {
apply(callback);
}

}

lastResponse = xhr;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apic.js",
"version": "0.11.0",
"version": "0.12.0",
"description": "REST API JavaScript Client Generator",
"keywords": [
"js",
Expand Down