1
0

helpers.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. (function() {
  2. var Behavior, combineArray, combineWithFunction,
  3. __slice = [].slice;
  4. Behavior = require('./behavior');
  5. exports.combine = function() {
  6. var args;
  7. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  8. if (args.length === 1 && Array.isArray(args[0])) {
  9. return combineArray(args[0]);
  10. } else if (typeof args[args.length - 1] === 'function') {
  11. return combineWithFunction(args);
  12. } else {
  13. throw new Error("Invalid object type");
  14. }
  15. };
  16. combineArray = function(array) {
  17. var behavior;
  18. return behavior = new Behavior(function() {
  19. var element, i, outputArray, ready, _i, _len,
  20. _this = this;
  21. outputArray = array.slice();
  22. ready = false;
  23. for (i = _i = 0, _len = array.length; _i < _len; i = ++_i) {
  24. element = array[i];
  25. if (element.constructor.name === 'Behavior') {
  26. (function(element, i) {
  27. return _this.subscribe(element.onValue(function(value, metadata) {
  28. if (ready) {
  29. outputArray = outputArray.slice();
  30. }
  31. outputArray[i] = value;
  32. if (ready) {
  33. return _this.emitValue(outputArray, metadata);
  34. }
  35. }));
  36. })(element, i);
  37. }
  38. }
  39. ready = true;
  40. return this.emitValue(outputArray);
  41. });
  42. };
  43. combineWithFunction = function(args) {
  44. var fn;
  45. fn = args.pop();
  46. return combineArray(args).map(function(argsArray) {
  47. return fn.apply(null, argsArray);
  48. });
  49. };
  50. }).call(this);