mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 14:38:45 +00:00
feat(net-events): Initial opinionated promise wrapper around net events
Lets see how I & the rest of the team feel about this specific standard and we can tweak it from there.
This commit is contained in:
1 parent
c94c69c646
commit
51b5620866
7 files changed
+165
-6
No files matched your search
@@ -35,7 +35,7 @@ class _ContactService {
|
||||
this.responseBuilder(src, {
|
||||
action: ContactEvents.UPDATE_CONTACT,
|
||||
error: {
|
||||
errorKey: ErrorStringKeys.UPDATE_FAILED,
|
||||
errorCode: ErrorStringKeys.UPDATE_FAILED,
|
||||
message: `Failed to update contact ${contact.id}`,
|
||||
},
|
||||
});
|
||||
@@ -52,7 +52,7 @@ class _ContactService {
|
||||
this.responseBuilder(src, {
|
||||
action: ContactEvents.DELETE_CONTACT,
|
||||
error: {
|
||||
errorKey: ErrorStringKeys.DELETE_FAILED,
|
||||
errorCode: ErrorStringKeys.DELETE_FAILED,
|
||||
message: `Failed to delete contactId ${contactId}`,
|
||||
},
|
||||
});
|
||||
@@ -72,7 +72,7 @@ class _ContactService {
|
||||
this.responseBuilder(src, {
|
||||
action: ContactEvents.ADD_CONTACT,
|
||||
error: {
|
||||
errorKey: ErrorStringKeys.ADD_FAILED,
|
||||
errorCode: ErrorStringKeys.ADD_FAILED,
|
||||
message: 'Unable to add contacts',
|
||||
},
|
||||
});
|
||||
@@ -92,7 +92,7 @@ class _ContactService {
|
||||
this.responseBuilder(src, {
|
||||
action: ContactEvents.SEND_CONTACTS,
|
||||
error: {
|
||||
errorKey: ErrorStringKeys.SERVER_ERROR,
|
||||
errorCode: ErrorStringKeys.SERVER_ERROR,
|
||||
message: 'Unable to fetch contacts',
|
||||
},
|
||||
});
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import { FxServerRespError } from '../../../../typings/phone';
|
||||
import { JSONLike, PromiseRespStatus, RawDataResp } from './promise.types';
|
||||
import { netEventLogger } from './utils';
|
||||
import { config } from '../../server';
|
||||
|
||||
// Meh temp maybe not idk.
|
||||
interface PromiseRespDebug {
|
||||
/* Start time returned by process.hrtime()*/
|
||||
started: [number, number];
|
||||
}
|
||||
|
||||
export class PromiseResponse {
|
||||
public readonly respEventName: string;
|
||||
public readonly source: number;
|
||||
private promiseRespDebug: PromiseRespDebug;
|
||||
protected responseData: RawDataResp;
|
||||
|
||||
/**
|
||||
* Create a new promise response context for use in the onNetPrommise,
|
||||
* callback argument
|
||||
*
|
||||
* @param respEventName - The response event name to emit to on resp.send()
|
||||
* @param source - The source of the player to emit back to
|
||||
*/
|
||||
constructor(respEventName: string, source: number) {
|
||||
this.respEventName = respEventName;
|
||||
this.source = source;
|
||||
|
||||
this.promiseRespDebug = {
|
||||
started: process.hrtime(),
|
||||
};
|
||||
|
||||
this.responseData = {
|
||||
body: {},
|
||||
status: 'in-progress',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Will emit back to the client with the data assigned using
|
||||
* `setData`. Will send all data assigned to the this.responseData
|
||||
* back to the client to resolve the promise.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public send(obj: JSONLike): void {
|
||||
this.responseData.body = obj;
|
||||
this.responseData.status = 'success';
|
||||
emitNet(this.respEventName, this.source, this.responseData);
|
||||
|
||||
if (config.debug.level === 'silly') {
|
||||
// Debug time data
|
||||
const hrEnd = process.hrtime(this.promiseRespDebug.started);
|
||||
// Divide by good old 1 x 10^6
|
||||
const nanoToMicro = hrEnd[1] / 1e6;
|
||||
const secondExec = hrEnd[0];
|
||||
|
||||
netEventLogger.silly(`Promise emitted to client after ${secondExec}s ${nanoToMicro}ms`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Will change the status field present on promiseResponses, this doesn't
|
||||
* really have a use for now except metadata
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
|
||||
public setStatus(status: PromiseRespStatus): this {
|
||||
this.responseData.status = status;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Will emit back to the client with the data assigned using
|
||||
* `setData`. Will send all data assigned to the this.responseData
|
||||
* back to the client to resolve the promise.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public sendError({ errorCode, message }: FxServerRespError): void {
|
||||
this.responseData = {
|
||||
error: {
|
||||
errorCode,
|
||||
message,
|
||||
},
|
||||
body: {},
|
||||
status: 'error',
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
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';
|
||||
|
||||
const netEventLogger = mainLogger.child({ module: 'events' });
|
||||
|
||||
export function onNetPromise<T>(eventName: string, cb: CBSignature<T>) {
|
||||
onNet(eventName, async (respEventName: string, data: PromiseRequestData<T>) => {
|
||||
const src = getSource();
|
||||
|
||||
const promiseRequest: PromiseRequest<T> = {
|
||||
source: src,
|
||||
body: data,
|
||||
};
|
||||
|
||||
// Lets hope GC can clean you up
|
||||
const promiseResp = new PromiseResponse(respEventName, src);
|
||||
|
||||
// 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',
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
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>;
|
||||
source: number;
|
||||
}
|
||||
|
||||
export interface PromiseRequestData<T> {
|
||||
data: T;
|
||||
}
|
||||
|
||||
export type CBSignature<ReqStruct> = (
|
||||
request: PromiseRequest<ReqStruct>,
|
||||
response: PromiseResponse,
|
||||
) => Promise<void> | void;
|
||||
@@ -0,0 +1,5 @@
|
||||
import { mainLogger } from '../../sv_logger';
|
||||
|
||||
export const netEventLogger = mainLogger.child({
|
||||
module: 'net-events',
|
||||
});
|
||||
Reference in new issue
Block a user