-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rename fromCallback
to factoryFromCallback
#787
Comments
+1 for renaming, I was also hit confusion first time try to use |
I vote for |
+1 for renaming and should not be in core. I don't like the new name very much, but I don't have a better suggestion right now. |
👎 on both renaming and removing it from core. It is essential for both Node programming but also binding to things such as jQuery etc. The from should also stay as is. After all toPromise isn't consistent either but yet I don't see any outrage over that. That and I hate the word Favtory since it sounds too Java-like. |
We could call it observablifyCallback (like Bluebird's "promisify" method) ;-) |
@mattpodwysocki fair enough, about core membership. But the name? Is Java-likeness really the problem? Plenty of people have tripped over on If not a rename, we could change it to actually return an Observable, not a function. E.g. var source = Rx.Observable.fromCallback(fs.exists, 'file.txt');
// Can be subscribed
source.subscribe(...) If anyone needs the factory function, then simply: function exists(filename) {
return Rx.Observable.fromCallback(fs.exists, filename);
} |
Well more interesting than that, @staltz, it returns a function that returns a stateful Observable that caches it's result, similar to |
Hmm, indeed https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/linq/observable/fromcallback.js#L16 |
No problem other than compatibility concerns, AFAICT. |
So, let's keep the name but fix the functionality... |
I'm inclined to agree with @staltz, we should let users compose the behavior they want here, and with consistent tools. |
A problem with changing the functionality is the signature starts getting really complicated: static fromCallback: <T>(callbackFunc: Function, args: any[], ctx?: Object, selector?: Function, scheduler?: Scheduler) => Observable<T>;
Observable.fromCallback(callbackFunc, ['blish', 'blesh', 'blosh'], {value: 42}, function (err, datum) { return datum; }, Scheduler.nextTick); Should we drop For instance perhaps static fromCallback: <T>(callbackFunc: Function, ...argsOrScheduler: Array<any | Scheduler>) => Observable<T>; Observable.fromCallback(callbackFunc, 'blish', 'blesh', 'blosh', Scheduler.nextTick); |
@staltz ... We wouldn't want to drop selector. It's an important feature for these types of creation methods because it generally saves people from creating a second subscription to |
@Blesh context is absolutely necessary, for example, if I were to call a method on a stateful object, it would lose its const $book = $('book');
const animate = Rx.Observable.fromCallback($book.animate, $book);
animate({
opacity: 0.25,
left: "+=50",
height: "toggle"
}, 5000
).subscribe(x => console.log(done)) |
I would be perfectly willing to rename to names such as |
I think what bites people is that |
@staltz there is a reason here for using an const mkdir = Rx.Observable.fromNodeCallback(fs.mkdir);
const createDirectory = mkdir('tmp');
createDirectory.subscribe(x => console.log('done')) To "share" that behavior you would either have to publish/refCount or worse leading to worse performance. |
|
... this is why I was suggesting that we make the caching behavior of this something that we can compose in. |
@Blesh @staltz I don't get why |
Honestly, the need hasn't arisen. That's probably the only reason it's not in here. |
Yeah, never used AsyncSubject neither |
Hi, jumping into the discussion, In an Angular 2 application, I have cases when I need
Thanks! |
@architruc you're in luck: #850 I'd expect that to land soon. |
I agree here. It might be better to be able to compose the behavior: We can add
@mattpodwysocki wouldn't your example behave the same with Or even nicer with ES function bind: Since |
+1 for core, this is very fundamental and important in getting more people on board with Rx. Anything that means less complexity for people get started and less mistakes in code they write is good IMO. +1 for renaming, this name implies it returns an observable and doesn't make a lot of sense. I think (btw, I think ES function bind at that current syntax is dead, just FYI). Also @Blesh as far as I recall bind is slow and I'd like to have as little overhead as possible in terms of performance in a method (the output of this factory method) that might be called hundreds of thousands of times on the server. |
If that were a major concern, would you not just implement your own
That's tragic. Hopefully |
@Blesh I would side with @benjamingr on this one as |
Also, they can't really do it themselves without it. They'd have to allocate a closure. |
Technically our impl allocates a closure. And to use a trick I learned from you, @benjamingr, I think we can provide a module (since we're all about modularity) that gives them a simple bind function that doesn't allocate a closure: export function bind(fn, thisArg) {
const bound = function boundFn() {
return boundFn.fn.apply(boundFn.thisArg, arguments);
};
bound.fn = fn;
bound.thisArg = thisArg;
return bound;
} |
FWIW, on Angular we found In our experience this code is much more readable:
just my 2c // cc: @petebacondarwin |
Okay, after an offline discussion with @mattpodwysocki, I think we can remove the
|
I'm not sure I understand this. For client side code this sounds fine - but then again for client side code performance of async primitives is almost never an issue - for this reason @IgorMinar 's comment is mostly irrelevant here if we're talking about performance. In terms of consistency I think it might be worth adding since all native methods have it. That's not the issue here again. The more expensive an abstraction is the more tempted users will be to make shortcuts and undermine it. For server side use allocating an extra closure here is a performance killer. It will make me avoid What I'd really want is an If we want to convince people to use observables here, we better give them close to zero overhead abstractions - or they'll take shortcuts. If I have a server and I call 5 async methods every time a user makes a request and I expect to support 20000 users this means 100000 observables in flight - and 100000 I ask you not to repeat our mistakes. |
That's fine, I'm willing to keep it on the methods that match native signature names:
I agree this would be a very cool feature, and I'd also like to see this. Make an issue (or better, submit a PR) and we'll get it in. |
... it should be noted that I'm not sure an |
It cache's the result from the callback for the invocation, I don't think it caches the result based on the invocation arguments. The fact the resulting So |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Since
Observable.fromCallback
doesn't really create an Observable, but rather creates a function, the name is a little confusing.Observable.factoryFromCallback
makes a little more sense, as suggested by @staltz.Related discussion in #729 is around whether or not it should be in Rx "core". It adds a lot of weight for limited value, IMO. I personally have never used this method, but perhaps it's more popular than I know.
factoryFromCallback
?The text was updated successfully, but these errors were encountered: