Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.00 KB | None | 0 0
  1. var B = require("bluebird/js/main/promise")();
  2. var Bproto = B.prototype;
  3.  
  4. var deferredPrototype = B.pending().constructor.prototype;
  5.  
  6. deferredPrototype.makeNodeResolver = function() {
  7. return this.asCallback;
  8. };
  9.  
  10. function bind(fn, ctx) {
  11. return function() {
  12. return fn.apply(ctx, arguments);
  13. };
  14. }
  15.  
  16. module.exports = Q;
  17. function Q(value) {
  18. return B.cast(value),
  19. }
  20.  
  21. Object.defineProperty(Q, "longStackSupport", {
  22. set: function(val) {
  23. if (val) {
  24. B.longStackTraces();
  25. }
  26. },
  27. get: function() {
  28. return B.haveLongStackTraces();
  29. }
  30. });
  31.  
  32. Q.reject = B.rejected;
  33. Q.defer = function() {
  34. var b = B.pending();
  35. b.resolve = bind(b.fulfill, b);
  36. b.reject = bind(b.reject, b);
  37. b.notify = bind(b.progress, b);
  38. return b;
  39. };
  40. Q.all = B.All;
  41. Q.allSettled = B.allSettled;
  42. Bproto.allSettled = Bproto.all;
  43.  
  44. Q.delay = function (object, timeout) {
  45. if (timeout === void 0) {
  46. timeout = object;
  47. object = void 0;
  48. }
  49. return Q(object).delay(timeout);
  50. };
  51.  
  52. Q.timeout = function (object, ms, message) {
  53. return Q(object).timeout(ms, message);
  54. };
  55.  
  56. Q.spread = function(a, b, c) {
  57. return Q(a).spread(b, c);
  58. };
  59.  
  60. Q.join = function (x, y) {
  61. return Q.spread([x, y], function (x, y) {
  62. if (x === y) {
  63. // TODO: "===" should be Object.is or equiv
  64. return x;
  65. } else {
  66. throw new Error("Can't join: not the same: " + x + " " + y);
  67. }
  68. });
  69. };
  70.  
  71. Q.race = B.race;
  72.  
  73. Q["try"] = function(callback) {
  74. return B.try(callback);
  75. };
  76.  
  77.  
  78. Q.fapply = function (callback, args) {
  79. return B.try(callback, args);
  80. };
  81.  
  82. Q.fcall = function (callback) {
  83. return B.try(callback, [].slice.call(arguments, 1));
  84. };
  85.  
  86. Q.fbind = function(callback) {
  87. var args = [].slice.call(arguments, 1);
  88. return function() {
  89. return B.try(callback, [].slice.call(arguments).concat(args), this);
  90. };
  91. };
  92.  
  93. Q.async = B.coroutine;
  94. Q.spawn = B.spawn;
  95. Q.cast = B.cast;
  96. Q.isPromise = B.is;
  97.  
  98. Q.promised = promised;
  99. function promised(callback) {
  100. return function () {
  101. return Q.spread([this, all(arguments)], function (self, args) {
  102. return callback.apply(self, args);
  103. });
  104. };
  105. }
  106.  
  107. Q.isThenable = function(o) {
  108. return o && typeof o.then === "function";
  109. };
  110.  
  111. Q.Promise = function(handler) {
  112. return new B(handler);
  113. };
  114.  
  115. Bproto.inspect = Bproto.toJSON;
  116. Bproto.thenResolve = Bproto.thenReturn;
  117. Bproto.thenReject = Bproto.thenThrow;
  118. Bproto.progress = Bproto.progressed;
  119.  
  120. Bproto.dispatch = function(op, args) {
  121. if (op === "then" || op === "get" || op === "post" || op === "keys") {
  122. return this[op].apply(this, args);
  123. }
  124. return B.rejected(new Error(
  125. "Fulfilled promises do not support the " + op + " operator"
  126. ));
  127. };
  128.  
  129. Bproto.post = function(name, args, thisp) {
  130. return this.then(function(val) {
  131. if (name == null) {
  132. return val.apply(thisp, args);
  133. }
  134. else {
  135. return val[name].apply(val, args);
  136. }
  137. });
  138. };
  139.  
  140. Bproto.invoke = function(name) {
  141. var args = [].slice.call(arguments, 1);
  142. return this.post(name, args);
  143. };
  144.  
  145. Bproto.fapply = function(args) {
  146. return this.post(void 0, args);
  147. };
  148.  
  149. Bproto.fcall = function() {
  150. var args = [].slice.call(arguments);
  151. return this.post(void 0, args);
  152. };
  153.  
  154. Bproto.keys = function() {
  155. return this.then(function(val) {
  156. return Object.keys(val);
  157. });
  158. };
  159.  
  160. Bproto.timeout = function(ms, message) {
  161. var self = this;
  162. setTimeout(function() {
  163. var e = new Error(message || "Timed out after " + ms + " ms");
  164. self._reject(e);
  165. }, ms);
  166. return this.then();
  167. };
  168.  
  169. function delay(ms, val) {
  170. var d = B.pending();
  171. setTimeout(function(){
  172. d.fulfill(val);
  173. }, ms);
  174. return d.promise;
  175. }
  176.  
  177. Bproto.delay = function(ms) {
  178. return this.then(function(value){
  179. return delay(ms, value);
  180. });
  181. };
  182.  
  183. var b = B.fulfilled();
  184. function thrower(e){
  185. process.nextTick(function(){
  186. throw e;
  187. });
  188. }
  189. Q.nextTick = function(fn) {
  190. b.then(fn).caught(thrower);
  191. };
  192.  
  193. Q.resolve = B.fulfilled;
  194. Q.fulfill = B.fulfilled;
  195. Q.isPromiseAlike = Q.isThenable;
  196. Q.when = function(a, b, c, d) {
  197. return B.cast(a).then(b, c, d);
  198. };
  199. Q.fail = function(a, b) {
  200. return B.cast(a).caught(b);
  201. };
  202. Q.fin = function(a, b) {
  203. return B.cast(a).lastly(b);
  204. };
  205. Q.progress = function(a, b) {
  206. return B.cast(a).progress(b);
  207. };
  208. Q.thenResolve = function(a, b) {
  209. return B.cast(a).thenReturn(b);
  210. };
  211. Q.thenReject = function(a, b) {
  212. return B.cast(a).thenThrow(b);
  213. };
  214. Q.isPending = function(a, b) {
  215. return B.cast(a).isPending()
  216. };
  217. Q.isFulfilled = function(a, b) {
  218. return B.cast(a).isFulfilled();
  219. };
  220. Q.isRejected = function(a, b) {
  221. return B.cast(a).isRejected();
  222. };
  223. Q.master = function(a, b) {
  224. return a;
  225. };
  226. Q.makePromise = function (b) {
  227. return Q.Promise(b);
  228. };
  229. Q.dispatch = function(a, b, c) {
  230. return B.cast(a).dispatch(b, c);
  231. };
  232. Q.get = function(a, b) {
  233. return B.cast(a).get(b);
  234. };
  235. Q.keys = function(a, b) {
  236. return B.cast(a).keys();
  237. };
  238. Q.post = function(a, b, c) {
  239. return B.cast(a).post(b, c);
  240. };
  241. Q.mapply = function(a, b, c) {
  242. return B.cast(a).post(b, c);
  243. };
  244. Q.send = function(a, b) {
  245. return B.cast(a).post(b, [].slice.call(arguments, 2));
  246. };
  247. Q.set = function(a, b, c) {
  248. return B.cast(a).then(function(val){
  249. return val[b] = c;
  250. });
  251. };
  252. Q.delete = function(a, b) {
  253. return B.cast(a).then(function(val){
  254. return delete val[b];
  255. });
  256. };
  257. Q.nearer = function(v) {
  258. if( B.is(v) && v.isFulfilled()) {
  259. return v.inspect().value;
  260. }
  261. return v;
  262. };
  263.  
  264. Bproto.fail = Bproto.caught;
  265. Bproto.fin = Bproto.lastly;
  266.  
  267. Bproto.mapply = Bproto.post;
  268. Bproto.fbind = function() {
  269. return Q.fbind.apply(Q, [this].concat(Array.prototype.slice.call(arguments)));
  270. };
  271.  
  272. Bproto.send = function(){
  273. return this.post(name, [].slice.call(arguments));
  274. };
  275.  
  276. Bproto.mcall = Bproto.send;
  277.  
  278. //wtf?
  279. Bproto.passByCopy = function(v) {
  280. return v;
  281. };
  282.  
  283. var NQ = {};
  284.  
  285. NQ.makeNodeResolver = function (resolve) {
  286. return function (error, value) {
  287. if (error) {
  288. resolve(Q.reject(error));
  289. } else if (arguments.length > 2) {
  290. resolve(Array.prototype.slice.call(arguments, 1));
  291. } else {
  292. resolve(value);
  293. }
  294. };
  295. };
  296.  
  297. Q.nfapply = function(callback, args) {
  298.  
  299. };
  300.  
  301. /*Directly copypasted from Q */
  302. //// BEGIN UNHANDLED REJECTION TRACKING
  303.  
  304. // This promise library consumes exceptions thrown in handlers so they can be
  305. // handled by a subsequent promise. The exceptions get added to this array when
  306. // they are created, and removed when they are handled. Note that in ES6 or
  307. // shimmed environments, this would naturally be a `Set`.
  308.  
  309. var unhandledReasons = [];
  310. var unhandledRejections = [];
  311. var unhandledReasonsDisplayed = false;
  312. var trackUnhandledRejections = true;
  313. function displayUnhandledReasons() {
  314. if (
  315. !unhandledReasonsDisplayed &&
  316. typeof window !== "undefined" &&
  317. window.console
  318. ) {
  319. console.warn("[Q] Unhandled rejection reasons (should be empty):",
  320. unhandledReasons);
  321. }
  322.  
  323. unhandledReasonsDisplayed = true;
  324. }
  325.  
  326. function logUnhandledReasons() {
  327. for (var i = 0; i < unhandledReasons.length; i++) {
  328. var reason = unhandledReasons[i];
  329. console.warn("Unhandled rejection reason:", reason);
  330. }
  331. }
  332.  
  333. function resetUnhandledRejections() {
  334. unhandledReasons.length = 0;
  335. unhandledRejections.length = 0;
  336. unhandledReasonsDisplayed = false;
  337.  
  338. if (!trackUnhandledRejections) {
  339. trackUnhandledRejections = true;
  340.  
  341. // Show unhandled rejection reasons if Node exits without handling an
  342. // outstanding rejection. (Note that Browserify presently produces a
  343. // `process` global without the `EventEmitter` `on` method.)
  344. if (typeof process !== "undefined" && process.on) {
  345. process.on("exit", logUnhandledReasons);
  346. }
  347. }
  348. }
  349.  
  350. function trackRejection(promise, reason) {
  351. if (!trackUnhandledRejections) {
  352. return;
  353. }
  354.  
  355. unhandledRejections.push(promise);
  356. if (reason && typeof reason.stack !== "undefined") {
  357. unhandledReasons.push(reason.stack);
  358. } else {
  359. unhandledReasons.push("(no stack) " + reason);
  360. }
  361. displayUnhandledReasons();
  362. }
  363.  
  364. function untrackRejection(promise) {
  365. if (!trackUnhandledRejections) {
  366. return;
  367. }
  368.  
  369. var at = unhandledRejections.indexOf(promise);
  370. if (at !== -1) {
  371. unhandledRejections.splice(at, 1);
  372. unhandledReasons.splice(at, 1);
  373. }
  374. }
  375.  
  376. Q.resetUnhandledRejections = resetUnhandledRejections;
  377.  
  378. Q.getUnhandledReasons = function () {
  379. // Make a copy so that consumers can't interfere with our internal state.
  380. return unhandledReasons.slice();
  381. };
  382.  
  383. Q.stopUnhandledRejectionTracking = function () {
  384. resetUnhandledRejections();
  385. if (typeof process !== "undefined" && process.on) {
  386. process.removeListener("exit", logUnhandledReasons);
  387. }
  388. trackUnhandledRejections = false;
  389. };
  390.  
  391. resetUnhandledRejections();
  392. /*Directly copypasted from Q end */
  393.  
  394.  
  395.  
  396. B.onPossiblyUnhandledRejection(function(reason, promise){
  397. trackRejection(promise, reason);
  398. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement