feat: init and fork some code from angular-auth-oidc-client
Some checks are pending
Build, Lint & Test Lib / Built, Lint and Test Library (push) Waiting to run
Build, Lint & Test Lib / Angular latest (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest Standalone & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular 16 & RxJs 6 (push) Blocked by required conditions
Build, Lint & Test Lib / Angular V16 (push) Blocked by required conditions
Docs / Build and Deploy Docs Job (push) Waiting to run
Docs / Close Pull Request Job (push) Waiting to run

This commit is contained in:
2025-01-18 01:05:00 +08:00
parent 276d9fbda8
commit 983254164b
201 changed files with 35689 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
export enum EventTypes {
/**
* This only works in the AppModule Constructor
*/
ConfigLoaded,
CheckingAuth,
CheckingAuthFinished,
CheckingAuthFinishedWithError,
ConfigLoadingFailed,
CheckSessionReceived,
UserDataChanged,
NewAuthenticationResult,
TokenExpired,
IdTokenExpired,
SilentRenewStarted,
SilentRenewFailed,
}

View File

@@ -0,0 +1,6 @@
import { EventTypes } from './event-types';
export interface OidcClientNotification<T> {
type: EventTypes;
value?: T;
}

View File

@@ -0,0 +1,69 @@
import { TestBed, waitForAsync } from '@angular/core/testing';
import { filter } from 'rxjs/operators';
import { EventTypes } from './event-types';
import { PublicEventsService } from './public-events.service';
describe('Events Service', () => {
let eventsService: PublicEventsService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [PublicEventsService],
});
});
beforeEach(() => {
eventsService = TestBed.inject(PublicEventsService);
});
it('should create', () => {
expect(eventsService).toBeTruthy();
});
it('registering to single event with one event emit works', waitForAsync(() => {
eventsService.registerForEvents().subscribe((firedEvent) => {
expect(firedEvent).toBeTruthy();
expect(firedEvent).toEqual({
type: EventTypes.ConfigLoaded,
value: { myKey: 'myValue' },
});
});
eventsService.fireEvent(EventTypes.ConfigLoaded, { myKey: 'myValue' });
}));
it('registering to single event with multiple same event emit works', waitForAsync(() => {
const spy = jasmine.createSpy('spy');
eventsService.registerForEvents().subscribe((firedEvent) => {
spy(firedEvent);
expect(firedEvent).toBeTruthy();
});
eventsService.fireEvent(EventTypes.ConfigLoaded, { myKey: 'myValue' });
eventsService.fireEvent(EventTypes.ConfigLoaded, { myKey: 'myValue2' });
expect(spy.calls.count()).toBe(2);
expect(spy.calls.first().args[0]).toEqual({
type: EventTypes.ConfigLoaded,
value: { myKey: 'myValue' },
});
expect(spy.calls.mostRecent().args[0]).toEqual({
type: EventTypes.ConfigLoaded,
value: { myKey: 'myValue2' },
});
}));
it('registering to single event with multiple emit works', waitForAsync(() => {
eventsService
.registerForEvents()
.pipe(filter((x) => x.type === EventTypes.ConfigLoaded))
.subscribe((firedEvent) => {
expect(firedEvent).toBeTruthy();
expect(firedEvent).toEqual({
type: EventTypes.ConfigLoaded,
value: { myKey: 'myValue' },
});
});
eventsService.fireEvent(EventTypes.ConfigLoaded, { myKey: 'myValue' });
eventsService.fireEvent(EventTypes.NewAuthenticationResult, true);
}));
});

View File

@@ -0,0 +1,26 @@
import { Injectable } from 'injection-js';
import { Observable, ReplaySubject } from 'rxjs';
import { EventTypes } from './event-types';
import { OidcClientNotification } from './notification';
@Injectable()
export class PublicEventsService {
private readonly notify = new ReplaySubject<OidcClientNotification<any>>(1);
/**
* Fires a new event.
*
* @param type The event type.
* @param value The event value.
*/
fireEvent<T>(type: EventTypes, value?: T): void {
this.notify.next({ type, value });
}
/**
* Wires up the event notification observable.
*/
registerForEvents(): Observable<OidcClientNotification<any>> {
return this.notify.asObservable();
}
}