Skip to content

Commit

Permalink
refactor(core): rename initialize$ subscription into initializing$
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `initialize$`  has been renamed to better reflect when this event is triggered

Use `initialized$` to get notify when the `cookieconsent` **has done** initializing successfully
  • Loading branch information
tinesoft committed Jul 7, 2021
1 parent a994f5e commit 7233aa0
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 29 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ import { Subscription } from 'rxjs';
export class AppComponent implements OnInit, OnDestroy {

//keep refs to subscriptions to be able to unsubscribe later
private popupOpenSubscription: Subscription;
private popupCloseSubscription: Subscription;
private initializeSubscription: Subscription;
private popupOpenSubscription!: Subscription;
private popupCloseSubscription!: Subscription;
private initializingSubscription!: Subscription;
private initializedSubscription!: Subscription;
private initializationErrorSubscription!: Subscription;
private statusChangeSubscription: Subscription;
private revokeChoiceSubscription: Subscription;
private noCookieLawSubscription: Subscription;
private statusChangeSubscription!: Subscription;
private revokeChoiceSubscription!: Subscription;
private noCookieLawSubscription!: Subscription;

constructor(private ccService: NgcCookieConsentService){}

Expand All @@ -166,10 +166,10 @@ export class AppComponent implements OnInit, OnDestroy {
// you can use this.ccService.getConfig() to do stuff...
});

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

