Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

HMR - Services bootstrapped multiple times, but never torn down #708

Closed
1 task done
larssn opened this issue Nov 9, 2018 · 2 comments
Closed
1 task done

HMR - Services bootstrapped multiple times, but never torn down #708

larssn opened this issue Nov 9, 2018 · 2 comments

Comments

@larssn
Copy link

larssn commented Nov 9, 2018

Environment

  • CLI: 5.0.0

  • Cross-platform modules:
    ✔ Component nativescript has 5.0.0 version and is up to date.
    ✔ Component tns-core-modules has 5.0.2 version and is up to date.
    ✔ Component tns-android has 5.0.0 version and is up to date.
    ✔ Component tns-ios has 5.0.0 version and is up to date.

  • Android Runtime: 5.0.0

  • iOS Runtime: 5.0.0

  • Plugin(s): None

  • Node.js: 10.12.0

  • Please, attach your package.json and webpack.config.js as these configurations are usually critical for investigating issues with webpack.

These are default out of the box configs from using tns create

Describe the bug
When HMR tears down the Angular platform on changes, it correctly calls ngOnDestroy for all components etc, however it does not for services, meaning your app can end up in a weirdly initialized state with multiple "singletons" running side by side.

To Reproduce
tns create - create a new angular project with say, the tabbed view.
Insert a constructor in app/core/data.service.ts: constructor() { console.log("DataService constructor"); }
tns run android --hmr - the app starts, "DataService constructor" is written in console as expected.
Change some text in home.component.html that will cause a refresh. "DataService constructor" gets output again.

Expected behavior
ngOnDestroy is a valid lifecycle hook for services as well, as mentioned here: https://angular.io/api/core/OnDestroy

A lifecycle hook that is called when a directive, pipe, or service is destroyed.

You can imagine the trouble you can get in when you start including Rx Observable subscriptions in the constructor of a service, and even more when services start bootstrapping plugins.

So ngOnDestroy should also get called for services.

@NickIliev
Copy link
Contributor

Issue moved to NativeScript/nativescript-angular #1605 via ZenHub

@lambourn
Copy link

@larssn hey - not sure if this is still relevant for you: I just experienced something similar.

However, once I implemented the OnDestroy interface in my @Injectable service, I see that ngOnDestroy gets called when the app gets bootstrapped againg by HMR.

I'm using {N} 5.3 with nativescript-dev-webpack 0.22.0

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants