Giter Club home page Giter Club logo

Comments (7)

arturovt avatar arturovt commented on July 21, 2024

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.

muuvmuuv avatar muuvmuuv commented on July 21, 2024

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.

muuvmuuv avatar muuvmuuv commented on July 21, 2024

I see there was already a try on until-destroy side to make something like that, in #122.

from until-destroy.

arturovt avatar arturovt commented on July 21, 2024

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.

arturovt avatar arturovt commented on July 21, 2024

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.

muuvmuuv avatar muuvmuuv commented on July 21, 2024

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.

devfservant avatar devfservant commented on July 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.