-
Notifications
You must be signed in to change notification settings - Fork 212
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
apply operator #73
Comments
Interesting proposal! So this operator is semantic sugar to better align in chain of operators ? I like it! Let's go ahead and add it. |
Yes it's just syntactic sugar but I think it encourages code that factors out common operator chains into functions that can be reused and composed together. I've seen this when wrapping 3rd party APIs, sometimes there are many API calls that are similar in their error codes and can share error handling logic, retry logic, etc. I imagine there are other scenarios too. I've recently also been looking at another common pattern that arises with
This is interesting because it is basically setting up a function call (the
I guess it's a separate idea from apply, so maybe I'll submit a separate issue for it :) |
I like this a lot ! |
@acchou the |
Name and description
I'd like to propose a very simple operator,
apply
, which takes a transformation function that takes an Observable and returns an Observable, and simply runs it and returns its value:Motivation for inclusion
It's idiomatic to write new operators as extensions of ObservableType. This makes sense for many custom operators because it preserves the chaining syntax of RxSwift. But sometimes this style of extension operator is uncomfortable when the operator is not generic in nature, but might perform some combination of application-specific actions, and may even have side-effects.
In these cases it makes sense to write a simple function to add operations to an Observable, for example:
This function can then be applied to several requests to apply consistent retries, side-effects, and error handling:
But this syntax is awkward because it requires each policy to be wrapped around the observable. The use of
apply
returns this to a more familiar chaining syntax:This is especially useful when composing multiple transform functions:
There may also be a desire to have a version with one or more arguments to apply:
Example of use
See above.
The text was updated successfully, but these errors were encountered: