AAE-30878 Migrating from event-emitter to eventemitter3 which is … (#11116)

This commit is contained in:
Vito Albano
2025-09-02 13:33:35 +01:00
committed by GitHub
parent aae4efdd92
commit 49375181e4
15 changed files with 174 additions and 264 deletions

View File

@@ -15,7 +15,7 @@
"url": "https://github.com/Alfresco/alfresco-ng2-components/issues"
},
"dependencies": {
"event-emitter": "^0.3.5",
"eventemitter3": "^5.0.1",
"superagent": "^9.0.1",
"tslib": "^2.6.1"
},

View File

@@ -15,7 +15,7 @@
* limitations under the License.
*/
import ee, { EmitterMethod, Emitter } from 'event-emitter';
import { EventEmitter } from 'eventemitter3';
import { ContentAuth } from './authentication/contentAuth';
import { ProcessAuth } from './authentication/processAuth';
import { Oauth2Auth } from './authentication/oauth2Auth';
@@ -26,11 +26,12 @@ import { AlfrescoApiConfig } from './alfrescoApiConfig';
import { Authentication } from './authentication/authentication';
import { AlfrescoApiType } from './to-deprecate/alfresco-api-type';
import { HttpClient } from './api-clients/http-client.interface';
import { AlfrescoApiClient, AlfrescoApiClientPromise } from './alfrescoApiClient';
export class AlfrescoApi implements Emitter, AlfrescoApiType {
type EventEmitterInstance = InstanceType<typeof EventEmitter>;
export class AlfrescoApi extends AlfrescoApiClient implements AlfrescoApiType {
__type = 'legacy-client';
storage: Storage;
config: AlfrescoApiConfig;
contentClient: ContentClient;
contentPrivateClient: ContentClient;
processClient: ProcessClient;
@@ -43,18 +44,11 @@ export class AlfrescoApi implements Emitter, AlfrescoApiType {
processAuth: ProcessAuth;
contentAuth: ContentAuth;
on: EmitterMethod;
off: EmitterMethod;
once: EmitterMethod;
bufferEvents: string[] = [];
emit: (type: string, ...args: any[]) => void;
username: string;
constructor(config?: AlfrescoApiConfig, public httpClient?: HttpClient) {
ee(this);
constructor(config?: AlfrescoApiConfig, httpClient?: HttpClient) {
super(undefined, httpClient);
if (config) {
this.setConfig(config);
@@ -392,14 +386,15 @@ export class AlfrescoApi implements Emitter, AlfrescoApiType {
return this.contentAuth.validateTicket();
}
private loginBPMECM(username: string, password: string): Promise<[string, string]> {
private loginBPMECM(username: string, password: string): AlfrescoApiClientPromise<[string, string]> {
const contentPromise = this.contentAuth.login(username, password);
const processPromise = this.processAuth.login(username, password);
const promise: any = new Promise<[string, string]>((resolve, reject) => {
const eventEmitter: EventEmitterInstance = new EventEmitter();
const promise = new Promise<[string, string]>((resolve, reject) => {
Promise.all([contentPromise, processPromise]).then(
(data) => {
promise.emit('success');
eventEmitter.emit('success');
resolve(data);
},
(error) => {
@@ -407,16 +402,15 @@ export class AlfrescoApi implements Emitter, AlfrescoApiType {
this.processAuth.invalidateSession();
if (error.status === 401) {
promise.emit('unauthorized');
eventEmitter.emit('unauthorized');
}
promise.emit('error');
eventEmitter.emit('error');
reject(error);
}
);
});
ee(promise); // jshint ignore:line
return promise;
return this.addPromiseListeners(promise, eventEmitter);
}
/**
@@ -449,29 +443,29 @@ export class AlfrescoApi implements Emitter, AlfrescoApiType {
return Promise.resolve();
}
private _logoutBPMECM(): Promise<void> {
private _logoutBPMECM(): AlfrescoApiClientPromise<void> {
const contentPromise = this.contentAuth.logout();
const processPromise = this.processAuth.logout();
const promise: any = new Promise<void>((resolve, reject) => {
const eventEmitter: EventEmitterInstance = new EventEmitter();
const promise = new Promise<void>((resolve, reject) => {
Promise.all([contentPromise, processPromise]).then(
() => {
this.config.ticket = undefined;
promise.emit('logout');
eventEmitter.emit('logout');
resolve();
},
(error) => {
if (error.status === 401) {
promise.emit('unauthorized');
eventEmitter.emit('unauthorized');
}
promise.emit('error');
eventEmitter.emit('error');
reject(error);
}
);
});
ee(promise); // jshint ignore:line
return promise;
return this.addPromiseListeners(promise, eventEmitter);
}
/**

View File

@@ -15,7 +15,7 @@
* limitations under the License.
*/
import ee from 'event-emitter';
import { EventEmitter } from 'eventemitter3';
import { AlfrescoApiConfig } from './alfrescoApiConfig';
import { Authentication } from './authentication/authentication';
import { SuperagentHttpClient } from './superagentHttpClient';
@@ -25,11 +25,13 @@ import { Storage } from './storage';
declare const Buffer: any;
type EventEmitterInstance = InstanceType<typeof EventEmitter>;
export type AlfrescoApiClientPromise<T = any> = Promise<T> & {
on: ee.EmitterMethod;
off: ee.EmitterMethod;
once: ee.EmitterMethod;
emit: (type: string, ...args: any[]) => void;
on: <K extends string | symbol>(event: K, fn: (...args: any[]) => void, context?: any) => AlfrescoApiClientPromise<T>;
off: <K extends string | symbol>(event: K, fn?: (...args: any[]) => void, context?: any) => AlfrescoApiClientPromise<T>;
once: <K extends string | symbol>(event: K, fn: (...args: any[]) => void, context?: any) => AlfrescoApiClientPromise<T>;
emit: <K extends string | symbol>(event: K, ...args: any[]) => boolean;
abort?: () => void;
};
@@ -62,11 +64,8 @@ export function buildCollectionParam(param: string[], collectionFormat: string):
}
}
export class AlfrescoApiClient implements ee.Emitter, LegacyHttpClient {
on: ee.EmitterMethod;
off: ee.EmitterMethod;
once: ee.EmitterMethod;
emit: (type: string, ...args: any[]) => void;
export class AlfrescoApiClient implements LegacyHttpClient {
private eventEmitter = new EventEmitter();
storage: Storage;
host: string;
@@ -105,13 +104,29 @@ export class AlfrescoApiClient implements ee.Emitter, LegacyHttpClient {
constructor(host?: string, httpClient?: HttpClient) {
this.host = host;
this.storage = Storage.getInstance();
// fallback for backward compatibility
this.httpClient = httpClient || new SuperagentHttpClient();
}
ee(this);
// EventEmitter delegation methods
on<K extends string | symbol>(event: K, fn: (...args: any[]) => void, context?: any): this {
this.eventEmitter.on(event, fn, context);
return this;
}
off<K extends string | symbol>(event: K, fn?: (...args: any[]) => void, context?: any): this {
this.eventEmitter.off(event, fn, context);
return this;
}
once<K extends string | symbol>(event: K, fn: (...args: any[]) => void, context?: any): this {
this.eventEmitter.once(event, fn, context);
return this;
}
emit<K extends string | symbol>(event: K, ...args: any[]): boolean {
return this.eventEmitter.emit(event, ...args);
}
request<T = any>(options: RequestOptions): Promise<T> {
@@ -325,7 +340,7 @@ export class AlfrescoApiClient implements ee.Emitter, LegacyHttpClient {
return {
apiClientEmitter,
eventEmitter: ee({})
eventEmitter: new EventEmitter()
};
}
@@ -374,27 +389,22 @@ export class AlfrescoApiClient implements ee.Emitter, LegacyHttpClient {
return Boolean(contentType?.match(/^application\/json(;.*)?$/i));
}
private addPromiseListeners<T = any>(promise: Promise<T>, eventEmitter: ee.Emitter): AlfrescoApiClientPromise<T> {
addPromiseListeners<T = any>(promise: Promise<T>, eventEmitter: EventEmitterInstance): AlfrescoApiClientPromise<T> {
return Object.assign(promise, {
on() {
// eslint-disable-next-line prefer-spread,prefer-rest-params
eventEmitter.on.apply(eventEmitter, arguments);
return this;
on<K extends string | symbol>(event: K, fn: (...args: any[]) => void, context?: any): AlfrescoApiClientPromise<T> {
eventEmitter.on(event, fn, context);
return this as AlfrescoApiClientPromise<T>;
},
once() {
// eslint-disable-next-line prefer-spread,prefer-rest-params
eventEmitter.once.apply(eventEmitter, arguments);
return this;
once<K extends string | symbol>(event: K, fn: (...args: any[]) => void, context?: any): AlfrescoApiClientPromise<T> {
eventEmitter.once(event, fn, context);
return this as AlfrescoApiClientPromise<T>;
},
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);
return this;
off<K extends string | symbol>(event: K, fn?: (...args: any[]) => void, context?: any): AlfrescoApiClientPromise<T> {
eventEmitter.off(event, fn, context);
return this as AlfrescoApiClientPromise<T>;
}
});
}

View File

@@ -16,7 +16,9 @@
*/
import { Authentication } from '../authentication/authentication';
import { Emitter } from 'event-emitter';
import { EventEmitter } from 'eventemitter3';
type EventEmitterInstance = InstanceType<typeof EventEmitter>;
export interface RequestOptions {
path: string;
@@ -93,8 +95,13 @@ export interface SecurityOptions {
}
export interface Emitters {
readonly eventEmitter: Emitter;
readonly apiClientEmitter: Emitter;
readonly eventEmitter: EventEmitterInstance;
readonly apiClientEmitter: {
on: EventEmitterInstance['on'];
off: EventEmitterInstance['off'];
once: EventEmitterInstance['once'];
emit: EventEmitterInstance['emit'];
};
}
export interface HttpClient {

View File

@@ -15,7 +15,7 @@
* limitations under the License.
*/
import ee from 'event-emitter';
import { EventEmitter } from 'eventemitter3';
import { AuthenticationApi, TicketBody } from '../api/auth-rest-api';
import { AlfrescoApiClient } from '../alfrescoApiClient';
import { AlfrescoApiConfig } from '../alfrescoApiConfig';
@@ -104,8 +104,7 @@ export class ContentAuth extends AlfrescoApiClient {
});
});
ee(promise); // jshint ignore:line
return promise;
return this.addPromiseListeners(promise, new EventEmitter());
}
/**
@@ -133,8 +132,7 @@ export class ContentAuth extends AlfrescoApiClient {
);
});
ee(promise); // jshint ignore:line
return promise;
return this.addPromiseListeners(promise, new EventEmitter());
}
/**
@@ -160,8 +158,7 @@ export class ContentAuth extends AlfrescoApiClient {
);
});
ee(promise); // jshint ignore:line
return promise;
return this.addPromiseListeners(promise, new EventEmitter());
}
/**

View File

@@ -15,7 +15,7 @@
* limitations under the License.
*/
import ee from 'event-emitter';
import { EventEmitter } from 'eventemitter3';
import { AlfrescoApiClient } from '../alfrescoApiClient';
import { AlfrescoApiConfig } from '../alfrescoApiConfig';
import { Authentication } from './authentication';
@@ -604,7 +604,7 @@ export class Oauth2Auth extends AlfrescoApiClient {
}
);
ee(promise); // jshint ignore:line
return this.addPromiseListeners(promise, new EventEmitter());
}
pollingRefreshToken() {
@@ -654,9 +654,7 @@ export class Oauth2Auth extends AlfrescoApiClient {
);
});
ee(promise); // jshint ignore:line
return promise;
return this.addPromiseListeners(promise, new EventEmitter());
}
universalBtoa(stringToConvert: string) {

View File

@@ -15,7 +15,7 @@
* limitations under the License.
*/
import ee from 'event-emitter';
import { EventEmitter } from 'eventemitter3';
import { AlfrescoApiClient, AlfrescoApiClientPromise } from '../alfrescoApiClient';
import { AlfrescoApiConfig } from '../alfrescoApiConfig';
import { Authentication } from './authentication';
@@ -123,8 +123,7 @@ export class ProcessAuth extends AlfrescoApiClient {
);
});
ee(promise); // jshint ignore:line
return promise;
return this.addPromiseListeners(promise, new EventEmitter());
}
/**
@@ -154,8 +153,7 @@ export class ProcessAuth extends AlfrescoApiClient {
);
});
ee(promise);
return promise;
return this.addPromiseListeners(promise, new EventEmitter());
}
/**

View File

@@ -15,7 +15,7 @@
* limitations under the License.
*/
import ee, { Emitter } from 'event-emitter';
import { EventEmitter } from 'eventemitter3';
import superagent, { Response, SuperAgentRequest } from 'superagent';
import { Authentication } from './authentication/authentication';
import { RequestOptions, HttpClient, SecurityOptions, Emitters } from './api-clients/http-client.interface';
@@ -26,6 +26,8 @@ import { isBrowser, paramToString } from './utils';
declare const Blob: any;
declare const Buffer: any;
type EventEmitterInstance = InstanceType<typeof EventEmitter>;
const isProgressEvent = (event: ProgressEvent | unknown): event is ProgressEvent => (event as ProgressEvent)?.lengthComputable;
export class SuperagentHttpClient implements HttpClient {
@@ -134,7 +136,7 @@ export class SuperagentHttpClient implements HttpClient {
contentType: string,
accept: string,
responseType: string,
eventEmitter: ee.Emitter,
eventEmitter: EventEmitterInstance,
returnType: string,
securityOptions: SecurityOptions
) {
@@ -268,7 +270,7 @@ export class SuperagentHttpClient implements HttpClient {
}
}
private progress(event: ProgressEvent | unknown, eventEmitter: Emitter): void {
private progress(event: ProgressEvent | unknown, eventEmitter: EventEmitterInstance): void {
if (isProgressEvent(event)) {
const percent = Math.round((event.loaded / event.total) * 100);