Skip to content

Commit

Permalink
fix: names getting reset when type component unmounted
Browse files Browse the repository at this point in the history
  • Loading branch information
voliva committed Apr 29, 2024
1 parent 47c8436 commit fb9ef3c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 28 deletions.
3 changes: 3 additions & 0 deletions src/CommonTypes/commonTypes.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from "rxjs"
import { selectedChains$ } from "../ChainPicker"
import { metadatas } from "../api/metadatas"
import { persistSubscription } from "../lib/persistSubscription"

export type MetadataEntry =
(V15 | V14)["lookup"] extends Array<infer R> ? R : never
Expand Down Expand Up @@ -117,6 +118,8 @@ export const commonTypeNames$ = state(

return result
}, {}),
// Because the list is virtualized, we would lose the state when components unsubscribe
persistSubscription(),
),
{},
)
Expand Down
10 changes: 2 additions & 8 deletions src/api/metadatas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ import { get, set } from "idb-keyval"
import { getSmProvider } from "polkadot-api/sm-provider"
import { mapObject } from "polkadot-api/utils"
import {
ReplaySubject,
catchError,
filter,
finalize,
map,
of,
share,
startWith,
switchMap,
take,
tap,
} from "rxjs"
import { selectedChains$ } from "../ChainPicker"
import { persistSubscription } from "../lib/persistSubscription"
import { chains } from "./smoldot"

export const [changeUseCache$, setUseCache] = createSignal<boolean>()
Expand Down Expand Up @@ -58,12 +57,7 @@ export const metadatas = mapObject(chains, (chain$, key) => {
if (!result) return throughSmoldot$
return of(result)
}),
share({
connector: () => new ReplaySubject(1),
resetOnComplete: false,
resetOnRefCountZero: false,
resetOnError: true,
}),
persistSubscription(),
)
})

Expand Down
24 changes: 4 additions & 20 deletions src/api/smoldot.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { startFromWorker } from "polkadot-api/smoldot/from-worker"
import SmWorker from "polkadot-api/smoldot/worker?worker"
import {
MonoTypeOperatorFunction,
Observable,
ReplaySubject,
combineLatest,
defer,
share,
switchMap,
} from "rxjs"
import { Observable, combineLatest, defer, switchMap } from "rxjs"
import { Chain } from "smoldot"
import { persistSubscription } from "../lib/persistSubscription"

export const smoldot = startFromWorker(new SmWorker())

Expand Down Expand Up @@ -48,7 +41,7 @@ export const chains: Record<string, Observable<Chain>> = Object.fromEntries(
chainSpec,
}),
),
).pipe(persist())
).pipe(persistSubscription())
const parachainChains = Object.entries(parachains).map(
([parachainKey, parachain]) =>
[
Expand All @@ -60,20 +53,11 @@ export const chains: Record<string, Observable<Chain>> = Object.fromEntries(
potentialRelayChains: [chainRelayChain],
}),
),
persist(),
persistSubscription(),
),
] as const,
)

return [[key, chainRelayChain], ...parachainChains]
}),
)

function persist<T>(): MonoTypeOperatorFunction<T> {
return share({
connector: () => new ReplaySubject(1),
resetOnComplete: false,
resetOnRefCountZero: false,
resetOnError: true,
})
}
10 changes: 10 additions & 0 deletions src/lib/persistSubscription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { MonoTypeOperatorFunction, share, ReplaySubject } from "rxjs"

export function persistSubscription<T>(): MonoTypeOperatorFunction<T> {
return share({
connector: () => new ReplaySubject(1),
resetOnComplete: false,
resetOnRefCountZero: false,
resetOnError: true,
})
}

0 comments on commit fb9ef3c

Please sign in to comment.