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