comparison js/core.js @ 3118:f78a66fbc018

Better asynchronous catcher. If a long queue of file uploads builds up, will resolve to the last in the queue
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Wed, 18 Jul 2018 14:47:14 +0100
parents e9d16f8ea673
children aa4503f8c630
comparison
equal deleted inserted replaced
3117:76cffc7ecdee 3118:f78a66fbc018
3834 this.SessionKey = (function (parent) { 3834 this.SessionKey = (function (parent) {
3835 var returnURL = ""; 3835 var returnURL = "";
3836 if (window.returnURL !== undefined) { 3836 if (window.returnURL !== undefined) {
3837 returnURL = String(window.returnURL); 3837 returnURL = String(window.returnURL);
3838 } 3838 }
3839
3840 var chainCount = 0;
3841 var chainPosition = chainCount;
3839 3842
3840 function postUpdate() { 3843 function postUpdate() {
3841 // Return a new promise.
3842 var hold = document.createElement("div");
3843 var clone = parent.root.cloneNode(true);
3844 hold.appendChild(clone);
3845 return new Promise(function (resolve, reject) { 3844 return new Promise(function (resolve, reject) {
3845 // Return a new promise.
3846 chainPosition+=1;
3847 var hold = document.createElement("div");
3848 var clone = parent.root.cloneNode(true);
3849 hold.appendChild(clone);
3846 // Do the usual XHR stuff 3850 // Do the usual XHR stuff
3847 console.log("Requested save..."); 3851 console.log("Requested save...");
3848 var req = new XMLHttpRequest(); 3852 var req = new XMLHttpRequest();
3849 req.open("POST", returnURL + "php/save.php?key=" + sessionKey + "&saveFilenamePrefix=" + parent.filenamePrefix); 3853 req.open("POST", returnURL + "php/save.php?key=" + sessionKey + "&saveFilenamePrefix=" + parent.filenamePrefix);
3850 req.setRequestHeader('Content-Type', 'text/xml'); 3854 req.setRequestHeader('Content-Type', 'text/xml');
3874 req.onerror = function () { 3878 req.onerror = function () {
3875 reject(Error("Network Error")); 3879 reject(Error("Network Error"));
3876 }; 3880 };
3877 3881
3878 // Make the request 3882 // Make the request
3879 req.send([hold.innerHTML]); 3883 if (chainCount > chainPosition) {
3884 // We have items downstream that will upload for us
3885 resolve(true);
3886 } else {
3887 req.send([hold.innerHTML]);
3888 }
3880 }); 3889 });
3881 } 3890 }
3882 3891
3883 function keyPromise() { 3892 function keyPromise() {
3884 return new Promise(function (resolve, reject) { 3893 return new Promise(function (resolve, reject) {
3958 "update": { 3967 "update": {
3959 "value": function () { 3968 "value": function () {
3960 if (requestChains === undefined) { 3969 if (requestChains === undefined) {
3961 throw ("Initiate key exchange first"); 3970 throw ("Initiate key exchange first");
3962 } 3971 }
3972 chainCount += 1;
3963 this.parent.root.setAttribute("state", "update"); 3973 this.parent.root.setAttribute("state", "update");
3964 requestChains = requestChains.then(postUpdate); 3974 requestChains = requestChains.then(postUpdate);
3965 } 3975 }
3966 }, 3976 },
3967 "finish": { 3977 "finish": {