source: Dev/trunk/src/client/qed-client/xhr.js @ 498

Last change on this file since 498 was 487, checked in by hendrikvanantwerpen, 11 years ago

Completed migration to API, without CouchDB proxy.

Move to API is now completed. The full API is password protected, a very
limited API is exposed for respondents, which works with secrets that
are passed in URLs.

Serverside the HTTPResult class was introduced, which is similar to
Promises, but specifically for HTTP. It carries a status code and
response and makes it easier to extract parts of async handling in
separate functions.

Fixed a bug in our schema (it seems optional attributes don't exist but
a required list does). Verification of our schema by grunt-tv4 didn't
work yet. Our schema is organized the wrong way (this is fixable),
but the json-schema schema has problems with simple types and $refs.

File size: 1.6 KB
RevLine 
[463]1define([
[468]2    "./session",
[463]3    "dojo/Deferred",
[487]4    "dojo/_base/lang",
[468]5    "dojo/on",
[487]6    "dojo/_base/xhr"
7], function(session, Deferred, lang, on, xhr) {
[463]8
[468]9    var user = session.get();
[463]10    var queue = [];
11   
[487]12    on(session, 'change', function(newUser){
[468]13        user = newUser;
[487]14        retry();
[468]15    });
[487]16
[468]17    function retry() {
[487]18        if (user && queue.length > 0) {
[468]19            var item = queue.shift();
[487]20            console.log("Retry",item.options.url);
[468]21            real_request(item);
22        }
23    }
24   
25    function real_request(item) {
[487]26        var req = xhr(item.method,lang.mixin(item.options||{},{
27            failOk: true
28        }));
29        item.promise.ioArgs = req.ioArgs;
[468]30
[487]31        req.then(function(result){
32            item.dfd.resolve(result);
[468]33            retry();
[487]34        }, function(error){
[468]35            if ( error.response.status === 401 ) {
36                queue.unshift(item);
37                session.restore();
38            } else {
[487]39                item.dfd.reject(error);
[468]40                retry();
41            }
42        });
43    }
44   
[487]45    var _request = function(method, options) {
[468]46        var item = {
[487]47            method: method,
[468]48            options: options,
49            dfd: new Deferred()
50        };
[487]51        item.promise = lang.delegate(item.dfd.promise);
[468]52        // only do the request directly if we are authenticated and
53        // there are no earlier requests queued.
54        if ( user && queue.length === 0 ) {
[487]55            console.log("Request",options.url);
[468]56            real_request(item);
[463]57        } else {
[487]58            console.log("Push",options.url);
[468]59            queue.push(item);
[463]60        }
[487]61        return item.promise;
[463]62    };
[468]63
[463]64    return _request;
65});
Note: See TracBrowser for help on using the repository browser.