Skip to content

Commit

Permalink
feat(server): react hooks use ref instead of memo
Browse files Browse the repository at this point in the history
Add setConfig to server class
  • Loading branch information
Justkant committed Aug 2, 2023
1 parent c3076e3 commit 187adea
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
7 changes: 7 additions & 0 deletions .changeset/wet-bats-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@ledgerhq/wallet-api-server": minor
---

feat(server): react hooks use ref instead of memo

Add setConfig to server class
16 changes: 10 additions & 6 deletions packages/server/src/WalletAPIServer.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {
Account,
AppHandlers,
Currency,
Logger,
Transport,
RpcNode,
RpcRequest,
Permission,
RpcError,
RpcErrorCode,
AppHandlers,
Permission,
createPermissionDenied,
RpcNode,
RpcRequest,
ServerError,
Transport,
createPermissionDenied,
} from "@ledgerhq/wallet-api-core";
import { BehaviorSubject, combineLatest } from "rxjs";
import { filterAccountsForCurrencies, matchCurrencies } from "./helpers";
Expand Down Expand Up @@ -62,6 +62,10 @@ export class WalletAPIServer extends RpcNode<
return this;
}

setConfig(config: ServerConfig) {
this.walletContext.config = config;
}

public setHandler<K extends keyof WalletHandlers>(
methodName: K,
method: WalletHandlers[K]
Expand Down
30 changes: 18 additions & 12 deletions packages/server/src/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import type {
Permission,
Transport,
} from "@ledgerhq/wallet-api-core";
import { useCallback, useEffect, useMemo } from "react";
import type { ServerConfig } from "./types";
import { useCallback, useEffect, useRef } from "react";
import { WalletAPIServer } from "./WalletAPIServer";
import type { ServerConfig } from "./types";

export function useWalletAPIServer({
transport,
Expand All @@ -24,22 +24,28 @@ export function useWalletAPIServer({
currencies: Currency[];
permission: Permission;
}) {
const server = useMemo(
() => new WalletAPIServer(transport, config, logger),
[config, logger, transport]
);
const server = useRef<WalletAPIServer>();

useEffect(() => {
server.current = new WalletAPIServer(transport, config, logger);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
server?.current?.setConfig(config);
}, [config]);

useEffect(() => {
server.setPermissions(permission);
}, [permission, server]);
server?.current?.setPermissions(permission);
}, [permission]);

useEffect(() => {
server.setCurrencies(currencies);
}, [currencies, server]);
server?.current?.setCurrencies(currencies);
}, [currencies]);

useEffect(() => {
server.setAccounts(accounts);
}, [accounts, server]);
server?.current?.setAccounts(accounts);
}, [accounts]);

const onMessage = useCallback(
(event: string) => {
Expand Down

0 comments on commit 187adea

Please sign in to comment.