Comments (7)
Or, had you merged your outstanding work here into the Angular project?
Haha, nope
Implementations are different from a technical perspective since takeUntilDestroyed
relies on the DestroyRef
injection unit. You must always provide it when calling the operator outside of the injection context (e.g., ngOnInit
).
In my large codebases, I used untilDestroyed(this)
over 1000 times, but I started using takeUntilDestroyed
after upgrading to Angular 16. However, I still use untilDestroyed(this)
in services since takeUntilDestroyed
can only be used in components and directives (where DestroyRef
is available).
from until-destroy.
Ah, alright! Thanks for the details. I just tested to migrate a few components, and it is a little tricky. :D
takeUntilDestroyed() can only be used within an injection context such as a constructor, a factory function, a field initializer, or a function used with `runInInjectionContext`. Find more at https://angular.io/errors/NG0203
Error: NG0203: takeUntilDestroyed() can only be used within an injection context such as a constructor, a factory function, a field initializer, or a function used with `runInInjectionContext`. Find more at https://angular.io/errors/NG0203
Considering performance when talking about 1000x (we have a little less), would you say one is better tho “until-destroy” hooks into the constructor and has “one more” import? Or in your case, why did you choose to use takeUntilDestroyed
anyway, just to reduce boilerplate?
Just trying to figure out which way we go, especially when Signals are coming, I guess that takeUntilDestroyed
is kind of developed for the new component api/way NG moving to.
from until-destroy.
I see there was already a try on until-destroy side to make something like that, in #122.
from until-destroy.
The takeUntilDestroyed
requires DestroyRef
outside of the context:
class App {
destroyRef = inject(DestroyRef);
constructor() {
// Can be used w/o arguments.
of(true).pipe(takeUntilDestroyed()).subscribe();
}
ngOnInit() {
// Requires `DestroyRef`
of(true).pipe(takeUntilDestroyed(this.destroyRef)).subscribe();
}
}
from until-destroy.
I don't think there's anything related to performance in this case since both operators are essentially piping source observables with takeUntil(destroySubject)
internally. However, using the decorator forces the compiler to emit different code. You can see more details about this issue in this link: #232 (comment)
from until-destroy.
Thanks for the example. OK, sounds like in general it would be a good "best-practise" to move to inject(DestroyRef)
, since Angular is moving towards that syntax and our codebase is almost only standalone components. Ah, and as well to cover #232.
from until-destroy.
since takeUntilDestroyed can only be used in components and directives (where DestroyRef is available)
I don't understand: from the docs, DestroyRef
may be injected elsewhere than in components and directives, no?
From my understanding, any untilDestroyed(this)
is replaceable by a takeUntilDestroyed(this.destroyRef)
.
Or would I miss something?
from until-destroy.
Related Issues (20)
- Add a util cancel Promises
- Angular 14 ut could not run correctly HOT 1
- Angular 14 ut could not run correctly with a minimal reproducible example
- Angular 14 ut could not run correctly with a a minimal reproducible example HOT 7
- Can I ask what's the utility for this package?
- How to use until-destroy-migration? HOT 4
- NG0204: Token InjectionToken X is missing a ɵprov definition after Angular 15 upgrade HOT 20
- Angular 15 Directive Composition API: Cannot access [component] before initialization. HOT 4
- [Documentation] For using `untilDestroyed` in non Angular apps HOT 1
- untilDestroyed assumes ngDevMode is defined HOT 2
- Warnings for github actions builds
- Component is undefined if @UntilDestroy() is used HOT 2
- Supporting other types of Subscriptions HOT 1
- Support TS 5.0 non experimental decorators for the UntilDestroy decorator
- ERROR Error: ASSERTION ERROR: token must be defined [Expected=> null != undefined <=Actual] HOT 5
- Need upgrade for Angular 17 HOT 2
- Debugger in source code
- Error when using Target 2022 - Similar issue link provided HOT 9
- Use decorator with Singleton service (providedIn: 'root') HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from until-destroy.