From 1cffa2de1fe761c7a22907ae5ceeb7ee41fe2fc2 Mon Sep 17 00:00:00 2001 From: YannBcf Date: Wed, 14 Jun 2023 16:11:59 +0200 Subject: [PATCH] feat: allow only 1 return value per rpc call --- src/initContractRouter.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/initContractRouter.ts b/src/initContractRouter.ts index 1ae8506..dd55c1e 100644 --- a/src/initContractRouter.ts +++ b/src/initContractRouter.ts @@ -60,12 +60,24 @@ export function initContractRouter< } if (!(_rpc?.returns instanceof z.ZodVoid) || !(_rpc?.returns instanceof z.ZodUndefined)) { + let hasReturned = false; + _args.returnValue = (returnValue: typeof _rpc.returns) => { if (env === "server") { + if (hasReturned) { + throw new Error(`[alt-rpc] The rpc ${contract} already returned a value.`); + } + (opts.emit as EmitFn)(_args.player, rpcName, returnValue); + hasReturned = true; } else { + if (hasReturned) { + throw new Error(`[alt-rpc] The rpc ${contract} already returned a value.`); + } + (opts.emit as EmitFn)(rpcName, returnValue); + hasReturned = true; } }; }