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

Update README.md with pipe(take(1) #146

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,53 +164,54 @@ export class AppComponent implements OnInit, OnDestroy {

ngOnInit() {
// subscribe to cookieconsent observables to react to main events
this.popupOpenSubscription = this.ccService.popupOpen$.subscribe(
this.popupOpenSubscription = this.ccService.popupOpen$.pipe(take(1)).subscribe(
() => {
// you can use this.ccService.getConfig() to do stuff...
});

this.popupCloseSubscription = this.ccService.popupClose$.subscribe(
this.popupCloseSubscription = this.ccService.popupClose$.pipe(take(1)).subscribe(
() => {
// you can use this.ccService.getConfig() to do stuff...
});

this.initializingSubscription = this.ccService.initializing$.subscribe(
this.initializingSubscription = this.ccService.initializing$.pipe(take(1)).subscribe(
(event: NgcInitializingEvent) => {
// the cookieconsent is initilializing... Not yet safe to call methods like `NgcCookieConsentService.hasAnswered()`
console.log(`initializing: ${JSON.stringify(event)}`);
});

this.initializedSubscription = this.ccService.initialized$.subscribe(
this.initializedSubscription = this.ccService.initialized$.pipe(take(1)).subscribe(
() => {
// the cookieconsent has been successfully initialized.
// It's now safe to use methods on NgcCookieConsentService that require it, like `hasAnswered()` for eg...
console.log(`initialized: ${JSON.stringify(event)}`);
});

this.initializationErrorSubscription = this.ccService.initializationError$.subscribe(
this.initializationErrorSubscription = this.ccService.initializationError$.pipe(take(1)).subscribe(
(event: NgcInitializationErrorEvent) => {
// the cookieconsent has failed to initialize...
console.log(`initializationError: ${JSON.stringify(event.error?.message)}`);
});

this.statusChangeSubscription = this.ccService.statusChange$.subscribe(
this.statusChangeSubscription = this.ccService.statusChange$.pipe(take(1)).subscribe(
(event: NgcStatusChangeEvent) => {
// you can use this.ccService.getConfig() to do stuff...
});

this.revokeChoiceSubscription = this.ccService.revokeChoice$.subscribe(
this.revokeChoiceSubscription = this.ccService.revokeChoice$.pipe(take(1)).subscribe(
() => {
// you can use this.ccService.getConfig() to do stuff...
});

this.noCookieLawSubscription = this.ccService.noCookieLaw$.subscribe(
this.noCookieLawSubscription = this.ccService.noCookieLaw$.pipe(take(1)).subscribe(
(event: NgcNoCookieLawEvent) => {
// you can use this.ccService.getConfig() to do stuff...
});
}

ngOnDestroy() {
// unsubscribe to cookieconsent observables to prevent memory leaks
// Although `take(1)` would already prevent memory leaks
// unsubscribe to cookieconsent observables if you're using it in another component (Like different cookies base on different page)
this.popupOpenSubscription.unsubscribe();
this.popupCloseSubscription.unsubscribe();
this.initializingSubscription.unsubscribe();
Expand Down