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