-
-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tweak(server/onNetPromise): Tweak to be less opinionated and simpler
- Loading branch information
1 parent
fed042a
commit b257f5b
Showing
4 changed files
with
13 additions
and
133 deletions.
There are no files selected for viewing
91 changes: 0 additions & 91 deletions
91
resources/server/utils/PromiseNetEvents/PromiseResponse.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file was deleted.
Oops, something went wrong.