stack.js 985 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. Copyright (c) 2012, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://yuilibrary.com/license/
  5. */
  6. var Stack = function () {
  7. this.errors = [];
  8. this.finished = 0;
  9. this.results = [];
  10. this.total = 0;
  11. };
  12. Stack.prototype = {
  13. add: function (fn) {
  14. var self = this,
  15. index = self.total;
  16. self.total += 1;
  17. return function (err) {
  18. if (err) { self.errors[index] = err; }
  19. self.finished += 1;
  20. self.results[index] = fn.apply(null, arguments);
  21. self.test();
  22. };
  23. },
  24. test: function () {
  25. if (this.finished >= this.total && this.callback) {
  26. this.callback.call(null, this.errors.length ? this.errors : null,
  27. this.results, this.data);
  28. }
  29. },
  30. done: function (callback, data) {
  31. this.callback = callback;
  32. this.data = data;
  33. this.test();
  34. }
  35. };
  36. exports.Stack = Stack;