@@ -5,7 +5,14 @@ import {concat, fillArray} from '../utils/collections';
55import { spread } from '../utils/functions' ;
66import never from '../primary/never' ;
77
8-
8+ function collect ( source , keys , values ) {
9+ for ( var prop in source ) {
10+ if ( source . hasOwnProperty ( prop ) ) {
11+ keys . push ( prop ) ;
12+ values . push ( source [ prop ] ) ;
13+ }
14+ }
15+ }
916
1017function defaultErrorsCombinator ( errors ) {
1118 let latestError ;
@@ -23,7 +30,7 @@ function Combine(active, passive, combinator) {
2330 Stream . call ( this ) ;
2431 this . _activeCount = active . length ;
2532 this . _sources = concat ( active , passive ) ;
26- this . _combinator = combinator ? spread ( combinator , this . _sources . length ) : ( x => x ) ;
33+ this . _combinator = combinator ;
2734 this . _aliveCount = 0 ;
2835 this . _latestValues = new Array ( this . _sources . length ) ;
2936 this . _latestErrors = new Array ( this . _sources . length ) ;
@@ -153,11 +160,43 @@ inherit(Combine, Stream, {
153160
154161} ) ;
155162
163+ function combineAsArray ( active , passive = [ ] , combinator ) {
164+ if ( ! Array . isArray ( passive ) ) {
165+ throw new Error ( 'Combine can only combine active and passive collections of the same type.' ) ;
166+ }
167+
168+ combinator = combinator ? spread ( combinator , active . length + passive . length ) : ( x => x ) ;
169+ return active . length === 0 ? never ( ) : new Combine ( active , passive , combinator ) ;
170+ }
171+
172+ function combineAsObject ( active , passive = { } , combinator ) {
173+ if ( typeof passive !== 'object' || Array . isArray ( passive ) ) {
174+ throw new Error ( 'Combine can only combine active and passive collections of the same type.' ) ;
175+ }
156176
157- export default function combine ( active , passive = [ ] , combinator ) {
177+ let keys = [ ] ,
178+ activeObservables = [ ] ,
179+ passiveObservables = [ ] ;
180+
181+ collect ( active , keys , activeObservables ) ;
182+ collect ( passive , keys , passiveObservables ) ;
183+
184+ const objectify = values => {
185+ let event = { } ;
186+ for ( let i = values . length - 1 ; 0 <= i ; i -- ) {
187+ event [ keys [ i ] ] = values [ i ] ;
188+ }
189+ return combinator ? combinator ( event ) : event ;
190+ }
191+
192+ return activeObservables . length === 0 ? never ( ) : new Combine ( activeObservables , passiveObservables , objectify ) ;
193+ }
194+
195+ export default function combine ( active , passive , combinator ) {
158196 if ( typeof passive === 'function' ) {
159197 combinator = passive ;
160- passive = [ ] ;
198+ passive = undefined ;
161199 }
162- return active . length === 0 ? never ( ) : new Combine ( active , passive , combinator ) ;
200+
201+ return Array . isArray ( active ) ? combineAsArray ( active , passive , combinator ) : combineAsObject ( active , passive , combinator ) ;
163202}
0 commit comments