Skip to content

Commit

Permalink
Merge pull request #386 from oat-sa/fix/send-requests-one-by-one
Browse files Browse the repository at this point in the history
Fix/send requests one by one
  • Loading branch information
krampstudio committed Mar 20, 2016
2 parents 617048f + 8911714 commit da1e26c
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 112 deletions.
2 changes: 1 addition & 1 deletion manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
'label' => 'QTI test model',
'description' => 'TAO QTI test implementation',
'license' => 'GPL-2.0',
'version' => '2.20.1',
'version' => '2.20.2',
'author' => 'Open Assessment Technologies',
'requires' => array(
'taoTests' => '>=2.6',
Expand Down
2 changes: 1 addition & 1 deletion scripts/update/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,6 @@ public function update($initialVersion) {
$this->setVersion('2.20.0');
}

$this->skip('2.20.0','2.20.1');
$this->skip('2.20.0','2.20.2');
}
}
124 changes: 62 additions & 62 deletions views/js/qtiTestRunner.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion views/js/qtiTestRunner.min.js.map

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion views/js/runner/provider/qti.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ define([
});


this.trigger('disablenav');
this.trigger('disablenav disabletools');

store()
.then(updateStats)
Expand All @@ -249,6 +249,8 @@ define([
})
.on('skip', function(scope){

this.trigger('disablenav disabletools');

computeNext('skip', {
scope : scope || 'item'
});
Expand Down Expand Up @@ -322,6 +324,9 @@ define([
})
.on('enableitem', function(){
this.trigger('enabletools');
})
.on('error', function(){
this.trigger('disabletools enablenav');
});

//starts the event collection
Expand Down
129 changes: 83 additions & 46 deletions views/js/runner/proxy/qtiServiceProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,57 +39,84 @@ define([
* @returns {Promise}
*/
function request(proxy, url, params, contentType, noToken) {
var headers = {};
var tokenHandler = proxy.getTokenHandler();
var token;

if (!noToken) {
token = tokenHandler.getToken();
if (token) {
headers['X-Auth-Token'] = token;
}
}

return new Promise(function(resolve, reject) {
$.ajax({
url: url,
type: params ? 'POST' : 'GET',
cache: false,
data: params,
headers: headers,
async: true,
dataType: 'json',
contentType : contentType
})
.done(function(data) {
if (data && data.token) {
tokenHandler.setToken(data.token);
}
if (data && data.success) {
resolve(data);
} else {
reject(data);
}
})
.fail(function(jqXHR, textStatus, errorThrown) {
var data;
try {
data = JSON.parse(jqXHR.responseText);
} catch (e) {
data = {
success: false,
code: jqXHR.status,
type: textStatus || 'error',
message: errorThrown || __('An error occurred!')
};
//run the request Promise
var requestPromise = function requestPromise(){
return new Promise(function(resolve, reject) {
var headers = {};
var tokenHandler = proxy.getTokenHandler();
var token;
if (!noToken) {
token = tokenHandler.getToken();
if (token) {
headers['X-Auth-Token'] = token;
}
}
$.ajax({
url: url,
type: params ? 'POST' : 'GET',
cache: false,
data: params,
headers: headers,
async: true,
dataType: 'json',
contentType : contentType
})
.done(function(data) {
if (data && data.token) {
tokenHandler.setToken(data.token);
}

if (data.token) {
tokenHandler.setToken(data.token);
}
if (data && data.success) {
resolve(data);
} else {
reject(data);
}
})
.fail(function(jqXHR, textStatus, errorThrown) {
var data;
try {
data = JSON.parse(jqXHR.responseText);
} catch (e) {
data = {
success: false,
code: jqXHR.status,
type: textStatus || 'error',
message: errorThrown || __('An error occurred!')
};
}

reject(data);
if (data.token) {
tokenHandler.setToken(data.token);
}

reject(data);
});
});
};

//no token protection, run the request
if(noToken === true){
return requestPromise();
}

//first promise, keep the ref
if(!proxy._runningPromise){
proxy._runningPromise = requestPromise();
return proxy._runningPromise;
}

//create a wrapping promise
return new Promise(function(resolve, reject){
//run the current request
var runRequest = function(){
var p = requestPromise();
proxy._runningPromise = p; //and keep the ref
p.then(resolve).catch(reject);
};

//wait the previous to resolve or fail and run the current one
proxy._runningPromise.then(runRequest).catch(runRequest);
});
}

Expand All @@ -99,6 +126,15 @@ define([
* @type {Object}
*/
var qtiServiceProxy = {

/**
* Keep a reference of the last running promise to
* ensure the tokened protected called are chained
* @type {Promise}
*/
_runningPromise : null,


/**
* Initializes the proxy
* @param {Object} config - The config provided to the proxy factory
Expand Down Expand Up @@ -129,6 +165,7 @@ define([
return new Promise(function(resolve) {
// no request, just a resources cleaning
self.storage = null;
self._runningPromise = null;
resolve();
});
},
Expand Down

0 comments on commit da1e26c

Please sign in to comment.