@@ -6349,93 +6349,99 @@ Rx.Observable.prototype.flatMapLatest = function(selector, resultSelector, thisA
63496349 } ;
63506350 } ;
63516351
6352- /**
6353- * Converts a callback function to an observable sequence.
6354- *
6355- * @param {Function } function Function with a callback as the last parameter to convert to an Observable sequence.
6356- * @param {Mixed } [context] The context for the func parameter to be executed. If not specified, defaults to undefined.
6357- * @param {Function } [selector] A selector which takes the arguments from the callback to produce a single item to yield on next.
6358- * @returns {Function } A function, when executed with the required parameters minus the callback, produces an Observable sequence with a single value of the arguments to the callback as an array.
6359- */
6360- Observable . fromCallback = function ( func , context , selector ) {
6361- return function ( ) {
6362- var len = arguments . length , args = new Array ( len )
6363- for ( var i = 0 ; i < len ; i ++ ) { args [ i ] = arguments [ i ] ; }
6352+ function createCbObservable ( fn , ctx , selector , args ) {
6353+ var o = new AsyncSubject ( ) ;
63646354
6365- var subject = new AsyncSubject ( ) ;
6355+ args . push ( createCbHandler ( o , ctx , selector ) ) ;
6356+ fn . apply ( ctx , args ) ;
63666357
6367- function handler ( ) {
6368- var len = arguments . length , results = new Array ( len ) ;
6369- for ( var i = 0 ; i < len ; i ++ ) { results [ i ] = arguments [ i ] ; }
6358+ return o . asObservable ( ) ;
6359+ }
63706360
6371- if ( selector ) {
6372- try {
6373- results = selector . apply ( context , results ) ;
6374- } catch ( e ) {
6375- return subject . onError ( e ) ;
6376- }
6361+ function createCbHandler ( o , ctx , selector ) {
6362+ return function handler ( ) {
6363+ var len = arguments . length , results = new Array ( len ) ;
6364+ for ( var i = 0 ; i < len ; i ++ ) { results [ i ] = arguments [ i ] ; }
63776365
6378- subject . onNext ( results ) ;
6379- } else {
6380- if ( results . length <= 1 ) {
6381- subject . onNext . apply ( subject , results ) ;
6382- } else {
6383- subject . onNext ( results ) ;
6384- }
6385- }
6386-
6387- subject . onCompleted ( ) ;
6366+ if ( isFunction ( selector ) ) {
6367+ results = tryCatch ( selector ) . apply ( ctx , results ) ;
6368+ if ( results === errorObj ) { return o . onError ( results . e ) ; }
6369+ o . onNext ( results ) ;
6370+ } else {
6371+ if ( results . length <= 1 ) {
6372+ o . onNext ( results [ 0 ] ) ;
6373+ } else {
6374+ o . onNext ( results ) ;
63886375 }
6376+ }
63896377
6390- args . push ( handler ) ;
6391- func . apply ( context , args ) ;
6378+ o . onCompleted ( ) ;
6379+ } ;
6380+ }
63926381
6393- return subject . asObservable ( ) ;
6394- } ;
6382+ /**
6383+ * Converts a callback function to an observable sequence.
6384+ *
6385+ * @param {Function } fn Function with a callback as the last parameter to convert to an Observable sequence.
6386+ * @param {Mixed } [ctx] The context for the func parameter to be executed. If not specified, defaults to undefined.
6387+ * @param {Function } [selector] A selector which takes the arguments from the callback to produce a single item to yield on next.
6388+ * @returns {Function } A function, when executed with the required parameters minus the callback, produces an Observable sequence with a single value of the arguments to the callback as an array.
6389+ */
6390+ Observable . fromCallback = function ( fn , ctx , selector ) {
6391+ return function ( ) {
6392+ var len = arguments . length , args = new Array ( len )
6393+ for ( var i = 0 ; i < len ; i ++ ) { args [ i ] = arguments [ i ] ; }
6394+ return createCbObservable ( fn , ctx , selector , args ) ;
63956395 } ;
6396+ } ;
63966397
6397- /**
6398- * Converts a Node.js callback style function to an observable sequence. This must be in function (err, ...) format.
6399- * @param {Function } func The function to call
6400- * @param {Mixed } [context] The context for the func parameter to be executed. If not specified, defaults to undefined.
6401- * @param {Function } [selector] A selector which takes the arguments from the callback minus the error to produce a single item to yield on next.
6402- * @returns {Function } An async function which when applied, returns an observable sequence with the callback arguments as an array.
6403- */
6404- Observable . fromNodeCallback = function ( func , context , selector ) {
6405- return function ( ) {
6406- var len = arguments . length , args = new Array ( len ) ;
6407- for ( var i = 0 ; i < len ; i ++ ) { args [ i ] = arguments [ i ] ; }
6398+ function createNodeObservable ( fn , ctx , selector , args ) {
6399+ var o = new AsyncSubject ( ) ;
64086400
6409- var o = new AsyncSubject ( ) ;
6401+ args . push ( createNodeHandler ( o , ctx , selector ) ) ;
6402+ fn . apply ( ctx , args ) ;
64106403
6411- function handler ( ) {
6412- var err = arguments [ 0 ] ;
6413- if ( err ) { return o . onError ( err ) ; }
6404+ return o . asObservable ( ) ;
6405+ }
64146406
6415- var len = arguments . length , results = [ ] ;
6416- for ( var i = 1 ; i < len ; i ++ ) { results [ i - 1 ] = arguments [ i ] ; }
6407+ function createNodeHandler ( o , ctx , selector ) {
6408+ return function handler ( ) {
6409+ var err = arguments [ 0 ] ;
6410+ if ( err ) { return o . onError ( err ) ; }
64176411
6418- if ( isFunction ( selector ) ) {
6419- var results = tryCatch ( selector ) . apply ( context , results ) ;
6420- if ( results === errorObj ) { return o . onError ( results . e ) ; }
6421- o . onNext ( results ) ;
6422- } else {
6423- if ( results . length <= 1 ) {
6424- o . onNext ( results [ 0 ] ) ;
6425- } else {
6426- o . onNext ( results ) ;
6427- }
6428- }
6412+ var len = arguments . length , results = [ ] ;
6413+ for ( var i = 1 ; i < len ; i ++ ) { results [ i - 1 ] = arguments [ i ] ; }
64296414
6430- o . onCompleted ( ) ;
6415+ if ( isFunction ( selector ) ) {
6416+ var results = tryCatch ( selector ) . apply ( ctx , results ) ;
6417+ if ( results === errorObj ) { return o . onError ( results . e ) ; }
6418+ o . onNext ( results ) ;
6419+ } else {
6420+ if ( results . length <= 1 ) {
6421+ o . onNext ( results [ 0 ] ) ;
6422+ } else {
6423+ o . onNext ( results ) ;
64316424 }
6425+ }
64326426
6433- args . push ( handler ) ;
6434- func . apply ( context , args ) ;
6427+ o . onCompleted ( ) ;
6428+ } ;
6429+ }
64356430
6436- return o . asObservable ( ) ;
6437- } ;
6431+ /**
6432+ * Converts a Node.js callback style function to an observable sequence. This must be in function (err, ...) format.
6433+ * @param {Function } fn The function to call
6434+ * @param {Mixed } [ctx] The context for the func parameter to be executed. If not specified, defaults to undefined.
6435+ * @param {Function } [selector] A selector which takes the arguments from the callback minus the error to produce a single item to yield on next.
6436+ * @returns {Function } An async function which when applied, returns an observable sequence with the callback arguments as an array.
6437+ */
6438+ Observable . fromNodeCallback = function ( fn , ctx , selector ) {
6439+ return function ( ) {
6440+ var len = arguments . length , args = new Array ( len ) ;
6441+ for ( var i = 0 ; i < len ; i ++ ) { args [ i ] = arguments [ i ] ; }
6442+ return createNodeObservable ( fn , ctx , selector , args ) ;
64386443 } ;
6444+ } ;
64396445
64406446 function isNodeList ( el ) {
64416447 if ( window . StaticNodeList ) {
@@ -6500,29 +6506,54 @@ Rx.Observable.prototype.flatMapLatest = function(selector, resultSelector, thisA
65006506 return event ;
65016507 }
65026508
6503- function createListener ( element , name , handler ) {
6504- // Standards compliant
6505- if ( element . addEventListener ) {
6506- element . addEventListener ( name , handler , false ) ;
6507- return disposableCreate ( function ( ) {
6508- element . removeEventListener ( name , handler , false ) ;
6509- } ) ;
6509+ function ListenDisposable ( e , n , fn ) {
6510+ this . _e = e ;
6511+ this . _n = n ;
6512+ this . _fn = fn ;
6513+ this . _e . addEventListener ( this . _n , this . _fn , false ) ;
6514+ this . isDisposed = false ;
6515+ }
6516+ ListenDisposable . prototype . dispose = function ( ) {
6517+ if ( ! this . isDisposed ) {
6518+ this . _e . removeEventListener ( this . _n , this . _fn , false ) ;
6519+ this . isDisposed = true ;
65106520 }
6511- if ( element . attachEvent ) {
6512- // IE Specific
6513- var innerHandler = function ( event ) {
6514- handler ( fixEvent ( event ) ) ;
6515- } ;
6516- element . attachEvent ( 'on' + name , innerHandler ) ;
6517- return disposableCreate ( function ( ) {
6518- element . detachEvent ( 'on' + name , innerHandler ) ;
6519- } ) ;
6521+ } ;
6522+
6523+ function AttachEventDisposable ( e , n , fn ) {
6524+ this . _e = e ;
6525+ this . _n = 'on' + n ;
6526+ this . _fn = function ( e ) { fn ( fixEvent ( e ) ) ; } ;
6527+ this . _e . attachEvent ( this . _n , this . _fn ) ;
6528+ this . isDisposed = false ;
6529+ }
6530+ AttachEventDisposable . prototype . dispose = function ( ) {
6531+ if ( ! this . isDisposed ) {
6532+ this . _e . detachEvent ( this . _n , this . _fn ) ;
6533+ this . isDisposed = true ;
65206534 }
6521- // Level 1 DOM Events
6522- element [ 'on' + name ] = handler ;
6523- return disposableCreate ( function ( ) {
6524- element [ 'on' + name ] = null ;
6525- } ) ;
6535+ } ;
6536+ function LevelOneDisposable ( e , n , fn ) {
6537+ this . _e = e ;
6538+ this . _n = 'on' + n ;
6539+ this . _e [ this . _n ] = fn ;
6540+ this . isDisposed = false ;
6541+ }
6542+ LevelOneDisposable . prototype . dispose = function ( ) {
6543+ if ( ! this . isDisposed ) {
6544+ this . _e [ this . _n ] = null ;
6545+ this . isDisposed = true ;
6546+ }
6547+ } ;
6548+
6549+ function createListener ( el , eventName , handler ) {
6550+ if ( el . addEventListener ) {
6551+ return new ListenDisposable ( el , eventName , handler )
6552+ }
6553+ if ( el . attachEvent ) {
6554+ return new AttachEventDisposable ( el , eventName , handler ) ;
6555+ }
6556+ return LevelOneDisposable ( el , eventName , handler ) ;
65266557 }
65276558
65286559 function createEventListener ( el , eventName , handler ) {
0 commit comments