1(function (window) { 2 var EVENT_SUCCESS = 'success'; 3 var EVENT_ERROR = 'error'; 4 5 var PromiseAccumulatorBus = function () { 6 var queue = {}; 7 8 this.addEventListener = function (event, callback) { 9 if (!queue[event]) { 10 queue[event] = []; 11 } 12 queue[event].push(callback); 13 }; 14 15 this.trigger = function (event, data) { 16 if (!queue[event]) { 17 return; 18 } 19 20 queue[event].forEach(function (callback) { 21 callback(data); 22 }); 23 }; 24 25 this.hasListeners = function () { 26 return queue[EVENT_SUCCESS] && queue[EVENT_SUCCESS].length > 0; 27 }; 28 }; 29 30 var PromiseAccumulatorBusManager = function () { 31 var bus = new PromiseAccumulatorBus(); 32 33 this.reCreateBus = function () { 34 var current = bus; 35 36 bus = new PromiseAccumulatorBus(); 37 38 return current; 39 }; 40 41 this.addEventListener = function (event, callback) { 42 bus.addEventListener(event, callback); 43 }; 44 45 this.hasListeners = function () { 46 return bus.hasListeners(); 47 }; 48 }; 49 50 var PromiseAccumulator = function (callback) { 51 var busManager = new PromiseAccumulatorBusManager(); 52 var running = false; 53 54 var run = function () { 55 if (running) { 56 return Promise.resolve(); 57 } 58 59 running = true; 60 var currentBus = busManager.reCreateBus(); 61 62 return callback().then(function (result) { 63 currentBus.trigger(EVENT_SUCCESS, result); 64 }).catch(function (e) { 65 currentBus.trigger(EVENT_ERROR, e); 66 }).finally(function () { 67 running = false; 68 69 if (busManager.hasListeners()) { 70 run(); 71 } 72 }); 73 }; 74 75 this.getPromise = function () { 76 var promise = new Promise(function (resolve, reject) { 77 busManager.addEventListener(EVENT_SUCCESS, resolve); 78 busManager.addEventListener(EVENT_ERROR, reject); 79 }); 80 81 run(); 82 83 return promise; 84 }; 85 }; 86 87 window.PromiseAccumulator = PromiseAccumulator; 88})(window);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.