stash.js (2266B)
1 var Stash = function(inbound, outbound) { 2 this.stashPath = '/presentation-api/controlling-ua/support/stash.py?id='; 3 this.inbound = inbound; 4 this.outbound = outbound; 5 } 6 7 // initialize a stash on wptserve 8 Stash.prototype.init = function() { 9 return Promise.all([ 10 fetch(this.stashPath + this.inbound).then(response => { 11 return response.text(); 12 }), 13 fetch(this.stashPath + this.outbound).then(response => { 14 return response.text(); 15 }) 16 ]); 17 } 18 19 // upload a test result to a stash on wptserve 20 Stash.prototype.send = function(result) { 21 return fetch(this.stashPath + this.outbound, { 22 method: 'POST', 23 body: JSON.stringify({ type: 'data', data: result }) 24 }).then(response => { 25 return response.text(); 26 }).then(text => { 27 return text === 'ok' ? null : Promise.reject(); 28 }) 29 }; 30 31 // wait until a test result is uploaded to a stash on wptserve 32 Stash.prototype.receive = function() { 33 return new Promise((resolve, reject) => { 34 let intervalId; 35 const interval = 500; // msec 36 const polling = () => { 37 return fetch(this.stashPath + this.inbound).then(response => { 38 return response.text(); 39 }).then(text => { 40 if (text) { 41 try { 42 const json = JSON.parse(text); 43 if (json.type === 'data') 44 resolve(json.data); 45 else 46 reject(); 47 } catch(e) { 48 resolve(text); 49 } 50 clearInterval(intervalId); 51 } 52 }); 53 }; 54 intervalId = setInterval(polling, interval); 55 }); 56 }; 57 58 // reset a stash on wptserve 59 Stash.prototype.stop = function() { 60 return Promise.all([ 61 fetch(this.stashPath + this.inbound).then(response => { 62 return response.text(); 63 }), 64 fetch(this.stashPath + this.outbound).then(response => { 65 return response.text(); 66 }) 67 ]).then(() => { 68 return Promise.all([ 69 fetch(this.stashPath + this.inbound, { 70 method: 'POST', 71 body: JSON.stringify({ type: 'stop' }) 72 }).then(response => { 73 return response.text(); 74 }), 75 fetch(this.stashPath + this.outbound, { 76 method: 'POST', 77 body: JSON.stringify({ type: 'stop' }) 78 }).then(response => { 79 return response.text(); 80 }) 81 ]); 82 }); 83 }