Skip to content
Open
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
19 changes: 14 additions & 5 deletions 03-client/test/src/hooks/useCounterContract.step7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function useCounterContract() {
const { sender } = useTonConnect();

const sleep = (time: number) => new Promise((resolve) => setTimeout(resolve, time));

const counterContract = useAsyncInitialize(async () => {
if (!client) return;
const contract = new Counter(
Expand All @@ -21,15 +21,24 @@ export function useCounterContract() {
}, [client]);

useEffect(() => {
let isCancelled = false;

async function getValue() {
if (!counterContract) return;
if (!counterContract || isCancelled) return;
setVal(null);
const val = await counterContract.getCounter();
setVal(val.toString());
await sleep(5000); // sleep 5 seconds and poll value again
getValue();
if (!isCancelled) {
setVal(val.toString());
await sleep(5000); // sleep 5 seconds and poll value again
getValue();
}
}

getValue();

return () => {
isCancelled = true;
};
}, [counterContract]);

return {
Expand Down