Skip to content

Commit

Permalink
Merge pull request #172 from foxdonut/fan-out
Browse files Browse the repository at this point in the history
Allow connecting to multiple methods using an array.
  • Loading branch information
briancavalier committed Apr 27, 2015
2 parents 469cf83 + fe79d2b commit 0857ac6
Show file tree
Hide file tree
Showing 3 changed files with 373 additions and 16 deletions.
39 changes: 38 additions & 1 deletion docs/connections.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,34 @@ define({
});
```

Connections can also be made to more than one method. Use an array of Strings instead of a single String to invoke multiple methods:

```js
define({
$plugins: [
{ module: 'wire/connect'},
// other plugins ...
],

component1: {
create: // ...
connect: {
// Whenever component2.doSomething is called,
// component1.doSomethingAlso1 and component1.doSomethingAlso2 will also
// be invoked, with the same parameters.
'component2.doSomething': [
'doSomethingAlso1',
'doSomethingAlso2'
]
}
},

component2: {
create: // ...
}
});
```

# Aspect Oriented Programming (AOP)

**Plugin:** wire/aop
Expand Down Expand Up @@ -304,7 +332,16 @@ define({
// component2.doSomething returns (but not if it throws, see
// afterThrowing below). The return value of component2.doSomething
// will be passed to component1.doSomethingAfterReturning
doSomething: 'component1.doSomethingAfterReturning'
doSomething: 'component1.doSomethingAfterReturning',

// component1.doSomething1 and component1.doSomething2 will be
// invoked after component2.doSomethingElse returns. The return
// value of component2.doSomething will be passed to both
// component1.doSomething1 and component1.doSomething2.
doSomethingElse: [
'component1.doSomething1',
'component1.doSomething2'
]
},

afterThrowing: {
Expand Down
39 changes: 24 additions & 15 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,30 @@ define(function(require) {
* rejects if an error occurs.
*/
function parseIncoming(source, eventName, targetProxy, connect, options, wire, createConnection) {
var promise, methodName;
var promise, methodNames;

if(eventName) {
// 'component.eventName': 'methodName'
// 'component.eventName': 'transform | methodName'

methodName = options;
methodNames = Array.isArray(options) ? options : [ options ];

promise = pipeline(targetProxy, methodName, wire).then(
function(func) {
var invoker = proxyInvoker(targetProxy, func);
promise = when.map(methodNames, function(methodName) {
return pipeline(targetProxy, methodName, wire).then(
function(func) {
var invoker = proxyInvoker(targetProxy, func);

return createConnection(source, eventName, invoker);
}
);
return createConnection(source, eventName, invoker);
}
);
});

} else {
// componentName: {
// eventName: 'methodName'
// eventName: 'transform | methodName'
// }

source = methodName;
promise = wire.getProxy(connect).then(function(srcProxy) {
var name, promises;

Expand Down Expand Up @@ -190,10 +191,6 @@ define(function(require) {
promise = connectOneOutgoing(targetProxy, options);

} else {
// eventName: {
// componentName: 'methodName'
// componentName: 'transform | methodName'
// }
promises = [];

resolveAndConnectOneOutgoing = function(targetRef, targetMethodSpec) {
Expand All @@ -202,8 +199,20 @@ define(function(require) {
});
};

for(name in options) {
promises.push(resolveAndConnectOneOutgoing(name, options[name]));
if (Array.isArray(options)) {
// eventName: [ 'methodName1', 'methodName2' ]
for (var i = 0, t = options.length; i < t; i++) {
promises.push(connectOneOutgoing(targetProxy, options[i]));
}

} else {
// eventName: {
// componentName: 'methodName'
// componentName: 'transform | methodName'
// }
for(name in options) {
promises.push(resolveAndConnectOneOutgoing(name, options[name]));
}
}

promise = when.all(promises);
Expand Down
Loading

0 comments on commit 0857ac6

Please sign in to comment.