@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
20 lines • 810 B
JavaScript
import { useEffect, useRef, useCallback } from "react";
import { JSONRPCServer, JSONRPCServerAndClient, JSONRPCClient } from "json-rpc-2.0";
export const useJSONRPCServer = (handlers, send) => {
const serverRef = useRef(null);
useEffect(() => {
const server = new JSONRPCServerAndClient(new JSONRPCServer(), new JSONRPCClient(send));
const methodIds = Object.keys(handlers);
methodIds.forEach((methodId) => {
server.addMethod(methodId, handlers[methodId]);
});
serverRef.current = server;
}, [send, handlers]);
const receive = useCallback(async (request) => {
if (serverRef.current) {
await serverRef.current.receiveAndSend(request);
}
}, []);
return [receive];
};
//# sourceMappingURL=JSONRPCServer.js.map