Skip to content

Commit

Permalink
tweak(server/onNetPromise): Tweak to be less opinionated and simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
TasoOneAsia committed Jun 4, 2021
1 parent fed042a commit b257f5b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 133 deletions.
91 changes: 0 additions & 91 deletions resources/server/utils/PromiseNetEvents/PromiseResponse.ts

This file was deleted.

22 changes: 10 additions & 12 deletions resources/server/utils/PromiseNetEvents/onNetPromise.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
import { ErrorStringKeys } from '../../../../typings/phone';
import { getSource } from '../miscUtils';
import { PromiseResponse } from './PromiseResponse';
import { mainLogger } from '../../sv_logger';
import { CBSignature, PromiseRequest, PromiseRequestData } from './promise.types';
import { CBSignature, PromiseEventReturnFunc, PromiseRequest } from './promise.types';

const netEventLogger = mainLogger.child({ module: 'events' });

export function onNetPromise<T>(eventName: string, cb: CBSignature<T>) {
onNet(eventName, async (respEventName: string, data: PromiseRequestData<T>) => {
export function onNetPromise<T>(eventName: string, cb: CBSignature<T>): void {
onNet(eventName, async (respEventName: string, data: T) => {
const src = getSource();

const promiseRequest: PromiseRequest<T> = {
source: src,
body: data,
data,
};

// Lets hope GC can clean you up
const promiseResp = new PromiseResponse(respEventName, src);
const promiseResp: PromiseEventReturnFunc = (data: unknown) => {
emitNet(respEventName, src, data);
netEventLogger.debug(`Response Promise Event ${respEventName}, Data >>`);
netEventLogger.debug(data);
};

// In case the cb is a promise, we use Promise.resolve
Promise.resolve(cb(promiseRequest, promiseResp)).catch((e) => {
netEventLogger.error(
`An error occured for a onNetPromise (${eventName}), Error: ${e.message}`,
);

promiseResp.sendError({
errorCode: ErrorStringKeys.SERVER_ERROR,
message: 'An error occured on the server',
});
promiseResp({ err: true, errMsg: 'Server error occurred' });
});
});
}
28 changes: 3 additions & 25 deletions resources/server/utils/PromiseNetEvents/promise.types.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,8 @@
import { FxServerRespError } from '../../../../typings/phone';
import { PromiseResponse } from './PromiseResponse';

export type BasicPrimitives = string | number | boolean;

export interface JSONLike {
[key: string]: BasicPrimitives;
}

export type PromiseRespStatus = 'error' | 'success' | 'warning' | 'in-progress';

export interface RawDataResp {
status: PromiseRespStatus;
error?: FxServerRespError;
body: JSONLike;
}

export interface PromiseRequest<T> {
body: PromiseRequestData<T>;
data: T;
source: number;
}

export interface PromiseRequestData<T> {
data: T;
}
export type PromiseEventReturnFunc = (returnData: unknown) => void;

export type CBSignature<ReqStruct> = (
request: PromiseRequest<ReqStruct>,
response: PromiseResponse,
) => Promise<void> | void;
export type CBSignature<T> = (reqObj: PromiseRequest<T>, resp: PromiseEventReturnFunc) => void;
5 changes: 0 additions & 5 deletions resources/server/utils/PromiseNetEvents/utils.ts

This file was deleted.

0 comments on commit b257f5b

Please sign in to comment.