diff options
author | Valerio Virgillito | 2012-02-03 12:20:10 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-02-03 12:20:10 -0800 |
commit | e6e539f345f5e1ac36a595db5d1a1c3700715e88 (patch) | |
tree | 39846bf1570f9b5a4057b726c54905de9e9e1c02 /node_modules/montage/core/promise.js | |
parent | 7950424cf704bb221971f4645406b01e6979db18 (diff) | |
parent | c630ead20487c3948fa36448d3e7d5f7b5cba782 (diff) | |
download | ninja-e6e539f345f5e1ac36a595db5d1a1c3700715e88.tar.gz |
Merge branch 'master' into components
Conflicts:
node_modules/montage/ui/bluemoon/progress.reel/progress.html
node_modules/montage/ui/image2.reel/image2.html
node_modules/ninja-components/effect/desaturate-effect.js
node_modules/ninja-components/effect/effect.js
node_modules/ninja-components/effect/invert-effect.js
node_modules/ninja-components/effect/kaliedoscope-effect.js
node_modules/ninja-components/effect/multiply-effect.js
node_modules/ninja-components/effect/sepia-effect.js
node_modules/ninja-components/flow-controller.reel/flow-controller.html
node_modules/ninja-components/flow-controller.reel/flow-controller.js
node_modules/ninja-components/flow-offset.js
node_modules/ninja-components/hottext.reel/hottext.css
node_modules/ninja-components/hottext.reel/hottext.html
node_modules/ninja-components/hottext.reel/hottext.js
node_modules/ninja-components/hottextunit.reel/hottextunit.css
node_modules/ninja-components/hottextunit.reel/hottextunit.html
node_modules/ninja-components/hottextunit.reel/hottextunit.js
node_modules/ninja-components/image2.reel/image2.html
node_modules/ninja-components/image2.reel/image2.js
node_modules/ninja-components/image3d.reel/image3d.html
node_modules/ninja-components/image3d.reel/image3d.js
node_modules/ninja-components/photo-editor.reel/photo-editor.css
node_modules/ninja-components/photo-editor.reel/photo-editor.html
node_modules/ninja-components/photo-editor.reel/photo-editor.js
node_modules/ninja-components/slider-base.js
user-document-templates/montage-application-cloud/appdelegate.js
Signed-off-by: Valerio Virgillito <valerio@motorola.com>
Diffstat (limited to 'node_modules/montage/core/promise.js')
-rwxr-xr-x | node_modules/montage/core/promise.js | 115 |
1 files changed, 42 insertions, 73 deletions
diff --git a/node_modules/montage/core/promise.js b/node_modules/montage/core/promise.js index 80c63943..aabe113d 100755 --- a/node_modules/montage/core/promise.js +++ b/node_modules/montage/core/promise.js | |||
@@ -5,7 +5,7 @@ | |||
5 | </copyright> */ | 5 | </copyright> */ |
6 | 6 | ||
7 | // Scope: | 7 | // Scope: |
8 | // * ES5, W3C setImmediate (shimmed if necessary) | 8 | // * ES5 |
9 | // * speed and economy of memory before safety and securability | 9 | // * speed and economy of memory before safety and securability |
10 | // * run-time compatibility via thenability | 10 | // * run-time compatibility via thenability |
11 | 11 | ||
@@ -30,13 +30,15 @@ | |||
30 | 30 | ||
31 | "use strict"; | 31 | "use strict"; |
32 | 32 | ||
33 | var TIMER; | ||
33 | try { | 34 | try { |
34 | // bootstrapping can't handle relative identifiers | 35 | // bootstrapping can't handle relative identifiers |
35 | require("core/shim/timers"); // setImmediate | 36 | TIMER = require("core/next-tick"); |
36 | } catch (exception) { | 37 | } catch (exception) { |
37 | // in this case, node can't handle absolute identifiers | 38 | // in this case, node can't handle absolute identifiers |
38 | require("./shim/timers"); // setImmediate | 39 | TIMER = require("./next-tick"); |
39 | } | 40 | } |
41 | var nextTick = TIMER.nextTick; | ||
40 | 42 | ||
41 | // merely ensures that the returned value can respond to | 43 | // merely ensures that the returned value can respond to |
42 | // messages; does not guarantee a full promise API | 44 | // messages; does not guarantee a full promise API |
@@ -107,17 +109,10 @@ var PrimordialPromise = Creatable.create({ | |||
107 | value: function (descriptor, promiseDescriptor) { | 109 | value: function (descriptor, promiseDescriptor) { |
108 | 110 | ||
109 | // automatically subcreate each of the contained promise types | 111 | // automatically subcreate each of the contained promise types |
110 | var creation = Object.create(this, { | 112 | var creation = Object.create(this); |
111 | DeferredPromise: { | 113 | creation.DeferredPromise = this.DeferredPromise.create(promiseDescriptor); |
112 | value: this.DeferredPromise.create(promiseDescriptor) | 114 | creation.FulfilledPromise = this.FulfilledPromise.create(promiseDescriptor); |
113 | }, | 115 | creation.RejectedPromise = this.RejectedPromise.create(promiseDescriptor); |
114 | FulfilledPromise: { | ||
115 | value: this.FulfilledPromise.create(promiseDescriptor) | ||
116 | }, | ||
117 | RejectedPromise: { | ||
118 | value: this.RejectedPromise.create(promiseDescriptor) | ||
119 | } | ||
120 | }); | ||
121 | 116 | ||
122 | if (descriptor) { | 117 | if (descriptor) { |
123 | Object.defineProperties(creation, descriptor); | 118 | Object.defineProperties(creation, descriptor); |
@@ -125,17 +120,13 @@ var PrimordialPromise = Creatable.create({ | |||
125 | 120 | ||
126 | // create static reflections of all new promise methods | 121 | // create static reflections of all new promise methods |
127 | if (promiseDescriptor) { | 122 | if (promiseDescriptor) { |
128 | var statics = {}; | ||
129 | Object.keys(promiseDescriptor).forEach(function (name) { | 123 | Object.keys(promiseDescriptor).forEach(function (name) { |
130 | statics[name] = { | 124 | creation[name] = function (value) { |
131 | value: function (value) { | 125 | var args = Array.prototype.slice.call(arguments, 1); |
132 | var args = Array.prototype.slice.call(arguments, 1); | 126 | var promise = this.ref(value); |
133 | var promise = this.ref(value); | 127 | return promise[name].apply(promise, args); |
134 | return promise[name].apply(promise, args); | ||
135 | } | ||
136 | }; | 128 | }; |
137 | }); | 129 | }); |
138 | Object.defineProperties(creation, statics); | ||
139 | } | 130 | } |
140 | 131 | ||
141 | return creation; | 132 | return creation; |
@@ -182,15 +173,10 @@ var PrimordialPromise = Creatable.create({ | |||
182 | 173 | ||
183 | fulfill: { | 174 | fulfill: { |
184 | value: function (value) { | 175 | value: function (value) { |
185 | return this.FulfilledPromise.create({ | 176 | var self = Object.create(this.FulfilledPromise); |
186 | _value: { | 177 | self._value = value; |
187 | value: value, | 178 | self.Promise = this; |
188 | writable: true | 179 | return self; |
189 | }, | ||
190 | Promise: { | ||
191 | value: this | ||
192 | } | ||
193 | }); | ||
194 | } | 180 | } |
195 | }, | 181 | }, |
196 | 182 | ||
@@ -246,17 +232,10 @@ var PrimordialPromise = Creatable.create({ | |||
246 | 232 | ||
247 | reject: { | 233 | reject: { |
248 | value: function (reason, error) { | 234 | value: function (reason, error) { |
249 | var self = this.RejectedPromise.create({ | 235 | var self = Object.create(this.RejectedPromise); |
250 | _reason: { | 236 | self._reason = reason; |
251 | value: reason | 237 | self._error = error; |
252 | }, | 238 | self.Promise = this; |
253 | _error: { | ||
254 | value: error | ||
255 | }, | ||
256 | Promise: { | ||
257 | value: this | ||
258 | } | ||
259 | }); | ||
260 | errors.push(error && error.stack || self); | 239 | errors.push(error && error.stack || self); |
261 | return self; | 240 | return self; |
262 | } | 241 | } |
@@ -310,31 +289,13 @@ var PrimordialPromise = Creatable.create({ | |||
310 | defer: { | 289 | defer: { |
311 | value: function () { | 290 | value: function () { |
312 | var deferred; | 291 | var deferred; |
313 | var promise = this.DeferredPromise.create({ | 292 | var promise = Object.create(this.DeferredPromise); |
314 | _pending: { | 293 | promise._pending = []; |
315 | value: [], | 294 | promise.Promise = this; |
316 | writable: true | 295 | deferred = Object.create(this.Deferred); |
317 | }, | 296 | deferred.promise = promise; |
318 | _value: { | 297 | deferred.Promise = this; |
319 | value: undefined, | 298 | promise._deferred = deferred; |
320 | writable: true | ||
321 | }, | ||
322 | Promise: { | ||
323 | value: this | ||
324 | } | ||
325 | }); | ||
326 | deferred = this.Deferred.create({ | ||
327 | promise: { | ||
328 | value: promise, | ||
329 | enumerable: true | ||
330 | }, | ||
331 | Promise: { | ||
332 | value: this | ||
333 | } | ||
334 | }); | ||
335 | Object.defineProperty(promise, "_deferred", { | ||
336 | value: deferred | ||
337 | }); | ||
338 | return deferred; | 299 | return deferred; |
339 | } | 300 | } |
340 | }, | 301 | }, |
@@ -349,7 +310,7 @@ var PrimordialPromise = Creatable.create({ | |||
349 | } | 310 | } |
350 | this.promise._value = value = toPromise(value); | 311 | this.promise._value = value = toPromise(value); |
351 | this.promise._pending.forEach(function (pending) { | 312 | this.promise._pending.forEach(function (pending) { |
352 | setImmediate(function () { | 313 | nextTick(function () { |
353 | value.sendPromise.apply(value, pending); | 314 | value.sendPromise.apply(value, pending); |
354 | }); | 315 | }); |
355 | }); | 316 | }); |
@@ -380,7 +341,7 @@ var PrimordialPromise = Creatable.create({ | |||
380 | } else { | 341 | } else { |
381 | var args = arguments, | 342 | var args = arguments, |
382 | value = this._value; | 343 | value = this._value; |
383 | setImmediate(function () { | 344 | nextTick(function () { |
384 | value.sendPromise.apply(value, args); | 345 | value.sendPromise.apply(value, args); |
385 | }); | 346 | }); |
386 | } | 347 | } |
@@ -415,6 +376,14 @@ var Promise = PrimordialPromise.create({}, { // Descriptor for each of the three | |||
415 | } | 376 | } |
416 | }, | 377 | }, |
417 | 378 | ||
379 | spread: { | ||
380 | value: function (fulfilled, rejected) { | ||
381 | return this.all().then(function (args) { | ||
382 | return fulfilled.apply(void 0, args); | ||
383 | }, rejected); | ||
384 | } | ||
385 | }, | ||
386 | |||
418 | then: { | 387 | then: { |
419 | value: function (fulfilled, rejected) { | 388 | value: function (fulfilled, rejected) { |
420 | var self = this; | 389 | var self = this; |
@@ -425,7 +394,7 @@ var Promise = PrimordialPromise.create({}, { // Descriptor for each of the three | |||
425 | try { | 394 | try { |
426 | deferred.resolve(fulfilled ? fulfilled(value) : value); | 395 | deferred.resolve(fulfilled ? fulfilled(value) : value); |
427 | } catch (error) { | 396 | } catch (error) { |
428 | console.log(error.stack); | 397 | console.error(error.stack); |
429 | deferred.reject(error.message, error); | 398 | deferred.reject(error.message, error); |
430 | } | 399 | } |
431 | } | 400 | } |
@@ -445,7 +414,7 @@ var Promise = PrimordialPromise.create({}, { // Descriptor for each of the three | |||
445 | } |