Skip to content

Commit

Permalink
feat: allow only 1 return value per rpc call
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbcf committed Jun 14, 2023
1 parent b787775 commit 1cffa2d
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/initContractRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Player, "server">)(_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<Player, "web" | "client">)(rpcName, returnValue);
hasReturned = true;
}
};
}
Expand Down

0 comments on commit 1cffa2d

Please sign in to comment.