mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-09-10 14:11:42 +00:00
AAE-30878 Migrating from event-emitter to eventemitter3 which is … (#11116)
This commit is contained in:
@@ -21,6 +21,7 @@ import { HttpClientTestingModule, HttpTestingController } from '@angular/common/
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { AdfHttpClient } from './adf-http-client.service';
|
||||
import { AlfrescoApiResponseError } from './alfresco-api/alfresco-api.response-error';
|
||||
import EventEmitter from 'eventemitter3';
|
||||
|
||||
const securityOptions: SecurityOptions = {
|
||||
authentications: {},
|
||||
@@ -30,16 +31,9 @@ const securityOptions: SecurityOptions = {
|
||||
withCredentials: false
|
||||
};
|
||||
|
||||
const emitter = {
|
||||
emit: () => {},
|
||||
off: () => {},
|
||||
on: () => {},
|
||||
once: () => {}
|
||||
};
|
||||
|
||||
const emitters: Emitters = {
|
||||
eventEmitter: emitter,
|
||||
apiClientEmitter: emitter
|
||||
eventEmitter: new EventEmitter(),
|
||||
apiClientEmitter: new EventEmitter()
|
||||
};
|
||||
|
||||
const mockResponse = {
|
||||
@@ -124,10 +118,10 @@ describe('AdfHttpClient', () => {
|
||||
httpMethod: 'POST'
|
||||
};
|
||||
|
||||
const spy = spyOn(emitter, 'emit').and.callThrough();
|
||||
const eventSpy = spyOn(emitters.eventEmitter, 'emit').and.callThrough();
|
||||
|
||||
angularHttpClient.request('http://example.com', options, securityOptions, emitters).catch(() => {
|
||||
expect(spy).toHaveBeenCalledWith('unauthorized');
|
||||
expect(eventSpy).toHaveBeenCalledWith('unauthorized');
|
||||
done();
|
||||
});
|
||||
|
||||
|
@@ -34,23 +34,24 @@ import { AlfrescoApiParamEncoder } from './alfresco-api/alfresco-api.param-encod
|
||||
import { AlfrescoApiResponseError } from './alfresco-api/alfresco-api.response-error';
|
||||
import { Constructor } from './types';
|
||||
import { RequestOptions, SecurityOptions } from './interfaces';
|
||||
import ee, { Emitter } from 'event-emitter';
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
|
||||
type EventEmitterInstance = InstanceType<typeof EventEmitter>;
|
||||
|
||||
type EventEmitterEvents = 'progress' | 'success' | 'error' | 'forbidden' | 'abort' | 'unauthorized' | string;
|
||||
|
||||
export interface Emitters {
|
||||
readonly eventEmitter: Emitter;
|
||||
readonly apiClientEmitter: Emitter;
|
||||
readonly eventEmitter: EventEmitterInstance;
|
||||
readonly apiClientEmitter: EventEmitterInstance;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AdfHttpClient implements ee.Emitter, JsApiHttpClient {
|
||||
on: ee.EmitterMethod;
|
||||
off: ee.EmitterMethod;
|
||||
once: ee.EmitterMethod;
|
||||
_disableCsrf: boolean;
|
||||
export class AdfHttpClient implements JsApiHttpClient {
|
||||
private eventEmitter = new EventEmitter();
|
||||
|
||||
emit: (type: string, ...args: any[]) => void;
|
||||
_disableCsrf: boolean;
|
||||
|
||||
get disableCsrf(): boolean {
|
||||
return this._disableCsrf;
|
||||
@@ -68,7 +69,27 @@ export class AdfHttpClient implements ee.Emitter, JsApiHttpClient {
|
||||
};
|
||||
|
||||
constructor(private httpClient: HttpClient) {
|
||||
ee(this);
|
||||
// No need for ee(this) anymore - we use composition instead of inheritance
|
||||
}
|
||||
|
||||
// EventEmitter delegation methods
|
||||
on(event: EventEmitterEvents, fn: (...args: any[]) => void, context?: any): this {
|
||||
this.eventEmitter.on(event, fn, context);
|
||||
return this;
|
||||
}
|
||||
|
||||
off(event: EventEmitterEvents, fn?: (...args: any[]) => void, context?: any): this {
|
||||
this.eventEmitter.off(event, fn, context);
|
||||
return this;
|
||||
}
|
||||
|
||||
once(event: EventEmitterEvents, fn: (...args: any[]) => void, context?: any): this {
|
||||
this.eventEmitter.once(event, fn, context);
|
||||
return this;
|
||||
}
|
||||
|
||||
emit(event: EventEmitterEvents, ...args: any[]): boolean {
|
||||
return this.eventEmitter.emit(event, ...args);
|
||||
}
|
||||
|
||||
setDefaultSecurityOption(options: any) {
|
||||
@@ -136,24 +157,19 @@ export class AdfHttpClient implements ee.Emitter, JsApiHttpClient {
|
||||
|
||||
private addPromiseListeners<T = any>(promise: Promise<T>, eventEmitter: any) {
|
||||
const eventPromise = Object.assign(promise, {
|
||||
on() {
|
||||
// eslint-disable-next-line prefer-spread, prefer-rest-params
|
||||
eventEmitter.on.apply(eventEmitter, arguments);
|
||||
on<K extends string | symbol>(event: K, fn: (...args: any[]) => void, context?: any) {
|
||||
eventEmitter.on(event, fn, context);
|
||||
return this;
|
||||
},
|
||||
once() {
|
||||
// eslint-disable-next-line prefer-spread, prefer-rest-params
|
||||
eventEmitter.once.apply(eventEmitter, arguments);
|
||||
once<K extends string | symbol>(event: K, fn: (...args: any[]) => void, context?: any) {
|
||||
eventEmitter.once(event, fn, context);
|
||||
return this;
|
||||
},
|
||||
emit() {
|
||||
// eslint-disable-next-line prefer-spread, prefer-rest-params
|
||||
eventEmitter.emit.apply(eventEmitter, arguments);
|
||||
return this;
|
||||
emit<K extends string | symbol>(event: K, ...args: any[]): boolean {
|
||||
return eventEmitter.emit(event, ...args);
|
||||
},
|
||||
off() {
|
||||
// eslint-disable-next-line prefer-spread, prefer-rest-params
|
||||
eventEmitter.off.apply(eventEmitter, arguments);
|
||||
off<K extends string | symbol>(event: K, fn?: (...args: any[]) => void, context?: any) {
|
||||
eventEmitter.off(event, fn, context);
|
||||
return this;
|
||||
}
|
||||
});
|
||||
@@ -162,16 +178,17 @@ export class AdfHttpClient implements ee.Emitter, JsApiHttpClient {
|
||||
}
|
||||
|
||||
private getEventEmitters(): Emitters {
|
||||
const apiClientEmitter = {
|
||||
on: this.on.bind(this),
|
||||
off: this.off.bind(this),
|
||||
once: this.once.bind(this),
|
||||
emit: this.emit.bind(this)
|
||||
};
|
||||
const apiClientEmitter: EventEmitterInstance = new EventEmitter();
|
||||
|
||||
// Bind this instance's methods to the apiClientEmitter for backward compatibility
|
||||
apiClientEmitter.on = this.on.bind(this);
|
||||
apiClientEmitter.off = this.off.bind(this);
|
||||
apiClientEmitter.once = this.once.bind(this);
|
||||
apiClientEmitter.emit = this.emit.bind(this);
|
||||
|
||||
return {
|
||||
apiClientEmitter,
|
||||
eventEmitter: ee({})
|
||||
eventEmitter: new EventEmitter()
|
||||
};
|
||||
}
|
||||
|
||||
|
@@ -16,18 +16,20 @@
|
||||
*/
|
||||
|
||||
import { HttpHeaders } from '@angular/common/http';
|
||||
import ee from 'event-emitter';
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
type EventEmitterInstance = InstanceType<typeof EventEmitter>;
|
||||
|
||||
export interface AuthenticationServiceInterface {
|
||||
onError: any;
|
||||
onLogin: any;
|
||||
onLogout: any;
|
||||
|
||||
on: ee.EmitterMethod;
|
||||
off: ee.EmitterMethod;
|
||||
once: ee.EmitterMethod;
|
||||
emit: (type: string, ...args: any[]) => void;
|
||||
on: EventEmitterInstance['on'];
|
||||
off: EventEmitterInstance['off'];
|
||||
once: EventEmitterInstance['once'];
|
||||
emit: EventEmitterInstance['emit'];
|
||||
|
||||
getToken(): string;
|
||||
isLoggedIn(): boolean;
|
||||
|
@@ -21,13 +21,13 @@ import { BasicAlfrescoAuthService } from '../basic-auth/basic-alfresco-auth.serv
|
||||
import { Observable, Subject, from } from 'rxjs';
|
||||
import { HttpHeaders } from '@angular/common/http';
|
||||
import { AuthenticationServiceInterface } from '../interfaces/authentication-service.interface';
|
||||
import ee from 'event-emitter';
|
||||
import { RedirectAuthService } from '../oidc/redirect-auth.service';
|
||||
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
type EventEmitterInstance = InstanceType<typeof EventEmitter>;
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthenticationService implements AuthenticationServiceInterface, ee.Emitter {
|
||||
export class AuthenticationService implements AuthenticationServiceInterface {
|
||||
onLogin: Subject<any> = new Subject<any>();
|
||||
onLogout: Subject<any> = new Subject<any>();
|
||||
onTokenReceived: Subject<any> = new Subject<any>();
|
||||
@@ -49,19 +49,19 @@ export class AuthenticationService implements AuthenticationServiceInterface, ee
|
||||
}
|
||||
}
|
||||
|
||||
get on(): ee.EmitterMethod {
|
||||
get on(): EventEmitterInstance['on'] {
|
||||
return this.isOauth() ? this.oidcAuthenticationService.on : this.basicAlfrescoAuthService.on;
|
||||
}
|
||||
|
||||
get off(): ee.EmitterMethod {
|
||||
get off(): EventEmitterInstance['off'] {
|
||||
return this.isOauth() ? this.oidcAuthenticationService.off : this.basicAlfrescoAuthService.off;
|
||||
}
|
||||
|
||||
get once(): ee.EmitterMethod {
|
||||
get once(): EventEmitterInstance['once'] {
|
||||
return this.isOauth() ? this.oidcAuthenticationService.once : this.basicAlfrescoAuthService.once;
|
||||
}
|
||||
|
||||
get emit(): (type: string, ...args: any[]) => void {
|
||||
get emit(): EventEmitterInstance['emit'] {
|
||||
return this.isOauth() ? this.oidcAuthenticationService.emit : this.basicAlfrescoAuthService.emit;
|
||||
}
|
||||
|
||||
|
@@ -21,13 +21,15 @@ import { Observable, Observer, ReplaySubject, throwError } from 'rxjs';
|
||||
import { AppConfigService, AppConfigValues } from '../../app-config/app-config.service';
|
||||
import { CookieService } from '../../common/services/cookie.service';
|
||||
import { AuthenticationServiceInterface } from '../interfaces/authentication-service.interface';
|
||||
import ee from 'event-emitter';
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
|
||||
export abstract class BaseAuthenticationService implements AuthenticationServiceInterface, ee.Emitter {
|
||||
on: ee.EmitterMethod;
|
||||
off: ee.EmitterMethod;
|
||||
once: ee.EmitterMethod;
|
||||
emit: (type: string, ...args: any[]) => void;
|
||||
type EventEmitterInstance = InstanceType<typeof EventEmitter>;
|
||||
|
||||
export abstract class BaseAuthenticationService implements AuthenticationServiceInterface {
|
||||
on: EventEmitterInstance['on'];
|
||||
off: EventEmitterInstance['off'];
|
||||
once: EventEmitterInstance['once'];
|
||||
emit: EventEmitterInstance['emit'];
|
||||
|
||||
protected redirectUrl: RedirectionModel = null;
|
||||
|
||||
@@ -38,9 +40,7 @@ export abstract class BaseAuthenticationService implements AuthenticationService
|
||||
protected constructor(
|
||||
protected appConfig: AppConfigService,
|
||||
protected cookie: CookieService
|
||||
) {
|
||||
ee(this);
|
||||
}
|
||||
) {}
|
||||
|
||||
abstract getAuthHeaders(requestUrl: string, header: HttpHeaders): HttpHeaders;
|
||||
abstract getToken(): string;
|
||||
|
Reference in New Issue
Block a user