bug1769155.sjs (1067B)
1 function waitForTrue(state) { 2 return new Promise(resolve => { 3 let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); 4 timer.init( 5 () => { 6 if (getState(state) == "true") { 7 timer.cancel(); 8 resolve(); 9 } 10 }, 11 400, 12 Ci.nsITimer.TYPE_REPEATING_SLACK 13 ); 14 }); 15 } 16 function handleRequest(request, response) { 17 response.processAsync(); 18 19 if (request.queryString != "stop") { 20 // This is called from a synchronous XHR that we want to block until 21 // we get a stop notification. 22 waitForTrue("stop").then(() => { 23 response.write(""); 24 response.finish(); 25 26 // Signal the other connection that we've closed the connection 27 // for the synchronous XHR. 28 setState("stopped", "true"); 29 }); 30 } else { 31 // Close the connection for the synchronous XHR. 32 setState("stop", "true"); 33 34 // Let's wait until we've actually closed the connection for the XHR. 35 waitForTrue("stopped").then(() => { 36 response.write(""); 37 response.finish(); 38 }); 39 } 40 }