Skip to content

Commit

Permalink
Merge pull request #310 from omnisat/fix/stale-state
Browse files Browse the repository at this point in the history
fix: stale state showing up in lasereyes hook
  • Loading branch information
hathbanger authored Feb 1, 2025
2 parents 9e7eafa + 2d4f229 commit f2cc095
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
7 changes: 3 additions & 4 deletions packages/lasereyes-core/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MapStore, WritableAtom, listenKeys } from 'nanostores'
import { MapStore, WritableAtom, keepMount, listenKeys } from 'nanostores'

import { Config, ContentType, NetworkType, ProviderType } from '../types'
import {
Expand Down Expand Up @@ -40,8 +40,6 @@ export class LaserEyesClient {

dispose() {
this.disposed = true
this.$store.off()
this.$network.off()
Object.values(this.$providerMap).forEach((provider) => provider?.dispose())
}

Expand All @@ -54,6 +52,7 @@ export class LaserEyesClient {
) {
this.$store = stores.$store
this.$network = stores.$network
keepMount(this.$store)
this.$providerMap = {
[LEATHER]: new LeatherProvider(stores, this, config),
[MAGIC_EDEN]: new MagicEdenProvider(stores, this, config),
Expand Down Expand Up @@ -111,7 +110,6 @@ export class LaserEyesClient {
LOCAL_STORAGE_DEFAULT_WALLET
) as ProviderType | undefined
if (defaultWallet) {
this.$store.setKey('provider', defaultWallet)
this.connect(defaultWallet)
}
}
Expand All @@ -120,6 +118,7 @@ export class LaserEyesClient {

async connect(defaultWallet: ProviderType) {
if (this.disposed) {
console.warn('Client disposed, cannot connect')
return
}

Expand Down
42 changes: 31 additions & 11 deletions packages/lasereyes-core/src/client/providers/magic-eden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,41 @@ export default class MagicEdenProvider extends WalletProvider {
restorePersistedValues() {
const vals = this.$valueStore.get()
for (const key of keysToPersist) {
if (key === 'balance') {
this.$store.setKey(key, BigInt(vals[key]))
continue
}
this.$store.setKey(key, vals[key])
}
this.$store.setKey(
'accounts',
[vals.address, vals.paymentAddress].filter(Boolean)
)
}

watchStateChange(
newState: LaserEyesStoreType,
_: LaserEyesStoreType | undefined,
changedKey: keyof LaserEyesStoreType | undefined
) {
if (changedKey && newState.provider === MAGIC_EDEN) {
if (changedKey === 'balance') {
this.$valueStore.setKey('balance', newState.balance?.toString() ?? '')
} else if ((keysToPersist as readonly string[]).includes(changedKey)) {
this.$valueStore.setKey(
changedKey as PersistedKey,
newState[changedKey]?.toString() ?? ''
)
if (newState.provider === MAGIC_EDEN) {
if (changedKey) {
if (changedKey === 'balance') {
this.$valueStore.setKey('balance', newState.balance?.toString() ?? '')
} else if ((keysToPersist as readonly string[]).includes(changedKey)) {
this.$valueStore.setKey(
changedKey as PersistedKey,
newState[changedKey]?.toString() ?? ''
)
}
} else {
this.$valueStore.set({
address: newState.address,
paymentAddress: newState.paymentAddress,
paymentPublicKey: newState.paymentPublicKey,
publicKey: newState.publicKey,
balance: newState.balance?.toString() ?? '',
})
}
}
}
Expand Down Expand Up @@ -128,10 +146,10 @@ export default class MagicEdenProvider extends WalletProvider {
if (address.startsWith('tb1') && isMainnetNetwork(this.network)) {
this.disconnect()
} else {
this.restorePersistedValues()
getBTCBalance(paymentAddress, this.network).then((totalBalance) => {
this.$store.setKey('balance', totalBalance)
})
this.restorePersistedValues()
return
}
}
Expand All @@ -156,9 +174,12 @@ export default class MagicEdenProvider extends WalletProvider {
if (!foundAddress || !foundPaymentAddress)
throw new Error('No address found')
if (foundAddress && foundPaymentAddress) {
this.$store.setKey('provider', MAGIC_EDEN)
this.$store.setKey('address', foundAddress.address)
this.$store.setKey('paymentAddress', foundPaymentAddress.address)
this.$store.setKey('accounts', [
foundAddress.address,
foundPaymentAddress.address,
])
}
this.$store.setKey(
'publicKey',
Expand All @@ -177,7 +198,6 @@ export default class MagicEdenProvider extends WalletProvider {
},
}
await getAddress(getAddressOptions as GetAddressOptions)
this.$store.setKey('connected', true)
} catch (e) {
throw e
}
Expand Down
11 changes: 11 additions & 0 deletions packages/lasereyes-core/src/client/providers/xverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,15 @@ export default class XVerseProvider extends WalletProvider {
restorePersistedValues() {
const vals = this.$valueStore.get()
for (const key of keysToPersist) {
if (key === 'balance') {
this.$store.setKey(key, BigInt(vals[key]))
}
this.$store.setKey(key, vals[key])
}
this.$store.setKey(
'accounts',
[vals.address, vals.paymentAddress].filter(Boolean)
)
}

watchStateChange(
Expand Down Expand Up @@ -151,6 +158,10 @@ export default class XVerseProvider extends WalletProvider {
if (foundAddress && foundPaymentAddress) {
this.$store.setKey('address', foundAddress.address)
this.$store.setKey('paymentAddress', foundPaymentAddress.address)
this.$store.setKey('accounts', [
foundAddress.address,
foundPaymentAddress.address,
])
}
this.$store.setKey(
'publicKey',
Expand Down
4 changes: 2 additions & 2 deletions packages/lasereyes-react/lib/providers/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useContext, useMemo } from 'react'
import { LaserEyesStoreContext } from './context'
import { LaserEyesContextType } from './types'
import { computed, keepMount, onNotify } from 'nanostores'
import { batched, keepMount, onNotify } from 'nanostores'
import { useStore } from '@nanostores/react'
import { compareValues } from '../utils/comparison'

Expand All @@ -13,7 +13,7 @@ export function useLaserEyes<T>(
const { $network, $store, methods } = useContext(LaserEyesStoreContext)

const $computedStore = useMemo(() => {
const computedStore = computed([$store, $network], (store, network) => {
const computedStore = batched([$store, $network], (store, network) => {
const value = {
paymentAddress: store.paymentAddress,
address: store.address,
Expand Down

0 comments on commit f2cc095

Please sign in to comment.