Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.
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

Description

@larssn

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.

Activity

  1. NickIliev commented on Nov 13, 2018

    @NickIliev
  2. lambourn commented on May 15, 2019

    @lambourn

    @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 join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions