Skip to content

Commit b6e1345

Browse files
committed
fix(app2): move effects
1 parent 7398199 commit b6e1345

File tree

2 files changed

+50
-76
lines changed

2 files changed

+50
-76
lines changed

app2/src/lib/components/Transfer/index.svelte

-76
Original file line numberDiff line numberDiff line change
@@ -18,83 +18,7 @@ import {
1818
isComplete as isAptosComplete
1919
} from "$lib/services/transfer-ucs03-aptos"
2020
import ChainAsset from "$lib/components/Transfer/ChainAsset/index.svelte"
21-
import { balancesStore } from "$lib/stores/balances.svelte.ts"
22-
import { wallets } from "$lib/stores/wallets.svelte.ts"
23-
import { Effect, Fiber, Option } from "effect"
24-
import { tokensStore } from "$lib/stores/tokens.svelte.ts"
2521
26-
$effect(() => {
27-
transfer.getQuoteToken()
28-
transfer.getWethQuoteToken()
29-
30-
})
31-
32-
$effect(() => {
33-
if (Option.isSome(transfer.sourceChain)) {
34-
tokensStore.fetchTokens(transfer.sourceChain.value.universal_chain_id)
35-
}
36-
})
37-
38-
// Keep track of current fiber and chain
39-
let currentFetchFiber = $state<Fiber.RuntimeFiber<void, never> | null>(null)
40-
let currentChainId = $state<string | null>(null)
41-
let lastFetchKey = $state("")
42-
43-
$effect(() => {
44-
// Get the current source chain
45-
const sourceChain = Option.getOrNull(transfer.sourceChain)
46-
47-
// If the chain changed, interrupt the existing fiber
48-
if (sourceChain?.universal_chain_id !== currentChainId) {
49-
if (currentFetchFiber) {
50-
console.log(`Interrupting fiber for chain: ${currentChainId}`)
51-
Effect.runPromise(Fiber.interrupt(currentFetchFiber))
52-
currentFetchFiber = null
53-
}
54-
55-
// Update the current chain ID
56-
currentChainId = sourceChain?.universal_chain_id || null
57-
58-
// Reset the lastFetchKey when changing chains
59-
lastFetchKey = ""
60-
}
61-
62-
// Exit if no source chain
63-
if (!sourceChain) return
64-
65-
// Check for wallet address
66-
const addressOption = wallets.getAddressForChain(sourceChain)
67-
if (!Option.isSome(addressOption)) return
68-
69-
const address = addressOption.value
70-
71-
// Get tokens data
72-
const tokensOption = tokensStore.data.get(sourceChain.universal_chain_id) ?? Option.none()
73-
74-
// Only proceed if we have tokens
75-
if (!Option.isSome(tokensOption)) return
76-
77-
// Create a unique key for this combination of chain + address
78-
const fetchKey = `${sourceChain.universal_chain_id}:${address}`
79-
80-
// Check if we need to fetch new balances
81-
if (fetchKey !== lastFetchKey) {
82-
const tokens = tokensOption.value
83-
const denoms = tokens.map(token => token.denom)
84-
85-
// Get the chainKey to access the fiber from balancesStore
86-
const chainKey = `${sourceChain.universal_chain_id}:${address}` as const
87-
88-
// Fetch balances for all tokens
89-
balancesStore.fetchBalances(sourceChain, address, denoms)
90-
91-
// Store the new fiber reference
92-
currentFetchFiber = balancesStore.chainFibers.get(chainKey) || null
93-
94-
// Update the last fetch key
95-
lastFetchKey = fetchKey
96-
}
97-
})
9822
function getStatus(
9923
state: TransferStateUnion
10024
): "empty" | "filling" | "processing" | "failed" | "complete" {
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<script lang="ts">
2+
import { transfer } from "$lib/components/Transfer/transfer.svelte.ts"
3+
import { Option } from "effect"
4+
import { tokensStore } from "$lib/stores/tokens.svelte.ts"
5+
import { wallets } from "$lib/stores/wallets.svelte.ts"
6+
import { balancesStore } from "$lib/stores/balances.svelte.ts"
7+
8+
let { children } = $props()
9+
10+
$effect(() => {
11+
transfer.getQuoteToken()
12+
transfer.getWethQuoteToken()
13+
})
14+
15+
$effect(() => {
16+
if (Option.isSome(transfer.sourceChain)) {
17+
tokensStore.fetchTokens(transfer.sourceChain.value.universal_chain_id)
18+
}
19+
})
20+
21+
let lastFetchKey = $state("")
22+
23+
$effect(() => {
24+
if (Option.isNone(transfer.sourceChain)) return
25+
26+
const sourceChain = transfer.sourceChain.value
27+
28+
const addressOption = wallets.getAddressForChain(sourceChain)
29+
if (Option.isNone(addressOption)) return
30+
31+
const address = addressOption.value
32+
33+
const tokensOption = tokensStore.data.get(sourceChain.universal_chain_id) ?? Option.none()
34+
if (Option.isNone(tokensOption)) return
35+
36+
const fetchKey = `${sourceChain.universal_chain_id}:${address}`
37+
38+
if (fetchKey !== lastFetchKey) {
39+
const tokens = tokensOption.value
40+
const denoms = tokens.map(token => token.denom)
41+
42+
balancesStore.fetchBalances(sourceChain, address, denoms)
43+
lastFetchKey = fetchKey
44+
}
45+
})
46+
</script>
47+
48+
{@render children()}
49+
50+

0 commit comments

Comments
 (0)