Skip to content

Commit

Permalink
chore: remove proxifyWithWait and waitAndCall
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego committed Oct 22, 2024
1 parent fd5e54f commit 5ccebc9
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 110 deletions.
6 changes: 3 additions & 3 deletions apps/meteor/ee/server/sdk/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { proxifyWithWait } from '@rocket.chat/core-services';
import { proxify } from '@rocket.chat/core-services';

import type { IInstanceService } from './types/IInstanceService';
import type { ILDAPEEService } from './types/ILDAPEEService';

export const LDAPEE = proxifyWithWait<ILDAPEEService>('ldap-enterprise');
export const Instance = proxifyWithWait<IInstanceService>('instance');
export const LDAPEE = proxify<ILDAPEEService>('ldap-enterprise');
export const Instance = proxify<IInstanceService>('instance');
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { IAuthorizationLivechat, RoomAccessValidator } from '@rocket.chat/core-services';
import { proxifyWithWait } from '@rocket.chat/core-services';
import { proxify } from '@rocket.chat/core-services';
import type { IOmnichannelRoom } from '@rocket.chat/core-typings';
import { Rooms } from '@rocket.chat/models';

const AuthorizationLivechat = proxifyWithWait<IAuthorizationLivechat>('authorization-livechat');
const AuthorizationLivechat = proxify<IAuthorizationLivechat>('authorization-livechat');

