Skip to content
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

feat: introduce PageService #2111

Merged
merged 2 commits into from
Jan 31, 2020
Merged

Conversation

edusperoni
Copy link
Collaborator

@edusperoni edusperoni commented Jan 27, 2020

PR Checklist

What is the current behavior?

To detect navigation we have to manually inject page, listen to events and handle the subscriptions.

What is the new behavior?

Introduces a PageService that has:

  • page: reference to the current page
  • inPage: boolean: Page is the currently displayed page
  • inPage$: Observable<boolean>: will emit true/false when entering/leaving page
  • pageEvents$: Observable<NavigatedData>: observable of page events (use with filter operator)

example implementation:

export class DemoComponent {
    subscribed = false;
    constructor(pageService: PageService) {}

    ngOnInit() {
        this.subscribeToEvents();
        this.pageService.inPage$.subscribe(
            (inPage) => inPage ? this.subscribeToEvents() : this.clearSubscriptions();
        )
    }
    ngOnDestroy() {
        this.clearSubscriptions();
    }

    subscribeToEvents() {
        if (this.subscribed) return;
        this.subscribed = true;
    }

    clearSubscriptions() {
        if (!this.subscribed) return;
        this.subscribed = false;
    }
}

The "RxJs way":

myObservable$ = this.pageService.inPage$.pipe(
    // not in page? unsubscribe to realSource$ and subscribe to NEVER
    switchMap((inPage) => inPage ? realSource$.pipe(materialize()) : NEVER),
    dematerialize()
    // materialize/dematerialize if you care about realSource$ completion from https://codeburst.io/heres-how-i-built-my-very-own-pausable-rxjs-operator-24550123e7a6
);

ngOnInit() {
   this.sub = this.myObservable$.subscribe(...);
}

ngOnDestroy() {
    this.sub.unsubscribe();
}

Provides an "angular way" of managing #374

@cla-bot cla-bot bot added the cla: yes label Jan 27, 2020
@vakrilov
Copy link
Contributor

test

vakrilov
vakrilov previously approved these changes Jan 30, 2020
Copy link
Contributor

@vakrilov vakrilov left a comment

Choose a reason for hiding this comment

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

Awesome work @edusperoni !
Nice improvements in the page-router-outlet code as well.

Only small change request - can you change Destruct<a>ble to Destruct<i>ble

@edusperoni
Copy link
Collaborator Author

Oops! Fixed it now!

@vakrilov
Copy link
Contributor

vakrilov commented Jan 30, 2020

Triggering internal CI

@vakrilov
Copy link
Contributor

test

@jessorlisa
Copy link

@edusperoni Is this still part of the latest @nativescript/angular@10.1.7? It appears to be gone or I am blind 😕 .

@edusperoni
Copy link
Collaborator Author

It seems it was removed in some cleanup. For the moment you can copy the code and put the class in the providers for your component, it should work without an issue.

@jessorlisa
Copy link

@edusperoni Thanks for checking!

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

Successfully merging this pull request may close these issues.

None yet

3 participants