Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(walletsync): enhance the websocket notifications hook for watch loop #7498

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions apps/web-tools/trustchain/components/AppAccountsSync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { Loading } from "./Loading";
import { TrustchainEjected } from "@ledgerhq/trustchain/lib-es/errors";
import { Tick } from "./Tick";
import { State } from "./types";
import { Actionable } from "./Actionable";

/*
import * as icons from "@ledgerhq/crypto-icons-ui/react";
Expand Down Expand Up @@ -176,6 +177,8 @@ export default function AppAccountsSync({
const [timestamp, setTimestamp] = useState(0);
const [onUserRefresh, setOnUserRefresh] = useState<() => void>(() => () => {});

const [watchConfig, setWatchConfig] = useState({ notificationsEnabled: false });

// pull and push wallet sync loop
useEffect(() => {
const localIncrementUpdate = makeLocalIncrementalUpdate({
Expand All @@ -187,6 +190,7 @@ export default function AppAccountsSync({
});

const { unsubscribe, onUserRefreshIntent } = walletSyncWatchLoop({
watchConfig,
walletSyncSdk,
localIncrementUpdate,
trustchain,
Expand Down Expand Up @@ -214,6 +218,7 @@ export default function AppAccountsSync({
getState,
saveUpdate,
ctx,
watchConfig,
]);

const setAccounts = useCallback(
Expand Down Expand Up @@ -264,6 +269,17 @@ export default function AppAccountsSync({
bridgeCache={bridgeCache}
setAccounts={setAccounts}
/>

<Actionable
buttonTitle="Toggle WebSocket notifications"
inputs={[watchConfig.notificationsEnabled]}
action={enabled => !enabled}
value={watchConfig.notificationsEnabled}
setValue={notificationsEnabled =>
typeof notificationsEnabled === "boolean" && setWatchConfig({ notificationsEnabled })
}
valueDisplay={v => (v ? "Listening" : "Not listening")}
/>
</div>
);
}
Expand Down
74 changes: 40 additions & 34 deletions libs/live-wallet/src/walletsync/createWalletSyncWatchLoop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import { MemberCredentials, Trustchain } from "@ledgerhq/trustchain/types";
import { WalletSyncDataManager } from "./types";
import { log } from "@ledgerhq/logs";
import { TrustchainEjected, TrustchainOutdated } from "@ledgerhq/trustchain/errors";
import { Subscription } from "rxjs";

export type WatchConfig =
| {
type: "polling";
pollingInterval?: number;
initialTimeout?: number;
userIntentDebounce?: number;
}
| {
type: "notifications";
};
export type WatchConfig = {
notificationsEnabled?: boolean;
pollingInterval?: number;
initialTimeout?: number;
userIntentDebounce?: number;
};

export type VisualConfig = {
visualPendingTimeout: number;
Expand Down Expand Up @@ -171,31 +168,40 @@ export function createWalletSyncWatchLoop<
}
}

if (watchConfig?.type === "notifications") {
throw new Error("notifications not implemented yet");
} else {
const pollingInterval = watchConfig?.pollingInterval || 30000;
const initialTimeout = watchConfig?.initialTimeout || 5000;
const userIntentDebounce = watchConfig?.userIntentDebounce || 1000;
const notificationsEnabled = watchConfig?.notificationsEnabled || false;
const pollingInterval = watchConfig?.pollingInterval || 10000;
const initialTimeout = watchConfig?.initialTimeout || 5000;
const userIntentDebounce = watchConfig?.userIntentDebounce || 1000;

// main loop
const callback = () => {
timeout = setTimeout(callback, pollingInterval);
loop();
};
let timeout = setTimeout(callback, initialTimeout);
// main loop
const callback = () => {
timeout = setTimeout(callback, pollingInterval);
loop();
};
let timeout = setTimeout(callback, initialTimeout);

return {
onUserRefreshIntent: () => {
if (unsubscribed) return;
// user intent will cancel the next loop call and reschedule one in a short time
clearTimeout(timeout);
timeout = setTimeout(callback, userIntentDebounce);
},
unsubscribe: () => {
unsubscribed = true;
clearInterval(timeout);
},
};
let notificationsSub: Subscription | null = null;
if (notificationsEnabled) {
// minimal implementation that do not handle any retry in case the notification stream is lost.
notificationsSub = walletSyncSdk
.listenNotifications(trustchain, memberCredentials)
.subscribe(() => {
log("walletsync", "notification");
loop();
});
}

return {
onUserRefreshIntent: () => {
if (unsubscribed) return;
// user intent will cancel the next loop call and reschedule one in a short time
clearTimeout(timeout);
timeout = setTimeout(callback, userIntentDebounce);
},
unsubscribe: () => {
unsubscribed = true;
clearInterval(timeout);
if (notificationsSub) notificationsSub.unsubscribe();
},
};
}
Loading