richnologies / ngx-stripe

Angular 6+ wrapper for StripeJS
MIT License
217 stars 77 forks source link

[BUG] Injecting StripeService in component cause change detection runs #223

Closed Ostrovyi closed 10 months ago

Ostrovyi commented 1 year ago

Import stripe service in component or another service: import {StripeService} from 'ngx-stripe'; Inject it: private stripeService = inject(StripeService); Open Angular extension in Developer Tools, section Profiler. Click start record. Or in app.component place this function:

ngDoCheck() {
    console.log('change detection');
}

You will see that change detection triggers multiple times.

richnologies commented 1 year ago

Hi @Ostrovyi,

I can see what you mean, but for what I've investigated the problem is not related to the injectStripe function. If I add the StripeFactoryService to my component's constructor and then create the instance using this.factory.create the same things happens. Even if I inject the StripeService and then assign the stripe instance to a variable in the component, I get the exact same behaviour.

I don't have much experience myself with the DoCheck lifecycle hook myself, but I guess it might be because once you inject one of these services the library has to download StripeJS and attached to DOM? I'm just speculating, really no idea.

If you have any suggestions about what the reason could be or what kind of changes we can do to prevent it I'm more that happy to dig into this. Of course, a PR is always welcome.

Kind regards

R

richnologies commented 10 months ago

I'm closing this due to inactivity