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
f8f722c8d2
commit
25a629a59b
7 files changed
+165
-6
No files matched your search
@@ -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',
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user