AAE-30878 Migrating from event-emitter to eventemitter3 (#11187)

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

* [AAE-30878] - have a single place for the EventEmitter types
This commit is contained in:
Vito Albano
2025-09-16 09:33:06 +01:00
committed by GitHub
parent 10afe75e94
commit b400757ad1
18 changed files with 186 additions and 269 deletions

View File

@@ -43,3 +43,4 @@ export * from './src/api-clients/api-client';
export * from './src/api-clients/http-client.interface';
export * from './src/utils';
export * from './src/constants';
export * from './src/types';

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,11 @@ 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';
import { EventEmitterInstance } from './types';
export class AlfrescoApi implements Emitter, AlfrescoApiType {
export class AlfrescoApi extends AlfrescoApiClient implements AlfrescoApiType {
__type = 'legacy-client';
storage: Storage;
config: AlfrescoApiConfig;
contentClient: ContentClient;
contentPrivateClient: ContentClient;
processClient: ProcessClient;
@@ -43,21 +43,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);
@@ -395,14 +385,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) => {
@@ -410,16 +401,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);
}
/**
@@ -452,29 +442,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,21 +15,22 @@
* 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';
import { Emitters, HttpClient, LegacyHttpClient, RequestOptions, SecurityOptions } from './api-clients/http-client.interface';
import { paramToString } from './utils';
import { Storage } from './storage';
import { EventEmitterInstance } from './types';
declare const Buffer: any;
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 +63,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 +103,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 +339,7 @@ export class AlfrescoApiClient implements ee.Emitter, LegacyHttpClient {
return {
apiClientEmitter,
eventEmitter: ee({})
eventEmitter: new EventEmitter()
};
}
@@ -374,27 +388,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

@@ -15,8 +15,8 @@
* limitations under the License.
*/
import { EventEmitterInstance } from './../types';
import { Authentication } from '../authentication/authentication';
import { Emitter } from 'event-emitter';
export interface RequestOptions {
path: string;
@@ -93,8 +93,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

@@ -43,3 +43,4 @@ export * from './api-clients/api-client';
export * from './api-clients/http-client.interface';
export * from './utils';
export * from './constants';
export * from './types';

View File

@@ -15,13 +15,13 @@
* limitations under the License.
*/
import ee, { Emitter } from 'event-emitter';
import superagent, { Response, SuperAgentRequest } from 'superagent';
import { Authentication } from './authentication/authentication';
import { RequestOptions, HttpClient, SecurityOptions, Emitters } from './api-clients/http-client.interface';
import { Oauth2 } from './authentication/oauth2';
import { BasicAuth } from './authentication/basicAuth';
import { isBrowser, paramToString } from './utils';
import { EventEmitterInstance } from './types';
declare const Blob: any;
declare const Buffer: any;
@@ -134,7 +134,7 @@ export class SuperagentHttpClient implements HttpClient {
contentType: string,
accept: string,
responseType: string,
eventEmitter: ee.Emitter,
eventEmitter: EventEmitterInstance,
returnType: string,
securityOptions: SecurityOptions
) {
@@ -268,7 +268,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);

21
lib/js-api/src/types.ts Normal file
View File

@@ -0,0 +1,21 @@
/*!
* @license
* Copyright © 2005-2025 Hyland Software, Inc. and its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { EventEmitter } from 'eventemitter3';
export type EventEmitterInstance = InstanceType<typeof EventEmitter>;
export type EventEmitterEvents = 'progress' | 'success' | 'error' | 'forbidden' | 'abort' | 'unauthorized' | string;