export const canAccessRoomLivechat: RoomAccessValidator = async (room, user, extraData): Promise<boolean> => {
// If we received a partial room and its type is not `l` or `v`, skip all checks.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { IAuthorizationVoip, RoomAccessValidator } from '@rocket.chat/core-services';
import { proxifyWithWait } from '@rocket.chat/core-services';
import { proxify } from '@rocket.chat/core-services';
import { Rooms } from '@rocket.chat/models';

const AuthorizationVoip = proxifyWithWait<IAuthorizationVoip>('authorization-livechat');
const AuthorizationVoip = proxify<IAuthorizationVoip>('authorization-livechat');

export const canAccessRoomVoip: RoomAccessValidator = async (room, user, extraData): Promise<boolean> => {
// room can be sent as `null` but in that case a `rid` is also sent on extraData
Expand Down
4 changes: 0 additions & 4 deletions apps/meteor/tests/mocks/server/BrokerMocked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ export class BrokerMocked {
return this.actions[method](data);
}

async waitAndCall(method: string, data: any): Promise<any> {
return this.actions[method](data);
}

async broadcastToServices(): Promise<void> {
// no op
}
Expand Down
30 changes: 0 additions & 30 deletions ee/packages/network-broker/src/NetworkBroker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ const lifecycle: { [k: string]: string } = {
stopped: 'stopped',
};

const {
WAIT_FOR_SERVICES_TIMEOUT = '10000', // 10 seconds
} = process.env;

const waitForServicesTimeout = parseInt(WAIT_FOR_SERVICES_TIMEOUT, 10) || 10000;

export class NetworkBroker implements IBroker {
private broker: ServiceBroker;

Expand Down Expand Up @@ -61,30 +55,6 @@ export class NetworkBroker implements IBroker {
});
}

async waitAndCall(method: string, data: any): Promise<any> {
if (!(await this.started)) {
return;
}

try {
await this.broker.waitForServices(method.split('.')[0], waitForServicesTimeout);
} catch (err) {
console.error(err);
throw new Error('Dependent services not available');
}

const context = asyncLocalStorage.getStore();
if (context?.ctx?.call) {
return context.ctx.call(method, data);
}

return this.broker.call(method, data, {
meta: {
optl: injectCurrentContext(),
},
});
}

async destroyService(instance: IServiceClass): Promise<void> {
const name = instance.getName();
if (!name) {
Expand Down
2 changes: 1 addition & 1 deletion ee/packages/omnichannel-services/src/QueueWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class QueueWorker extends ServiceClass implements IQueueWorkerService {
this.logger.info(`Processing queue item ${queueItem._id} for work`);
this.logger.info(`Queue item is trying to call ${queueItem.message.to}`);
try {
await api.waitAndCall(queueItem.message.to, [queueItem.message]);
await api.call(queueItem.message.to, [queueItem.message]);
this.logger.info(`Queue item ${queueItem._id} completed`);
return 'Completed' as const;
} catch (err: unknown) {
Expand Down
4 changes: 0 additions & 4 deletions packages/core-services/src/LocalBroker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ export class LocalBroker implements IBroker {
return result;
}

async waitAndCall(method: string, data: any): Promise<any> {
return this.call(method, data);
}

async destroyService(instance: ServiceClass): Promise<void> {
const namespace = instance.getName();

Expand Down
76 changes: 38 additions & 38 deletions packages/core-services/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { proxify, proxifyWithWait } from './lib/proxify';
import { proxify } from './lib/proxify';
import type { IAccount, ILoginResult } from './types/IAccount';
import type { IAnalyticsService } from './types/IAnalyticsService';
import { IApiService } from './types/IApiService';
Expand Down Expand Up @@ -124,7 +124,7 @@ export {
IVoipFreeSwitchService,
NPSCreatePayload,
NPSVotePayload,
proxifyWithWait,
proxify,
ResizeResult,
RoomAccessValidator,
TelemetryEvents,
Expand Down Expand Up @@ -155,42 +155,42 @@ export const dbWatchersDisabled =
(process.env.NODE_ENV !== 'production' && !['no', 'false'].includes(disabledEnvVar));

// TODO think in a way to not have to pass the service name to proxify here as well
export const Authorization = proxifyWithWait<IAuthorization>('authorization');
export const Apps = proxifyWithWait<IAppsEngineService>('apps-engine');
export const Presence = proxifyWithWait<IPresence>('presence');
export const Account = proxifyWithWait<IAccount>('accounts');
export const License = proxifyWithWait<ILicense>('license');
export const MeteorService = proxifyWithWait<IMeteor>('meteor');
export const Banner = proxifyWithWait<IBannerService>('banner');
export const UiKitCoreApp = proxifyWithWait<IUiKitCoreAppService>('uikit-core-app');
export const NPS = proxifyWithWait<INPSService>('nps');
export const Team = proxifyWithWait<ITeamService>('team');
export const MessageReads = proxifyWithWait<IMessageReadsService>('message-reads');
export const Room = proxifyWithWait<IRoomService>('room');
export const Media = proxifyWithWait<IMediaService>('media');
export const VoipAsterisk = proxifyWithWait<IVoipService>('voip-asterisk');
export const VoipFreeSwitch = proxifyWithWait<IVoipFreeSwitchService>('voip-freeswitch');
export const LivechatVoip = proxifyWithWait<IOmnichannelVoipService>('omnichannel-voip');
export const Analytics = proxifyWithWait<IAnalyticsService>('analytics');
export const LDAP = proxifyWithWait<ILDAPService>('ldap');
export const SAUMonitor = proxifyWithWait<ISAUMonitorService>('sau-monitor');
export const DeviceManagement = proxifyWithWait<IDeviceManagementService>('device-management');
export const VideoConf = proxifyWithWait<IVideoConfService>('video-conference');
export const Upload = proxifyWithWait<IUploadService>('upload');
export const Calendar = proxifyWithWait<ICalendarService>('calendar');
export const QueueWorker = proxifyWithWait<IQueueWorkerService>('queue-worker');
export const OmnichannelTranscript = proxifyWithWait<IOmnichannelTranscriptService>('omnichannel-transcript');
export const Message = proxifyWithWait<IMessageService>('message');
export const Translation = proxifyWithWait<ITranslationService>('translation');
export const Settings = proxifyWithWait<ISettingsService>('settings');
export const OmnichannelIntegration = proxifyWithWait<IOmnichannelIntegrationService>('omnichannel-integration');
export const Federation = proxifyWithWait<IFederationService>('federation');
export const FederationEE = proxifyWithWait<IFederationServiceEE>('federation-enterprise');
export const Omnichannel = proxifyWithWait<IOmnichannelService>('omnichannel');
export const OmnichannelEEService = proxifyWithWait<IOmnichannelEEService>('omnichannel-ee');
export const Import = proxifyWithWait<IImportService>('import');
export const OmnichannelAnalytics = proxifyWithWait<IOmnichannelAnalyticsService>('omnichannel-analytics');
export const User = proxifyWithWait<IUserService>('user');
export const Authorization = proxify<IAuthorization>('authorization');
export const Apps = proxify<IAppsEngineService>('apps-engine');
export const Presence = proxify<IPresence>('presence');
export const Account = proxify<IAccount>('accounts');
export const License = proxify<ILicense>('license');
export const MeteorService = proxify<IMeteor>('meteor');
export const Banner = proxify<IBannerService>('banner');
export const UiKitCoreApp = proxify<IUiKitCoreAppService>('uikit-core-app');
export const NPS = proxify<INPSService>('nps');
export const Team = proxify<ITeamService>('team');
export const MessageReads = proxify<IMessageReadsService>('message-reads');
export const Room = proxify<IRoomService>('room');
export const Media = proxify<IMediaService>('media');
export const VoipAsterisk = proxify<IVoipService>('voip-asterisk');
export const VoipFreeSwitch = proxify<IVoipFreeSwitchService>('voip-freeswitch');
export const LivechatVoip = proxify<IOmnichannelVoipService>('omnichannel-voip');
export const Analytics = proxify<IAnalyticsService>('analytics');
export const LDAP = proxify<ILDAPService>('ldap');
export const SAUMonitor = proxify<ISAUMonitorService>('sau-monitor');
export const DeviceManagement = proxify<IDeviceManagementService>('device-management');
export const VideoConf = proxify<IVideoConfService>('video-conference');
export const Upload = proxify<IUploadService>('upload');
export const Calendar = proxify<ICalendarService>('calendar');
export const QueueWorker = proxify<IQueueWorkerService>('queue-worker');
export const OmnichannelTranscript = proxify<IOmnichannelTranscriptService>('omnichannel-transcript');
export const Message = proxify<IMessageService>('message');
export const Translation = proxify<ITranslationService>('translation');
export const Settings = proxify<ISettingsService>('settings');
export const OmnichannelIntegration = proxify<IOmnichannelIntegrationService>('omnichannel-integration');
export const Federation = proxify<IFederationService>('federation');
export const FederationEE = proxify<IFederationServiceEE>('federation-enterprise');
export const Omnichannel = proxify<IOmnichannelService>('omnichannel');
export const OmnichannelEEService = proxify<IOmnichannelEEService>('omnichannel-ee');
export const Import = proxify<IImportService>('import');
export const OmnichannelAnalytics = proxify<IOmnichannelAnalyticsService>('omnichannel-analytics');
export const User = proxify<IUserService>('user');

// Calls without wait. Means that the service is optional and the result may be an error
// of service/method not available
Expand Down
4 changes: 0 additions & 4 deletions packages/core-services/src/lib/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ export class Api implements IApiService {
return this.broker?.call(method, data);
}

async waitAndCall(method: string, data: any): Promise<any> {
return this.broker?.waitAndCall(method, data);
}

async broadcast<T extends keyof EventSignatures>(event: T, ...args: Parameters<EventSignatures[T]>): Promise<void> {
if (!this.broker) {
throw new Error(`No broker set to broadcast: ${event}, ${JSON.stringify(args)}`);
Expand Down
25 changes: 6 additions & 19 deletions packages/core-services/src/lib/proxify.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
import { api } from '../api';

type Prom<T> = {
[K in keyof T as T[K] extends (...params: any) => any ? K : never]: T[K] extends (...params: any) => Promise<any>
type Promisify<T> = {
[K in keyof T as T[K] extends (...params: any[]) => unknown ? K : never]: T[K] extends (...params: any[]) => Promise<any>
? T[K]
: T[K] extends (...params: infer P) => infer R
? (...params: P) => Promise<R>
: never;
};

type PromOrError<T> = {
[K in keyof T as T[K] extends (...params: any) => any ? K : never]: T[K] extends (...params: any) => Promise<any>
? T[K]
: T[K] extends (...params: infer P) => infer R
? (...params: P) => Promise<R | Error>
: never;
};

function handler<T extends object>(namespace: string, waitService: boolean): ProxyHandler<T> {
function handler<T extends object>(namespace: string): ProxyHandler<T> {
return {
get:
(_target: T, prop: string): any =>
(...params: any): Promise<any> =>
api[waitService ? 'waitAndCall' : 'call'](`${namespace}.${prop}`, params),
api.call(`${namespace}.${prop}`, params),
};
}

// TODO remove the need to wait for a service, if that is really needed it should have a dependency on startup
export function proxifyWithWait<T>(namespace: string): Prom<T> {
return new Proxy({}, handler(namespace, true)) as unknown as Prom<T>;
}

export function proxify<T>(namespace: string): PromOrError<T> {
return new Proxy({}, handler(namespace, false)) as unknown as PromOrError<T>;
export function proxify<T>(namespace: string): Promisify<T> {
return new Proxy({}, handler(namespace)) as unknown as Promisify<T>;
}
2 changes: 0 additions & 2 deletions packages/core-services/src/types/IApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ export interface IApiService {

call(method: string, data?: unknown): Promise<any>;

waitAndCall(method: string, data: any): Promise<any>;

broadcast<T extends keyof EventSignatures>(event: T, ...args: Parameters<EventSignatures[T]>): Promise<void>;

broadcastToServices<T extends keyof EventSignatures>(
Expand Down
1 change: 0 additions & 1 deletion packages/core-services/src/types/IBroker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export interface IBroker {
destroyService(service: IServiceClass): Promise<void>;
createService(service: IServiceClass, serviceDependencies?: string[]): void;
call(method: string, data: any): Promise<any>;
waitAndCall(method: string, data: any): Promise<any>;
broadcastToServices<T extends keyof EventSignatures>(
services: string[],
event: T,
Expand Down

0 comments on commit 5ccebc9

Please sign in to comment.