@@ -43,17 +43,30 @@ export class BoundCallbackObservable<T> extends Observable<T> {
4343 * `bindCallback` is not an operator because its input and output are not
4444 * Observables. The input is a function `func` with some parameters, but the
4545 * last parameter must be a callback function that `func` calls when it is
46- * done. The output of `bindCallback` is a function that takes the same
46+ * done.
47+ *
48+ * The output of `bindCallback` is a function that takes the same
4749 * parameters as `func`, except the last one (the callback). When the output
4850 * function is called with arguments, it will return an Observable where the
4951 * results will be delivered to.
5052 *
53+ * If `func` depends on some context (`this` property), that context will be set
54+ * to the same context that returned function has at call time. In particular, if `func`
55+ * is usually called as method of some object, in order to preserve proper behaviour,
56+ * it is recommended to set context of output function to that object as well,
57+ * provided `func` is not already bound.
58+ *
5159 * @example <caption>Convert jQuery's getJSON to an Observable API</caption>
5260 * // Suppose we have jQuery.getJSON('/my/url', callback)
5361 * var getJSONAsObservable = Rx.Observable.bindCallback(jQuery.getJSON);
5462 * var result = getJSONAsObservable('/my/url');
5563 * result.subscribe(x => console.log(x), e => console.error(e));
5664 *
65+ * @example <caption>Use bindCallback on object method</caption>
66+ * const boundMethod = Rx.Observable.bindCallback(someObject.methodWithCallback);
67+ * boundMethod.call(someObject) // make sure methodWithCallback has access to someObject
68+ * .subscribe(subscriber);
69+ *
5770 * @see {@link bindNodeCallback }
5871 * @see {@link from }
5972 * @see {@link fromPromise }
@@ -72,14 +85,15 @@ export class BoundCallbackObservable<T> extends Observable<T> {
7285 static create < T > ( func : Function ,
7386 selector : Function | void = undefined ,
7487 scheduler ?: IScheduler ) : ( ...args : any [ ] ) => Observable < T > {
75- return ( ...args : any [ ] ) : Observable < T > = > {
76- return new BoundCallbackObservable < T > ( func , < any > selector , args , scheduler ) ;
88+ return function ( this : any , ...args : any [ ] ) : Observable < T > {
89+ return new BoundCallbackObservable < T > ( func , < any > selector , args , this , scheduler ) ;
7790 } ;
7891 }
7992
8093 constructor ( private callbackFunc : Function ,
8194 private selector : Function ,
8295 private args : any [ ] ,
96+ private context : any ,
8397 private scheduler : IScheduler ) {
8498 super ( ) ;
8599 }
@@ -112,20 +126,20 @@ export class BoundCallbackObservable<T> extends Observable<T> {
112126 // use named function instance to avoid closure.
113127 ( < any > handler ) . source = this ;
114128
115- const result = tryCatch ( callbackFunc ) . apply ( this , args . concat ( handler ) ) ;
129+ const result = tryCatch ( callbackFunc ) . apply ( this . context , args . concat ( handler ) ) ;
116130 if ( result === errorObject ) {
117131 subject . error ( errorObject . e ) ;
118132 }
119133 }
120134 return subject . subscribe ( subscriber ) ;
121135 } else {
122- return scheduler . schedule ( BoundCallbackObservable . dispatch , 0 , { source : this , subscriber } ) ;
136+ return scheduler . schedule ( BoundCallbackObservable . dispatch , 0 , { source : this , subscriber, context : this . context } ) ;
123137 }
124138 }
125139
126- static dispatch < T > ( state : { source : BoundCallbackObservable < T > , subscriber : Subscriber < T > } ) {
140+ static dispatch < T > ( state : { source : BoundCallbackObservable < T > , subscriber : Subscriber < T > , context : any } ) {
127141 const self = ( < Subscription > < any > this ) ;
128- const { source, subscriber } = state ;
142+ const { source, subscriber, context } = state ;
129143 const { callbackFunc, args, scheduler } = source ;
130144 let subject = source . subject ;
131145
@@ -150,7 +164,7 @@ export class BoundCallbackObservable<T> extends Observable<T> {
150164 // use named function to pass values in without closure
151165 ( < any > handler ) . source = source ;
152166
153- const result = tryCatch ( callbackFunc ) . apply ( this , args . concat ( handler ) ) ;
167+ const result = tryCatch ( callbackFunc ) . apply ( context , args . concat ( handler ) ) ;
154168 if ( result === errorObject ) {
155169 subject . error ( errorObject . e ) ;
156170 }
0 commit comments