@@ -39,11 +39,16 @@ export class BoundNodeCallbackObservable<T> extends Observable<T> {
39
39
* last parameter must be a callback function that `func` calls when it is
40
40
* done. The callback function is expected to follow Node.js conventions,
41
41
* where the first argument to the callback is an error, while remaining
42
- * arguments are the callback result. The output of `bindNodeCallback` is a
42
+ * arguments are the callback result.
43
+ *
44
+ * The output of `bindNodeCallback` is a
43
45
* function that takes the same parameters as `func`, except the last one (the
44
46
* callback). When the output function is called with arguments, it will
45
47
* return an Observable where the results will be delivered to.
46
48
*
49
+ * As in {@link bindCallback}, context (`this` property) of input function will be set to context
50
+ * of returned function, when it is called.
51
+ *
47
52
* @example <caption>Read a file from the filesystem and get the data as an Observable</caption>
48
53
* import * as fs from 'fs';
49
54
* var readFileAsObservable = Rx.Observable.bindNodeCallback(fs.readFile);
@@ -69,14 +74,15 @@ export class BoundNodeCallbackObservable<T> extends Observable<T> {
69
74
static create < T > ( func : Function ,
70
75
selector : Function | void = undefined ,
71
76
scheduler ?: IScheduler ) : ( ...args : any [ ] ) => Observable < T > {
72
- return ( ...args : any [ ] ) : Observable < T > = > {
73
- return new BoundNodeCallbackObservable < T > ( func , < any > selector , args , scheduler ) ;
77
+ return function ( this : any , ...args : any [ ] ) : Observable < T > {
78
+ return new BoundNodeCallbackObservable < T > ( func , < any > selector , args , this , scheduler ) ;
74
79
} ;
75
80
}
76
81
77
82
constructor ( private callbackFunc : Function ,
78
83
private selector : Function ,
79
84
private args : any [ ] ,
85
+ private context : any ,
80
86
public scheduler : IScheduler ) {
81
87
super ( ) ;
82
88
}
@@ -113,26 +119,27 @@ export class BoundNodeCallbackObservable<T> extends Observable<T> {
113
119
// use named function instance to avoid closure.
114
120
( < any > handler ) . source = this ;
115
121
116
- const result = tryCatch ( callbackFunc ) . apply ( this , args . concat ( handler ) ) ;
122
+ const result = tryCatch ( callbackFunc ) . apply ( this . context , args . concat ( handler ) ) ;
117
123
if ( result === errorObject ) {
118
124
subject . error ( errorObject . e ) ;
119
125
}
120
126
}
121
127
return subject . subscribe ( subscriber ) ;
122
128
} else {
123
- return scheduler . schedule ( dispatch , 0 , { source : this , subscriber } ) ;
129
+ return scheduler . schedule ( dispatch , 0 , { source : this , subscriber, context : this . context } ) ;
124
130
}
125
131
}
126
132
}
127
133
128
134
interface DispatchState < T > {
129
135
source : BoundNodeCallbackObservable < T > ;
130
136
subscriber : Subscriber < T > ;
137
+ context : any ;
131
138
}
132
139
133
140
function dispatch < T > ( this : Action < DispatchState < T > > , state : DispatchState < T > ) {
134
141
const self = ( < Subscription > this ) ;
135
- const { source, subscriber } = state ;
142
+ const { source, subscriber, context } = state ;
136
143
// XXX: cast to `any` to access to the private field in `source`.
137
144
const { callbackFunc, args, scheduler } = source as any ;
138
145
let subject = source . subject ;
@@ -162,7 +169,7 @@ function dispatch<T>(this: Action<DispatchState<T>>, state: DispatchState<T>) {
162
169
// use named function to pass values in without closure
163
170
( < any > handler ) . source = source ;
164
171
165
- const result = tryCatch ( callbackFunc ) . apply ( this , args . concat ( handler ) ) ;
172
+ const result = tryCatch ( callbackFunc ) . apply ( context , args . concat ( handler ) ) ;
166
173
if ( result === errorObject ) {
167
174
subject . error ( errorObject . e ) ;
168
175
}
0 commit comments