Skip to content

onRouterReuse implementation #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ritox842
Copy link

@ritox842 ritox842 commented Oct 8, 2021

I've reverted you latest change with the new lifecycle hooks and propose my own approach.

@ritox842 ritox842 changed the title onRouterReuse implementatio onRouterReuse implementation Oct 8, 2021
@@ -77,6 +77,10 @@ export class AppRouteReuseStrategy implements RouteReuseStrategy {
if (DEBUG) {
console.log('%cDEBUG[retrieve]', 'background:green', {path});
}

// @ts-ignore
handle.componentRef.instance.onRouterReuse()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handle type isn't implemented correctly so I'm wrapping it in @ts-ignore.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I'm curious as to how you found this private API (via debugger?)

I can only access the public API with my IDE (IntelliJ points me to d.ts files)

If you have tips to get access the underlying Angular code I'm all ears :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just familiar with bind-query-param repo... You can see it with debuggers or just look into its source code...

Of course that this is just a POC... You need to open a detailed issue for bind-query-param repo explaining the issue and asking for making it public (Calling it directly won't work for controls that uses a special URL parser...)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got ya, thanks :)

this.loadedAt = Date.now();
}

ngOnDestroy(): void {
this.manager.destroy();
onRouterReuse(): void {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a callback function named onRouterReuse that it been called every time the route is reused.
We also use router.events because we can't change the urlParams before the route completes, Otherwise it mess the route change.

Then I call a private function updateQueryParams with the form value.
We might need ti change this method to be public in bind-query-param repo.

.subscribe(() => {
// @ts-ignore
this.manager.updateQueryParams(this.form.value);
})
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice I like it! It'd be great if a default implementation could be provided somehow (to reduce the boilerplate later)

 onRouterReuse(): void {
    this.router.events.pipe(filter((e): e is NavigationEnd => e instanceof NavigationEnd),
      take(1))
      .subscribe(() => {
        // yield here, code specific to a component
      })
  }

I'm not sure how to go about this. The closest I got was this ugly thing:

// OnRouterReuseImpl.ts
export class OnRouterReuseImpl {
  fn?: () => void;
  constructor(private router: Router) {
  }

  onRouterReuse(): void {
    this.router.events.pipe(filter((e): e is NavigationEnd => e instanceof NavigationEnd),
      take(1))
      .subscribe(() => {
        if (this.fn) {
          this.fn();
        }
      })
  }

}

// my-form.component.ts
private routeReuse: OnRouterReuseImpl;
constructor(private factory: BindQueryParamsFactory,
            private router: Router) {
    this.routeReuse = new OnRouterReuseImpl(router);
    this.routeReuse.fn = () => {
      // @ts-ignore
      this.manager.updateQueryParams(this.form.value);
    }
  }

  onRouterReuse(): void {
    this.routeReuse.onRouterReuse();
  }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, I didn't manage to add Router to app-route-reuse-strategy.ts because of circular dependencies...
I think that this whole function should be inside bind-query-param repo so you won't repeat yourself, Or just keep it as you did with some kind of common service that handles it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

I’m finding using a custom route reuse strategy quite tricky in practice.

And having two events (attached/detached) seems to be quite useful for the app I’m working on.

Thanks for your input though, I’ll have to think it some more :)

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

Successfully merging this pull request may close these issues.

3 participants