this.initializedSubscription = this.ccService.initialized$.subscribe(
Expand Down Expand Up @@ -205,7 +205,9 @@ export class AppComponent implements OnInit, OnDestroy {
// unsubscribe to cookieconsent observables to prevent memory leaks
this.popupOpenSubscription.unsubscribe();
this.popupCloseSubscription.unsubscribe();
this.initializeSubscription.unsubscribe();
this.initializingSubscription.unsubscribe();
this.initializedSubscription.unsubscribe();
this.initializationErrorSubscription.unsubscribe();
this.statusChangeSubscription.unsubscribe();
this.revokeChoiceSubscription.unsubscribe();
this.noCookieLawSubscription.unsubscribe();
Expand Down
14 changes: 8 additions & 6 deletions apps/ngx-cookieconsent-demo/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component, OnInit, OnDestroy, Inject, PLATFORM_ID } from '@angular/core
import { Router, NavigationEnd, Event } from '@angular/router';
import { isPlatformBrowser } from '@angular/common';

import { NgcCookieConsentService, NgcInitializeEvent, NgcInitializationErrorEvent, NgcStatusChangeEvent, NgcNoCookieLawEvent } from 'ngx-cookieconsent';
import { NgcCookieConsentService, NgcInitializingEvent, NgcInitializationErrorEvent, NgcStatusChangeEvent, NgcNoCookieLawEvent } from 'ngx-cookieconsent';

import { filter } from 'rxjs/operators';
import { Subscription } from 'rxjs';
Expand All @@ -17,7 +17,7 @@ export class AppComponent implements OnInit, OnDestroy{
//keep refs to subscriptions to be able to unsubscribe later
private popupOpenSubscription!: Subscription;
private popupCloseSubscription!: Subscription;
private initializeSubscription!: Subscription;
private initializingSubscription!: Subscription;
private initializedSubscription!: Subscription;
private initializationErrorSubscription!: Subscription;
private statusChangeSubscription!: Subscription;
Expand Down Expand Up @@ -48,10 +48,10 @@ export class AppComponent implements OnInit, OnDestroy{
console.log('popuClose');
});

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

this.initializedSubscription = this.ccService.initialized$.subscribe(
Expand Down Expand Up @@ -95,7 +95,9 @@ export class AppComponent implements OnInit, OnDestroy{
// unsubscribe to cookieconsent observables to prevent memory leaks
this.popupOpenSubscription.unsubscribe();
this.popupCloseSubscription.unsubscribe();
this.initializeSubscription.unsubscribe();
this.initializingSubscription.unsubscribe();
this.initializedSubscription.unsubscribe();
this.initializationErrorSubscription.unsubscribe();
this.statusChangeSubscription.unsubscribe();
this.revokeChoiceSubscription.unsubscribe();
this.noCookieLawSubscription.unsubscribe();
Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion libs/ngx-cookieconsent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export {
NgcCookiePosition, NgcCookieLayout, NgcCookieType, NgcCookieTheme, NgcCookieCompliance, NgcCookieOptions
} from './lib/model/index';

export { NgcInitializeEvent, NgcInitializationErrorEvent, NgcStatusChangeEvent, NgcNoCookieLawEvent } from './lib/event/index';
export { NgcInitializingEvent, NgcInitializationErrorEvent, NgcStatusChangeEvent, NgcNoCookieLawEvent } from './lib/event/index';
2 changes: 1 addition & 1 deletion libs/ngx-cookieconsent/src/lib/event/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './status-change.event';
export * from './initialize.event';
export * from './initializing.event';
export * from './initialization-error.event';
export * from './no-cookie-law.event';

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Event fired when Cookie Consent initializes.
*/
export interface NgcInitializeEvent {
export interface NgcInitializingEvent {
/**
* The status of cookie consent
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { NgcCookieConsentModule } from './../ngx-cookieconsent.module';
import { NgcCookieConsentService } from './cookieconsent.service';
import { NgcCookieConsentConfig } from './cookieconsent-config';
import { WindowService } from './window.service';
import { NgcInitializeEvent } from './../event/initialize.event';
import { NgcInitializingEvent } from './../event/initializing.event';
import { NgcStatusChangeEvent } from './../event/status-change.event';
import { NgcNoCookieLawEvent } from './../event/no-cookie-law.event';

Expand Down Expand Up @@ -177,7 +177,7 @@ describe('Service: NgcCookieConsent', () => {
});


it('should emit initialize$ event when calling onInitialise() callback', () => {
it('should emit initializing$ event when calling onInitialise() callback', () => {
TestBed.configureTestingModule({
imports: [NgcCookieConsentModule.forRoot(myConfig)]
});
Expand All @@ -190,7 +190,7 @@ describe('Service: NgcCookieConsent', () => {
'1': 'allow',
'2': 'deny'
};
service.initialize$.subscribe((event: NgcInitializeEvent) => {
service.initializing$.subscribe((event: NgcInitializingEvent) => {
calls++;
expect(event).toEqual({ status: `${statusCallParams[calls]}` });
});
Expand Down
12 changes: 6 additions & 6 deletions libs/ngx-cookieconsent/src/lib/service/cookieconsent.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Subject, Observable } from 'rxjs';
import { NgcCookieConsentStatus } from '../model/common-interfaces';
import { NgcStatusChangeEvent } from '../event/status-change.event';
import { NgcNoCookieLawEvent } from '../event/no-cookie-law.event';
import { NgcInitializeEvent } from '../event/initialize.event';
import { NgcInitializingEvent } from '../event/initializing.event';
import { NgcInitializationErrorEvent } from '../event';
import { NgcCookieConsentConfig } from './cookieconsent-config';
import { WindowService } from './window.service';
Expand Down Expand Up @@ -69,7 +69,7 @@ export class NgcCookieConsentService {
// Observable sources
private popupOpenSource: Subject<void>;
private popupCloseSource: Subject<void>;
private initializeSource: Subject<NgcInitializeEvent>;
private initializingSource: Subject<NgcInitializingEvent>;
private initializedSource: Subject<void>;
private initializationErrorSource: Subject<NgcInitializationErrorEvent>;
private statusChangeSource: Subject<NgcStatusChangeEvent>;
Expand All @@ -87,7 +87,7 @@ export class NgcCookieConsentService {
/**
* Observable to subscribe to and get notified when Cookie Consent is initializing.
*/
initialize$: Observable<NgcInitializeEvent>;
initializing$: Observable<NgcInitializingEvent>;
/**
* Observable to subscribe to and get notified when Cookie Consent has been successfully initialized.
*/
Expand All @@ -113,7 +113,7 @@ export class NgcCookieConsentService {
// Observable sources
this.popupOpenSource = new Subject<void>();
this.popupCloseSource = new Subject<void>();
this.initializeSource = new Subject<NgcInitializeEvent>();
this.initializingSource = new Subject<NgcInitializingEvent>();
this.initializedSource = new Subject<void>();
this.initializationErrorSource = new Subject<NgcInitializationErrorEvent>();
this.statusChangeSource = new Subject<NgcStatusChangeEvent>();
Expand All @@ -123,7 +123,7 @@ export class NgcCookieConsentService {
// Observable streams
this.popupOpen$ = this.popupOpenSource.asObservable();
this.popupClose$ = this.popupCloseSource.asObservable();
this.initialize$ = this.initializeSource.asObservable();
this.initializing$ = this.initializingSource.asObservable();
this.initialized$ = this.initializedSource.asObservable();
this.initializationError$ = this.initializationErrorSource.asObservable();
this.statusChange$ = this.statusChangeSource.asObservable();
Expand Down Expand Up @@ -158,7 +158,7 @@ export class NgcCookieConsentService {
() => this.popupCloseSource.next();

this.config.onInitialise =
(status: 'allow' | 'deny' | 'dismiss') => this.initializeSource.next({ status: status });
(status: 'allow' | 'deny' | 'dismiss') => this.initializingSource.next({ status: status });

this.config.onStatusChange =
(status: 'allow' | 'deny' | 'dismiss', chosenBefore: boolean) => {
Expand Down

0 comments on commit 7233aa0

Please sign in to comment.