-
Notifications
You must be signed in to change notification settings - Fork 25.4k
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
New feature: allow injectable SubscriptionStrategies for AsyncPipe #23571
Comments
Wouldn't it be better to provide service that converts object to class DerivableToObservable<T=any> implements ToObservable<Derivable<T>, T> {
supports(obj: any): obj is Derivable<T> {
return isDerivable(obj);
}
createObservable(obj: Derivable<T>): Observable<T> {
const stop$ = atom<boolean>(false);
return new Observable<T>(sub => {
obj.react(v => sub.next(v), { until: stop$ });
return () => stop$.set(true);
});
}
} It's simpler and could be reused for other purposes than in async pipe. |
Rxjs is not a panacea for asynchrony. While observables solve a lot of problems, they're not a catch-all for all asynchronous operations. I argue that the AsyncPipe ought to support every type of asynchrony. We've been working with asynchronous proxies for example, which would not work with your example. I think the AsyncPipe is a very powerful tool that is currently hindered by its own implementation. I'd like to free up the code. |
While I agree to this proposal, A little problem in JavaScript could be which of Eventually all types providing asynchronous-compatible functionality should implement |
I'm with @trotyl on this one - it would be reasonably straightforward to ship pipes for MobX or whatever, without adding to the complexity/code-size of core. It's likely we'll roll the async pipe's facility into the core of the templating language in the near future, and I'd rather not introduce an API that we won't be able to support in the future. Better for this to live as a third-party OSS library, I think? |
Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends. Find more details about Angular's feature request process in our documentation. |
Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage. We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package. You can find more details about the feature request process in our documentation. |
There are 2 related points discussed here:
Going to close this one as the issue is partially resolved and we don't want to put too many responsibilities into the async pipe. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I'm submitting a...
Current behavior
The
AsyncPipe
currently supports two types of asynchronous objects,Promise<T>
andObservable<T>
. It is impossible to add additionalSubscriptionStrategies
.Expected behavior
There are other implementations of asynchronous delivery of values than just
Observable
andPromise
. MobX comes to mind, and our team has been working extensively with a state management library we developed in-house called Sherlock. I'm sure there are other libraries out there, and there will be others coming along in the future as well.I propose adding a multi provider for
SUBSCRIPTION_STRATEGY_FACTORIES
toCommonModule
. When using a factory pattern, the respective strategies created by those factories could hold an internal reference to aSubscriptionLike
, cleaning up some code inAsyncPipe#dispose
.By default, an
ObservableStrategyFactory
and aPromiseStrategyFactory
should be provided, but consumers would be able to provide their own strategy factories to this provider.A
SubscriptionStrategyFactory
should be able to determine whether it supports a passed in asynchronous object, and if so, create a newSubscriptionStrategy<T>
for that type.Proposed implementation:
This proposal requires quite a few changes. In order to let strategies keep some internal state on their subscription, a factory pattern is needed (not unlike the differ factories for
ngForOf
). These factories should declare their support for different flavours of asynchronous value sources, and create a strategy when needed.An interface for
SubscriptionStrategyFactory
would look something like this:An interface for
SubscriptionStrategy
would look like this:The method
AsyncPipe#_selectStrategy
would have to be refactored to this:The implementation for
ObservableStrategy
, for instance, would then look something like this:Implementing a new
SubscriptionStrategy
would then look something like this, in this case implementing a strategy for a SherlockDerivable<T>
.I'm not too familiar with how MobX handles reactions, but I presume
@observable
reactions could be easily implemented with this pattern.Minimal reproduction of the problem with instructions
n/a
What is the motivation / use case for changing the behavior?
Benefits are:
AsyncPipe
a generic link between theChangeDetectorRef
and any asynchronous emitterAsyncPipe
will 'future-proof' its implementationAsyncPipe
handle the change detectionI've been working on implementing this for a couple of days, but it got a bit larger than I expected. I have it working quite nicely, but it touches upon some core architecture principles in
AsyncPipe
and I'd like to test the waters and see what people think of this proposal before I clean up a PR and write extensive tests.Environment
Not applicable.
The text was updated successfully, but these errors were encountered: