1;(function(window) 2{ 3 /****************** ATTENTION ******************************* 4 * Please do not use Bitrix CoreJS in this class. 5 * This class can be called on page without Bitrix Framework 6 *************************************************************/ 7 8 if (!window.BX) 9 { 10 window.BX = {}; 11 } 12 13 var BX = window.BX; 14 15 BX.Promise = function(fn, ctx) // fn is future-reserved 16 { 17 this.state = null; 18 this.value = null; 19 this.reason = null; 20 this.next = null; 21 this.ctx = ctx || this; 22 23 this.onFulfilled = []; 24 this.onRejected = []; 25 }; 26 BX.Promise.prototype.fulfill = function(value) 27 { 28 this.checkState(); 29 30 this.value = value; 31 this.state = true; 32 this.execute(); 33 }; 34 BX.Promise.prototype.reject = function(reason) 35 { 36 this.checkState(); 37 38 this.reason = reason; 39 this.state = false; 40 this.execute(); 41 }; 42 BX.Promise.prototype.then = function(onFulfilled, onRejected) 43 { 44 if(typeof (onFulfilled) == "function" || onFulfilled instanceof Function) 45 { 46 this.onFulfilled.push(onFulfilled); 47 } 48 if(typeof (onRejected) == "function" || onRejected instanceof Function) 49 { 50 this.onRejected.push(onRejected); 51 } 52 53 if(this.next === null) 54 { 55 this.next = new BX.Promise(null, this.ctx); 56 } 57 58 if(this.state !== null) // if promise was already resolved, execute immediately 59 { 60 this.execute(); 61 } 62 63 return this.next; 64 }; 65 66 BX.Promise.prototype.catch = function(onRejected) 67 { 68 if(typeof (onRejected) == "function" || onRejected instanceof Function) 69 { 70 this.onRejected.push(onRejected); 71 } 72 73 if(this.next === null) 74 { 75 this.next = new BX.Promise(null, this.ctx); 76 } 77 78 if(this.state !== null) // if promise was already resolved, execute immediately 79 { 80 this.execute(); 81 } 82 83 return this.next; 84 }; 85 86 BX.Promise.prototype.setAutoResolve = function(way, ms) 87 { 88 this.timer = setTimeout(function(){ 89 if(this.state === null) 90 { 91 this[way ? 'fulfill' : 'reject'](); 92 } 93 }.bind(this), ms || 15); 94 }; 95 BX.Promise.prototype.cancelAutoResolve = function() 96 { 97 clearTimeout(this.timer); 98 }; 99 /** 100 * Resolve function. This function allows promise chaining, like ..then().then()... 101 * Typical usage: 102 *
103 * var p = new Promise(); 104 * 105 * p.then(function(value){ 106 * return someValue; // next promise in the chain will be fulfilled with someValue 107 * }).then(function(value){ 108 * 109 * var p1 = new Promise(); 110 * *** some async code here, that eventually resolves p1 *** 111 * 112 * return p1; // chain will resume when p1 resolved (fulfilled or rejected) 113 * }).then(function(value){ 114 * 115 * // you can also do 116 * var e = new Error(); 117 * throw e; 118 * // it will cause next promise to be rejected with e 119 * 120 * return someOtherValue; 121 * }).then(function(value){ 122 * ... 123 * }, function(reason){ 124 * // promise was rejected with reason 125 * })...; 126 * 127 * p.fulfill('let`s start this chain'); 128 * 129 * @param x 130 */ 131 BX.Promise.prototype.resolve = function(x) 132 { 133 var this_ = this; 134 135 if(this === x) 136 { 137 this.reject(new TypeError('Promise cannot fulfill or reject itself')); // avoid recursion 138 } 139 // allow "pausing" promise chaining until promise x is fulfilled or rejected 140 else if(x && x.toString() === "[object BX.Promise]") 141 { 142 x.then(function(value){ 143 this_.fulfill(value); 144 }, function(reason){ 145 this_.reject(reason); 146 }); 147 } 148 else // auto-fulfill this promise 149 { 150 this.fulfill(x); 151 } 152 }; 153 154 BX.Promise.prototype.toString = function() 155 { 156 return "[object BX.Promise]"; 157 }; 158 159 BX.Promise.prototype.execute = function() 160 { 161 if(this.state === null) 162 { 163 //then() must not be called before BX.Promise resolve() happens 164 return; 165 } 166 167 var value = undefined; 168 var reason = undefined; 169 var x = undefined; 170 var k; 171 if(this.state === true) // promise was fulfill()-ed 172 { 173 if(this.onFulfilled.length) 174 { 175 try 176 { 177 for(k = 0; k < this.onFulfilled.length; k++) 178 {
179 x = this.onFulfilled[k].apply(this.ctx, [this.value]); 180 if(typeof x != 'undefined') 181 { 182 value = x; 183 } 184 } 185 } 186 catch(e) 187 { 188 if('console' in window) 189 { 190 console.dir(e); 191 } 192 193 if (typeof BX.debug !== 'undefined') 194 { 195 BX.debug(e); 196 } 197 198 reason = e; // reject next 199 } 200 } 201 else 202 { 203 value = this.value; // resolve next 204 } 205 } 206 else if(this.state === false) // promise was reject()-ed 207 { 208 if(this.onRejected.length) 209 { 210 try 211 { 212 for(k = 0; k < this.onRejected.length; k++) 213 { 214 x = this.onRejected[k].apply(this.ctx, [this.reason]); 215 if(typeof x != 'undefined') 216 { 217 value = x; 218 } 219 } 220 } 221 catch(e) 222 { 223 if('console' in window) 224 { 225 console.dir(e); 226 } 227 228 if (typeof BX.debug !== 'undefined') 229 { 230 BX.debug(e); 231 } 232 233 reason = e; // reject next 234 } 235 } 236 else 237 { 238 reason = this.reason; // reject next 239 } 240 } 241 242 if(this.next !== null) 243 { 244 if(typeof reason != 'undefined') 245 { 246 this.next.reject(reason); 247 } 248 else if(typeof value != 'undefined') 249 { 250 this.next.resolve(value); 251 } 252 } 253 }; 254 BX.Promise.prototype.checkState = function() 255 { 256 if(this.state !== null) 257 { 258 throw new Error('You can not do fulfill() or reject() multiple times'); 259 } 260 }; 261})(window); 262 263
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.