import { observable , action , computed , makeObservable , flow } from "mobx" ; import { ChainStore as BaseChainStore } from "@keplr-wallet/stores" ; import { ChainInfo } from "@keplr-wallet/types" ; import { ChainInfoWithEmbed , GetChainInfosMsg , RemoveSuggestedChainInfoMsg , TryUpdateChainMsg , } from "@keplr-wallet/background" ; import { BACKGROUND_PORT } from "@keplr-wallet/router" ; import { MessageRequester } from "@keplr-wallet/router" ; import { KVStore , toGenerator } from "@keplr-wallet/common" ; import { AppChainInfo } from "../../config" ; export class ChainStore extends BaseChainStore < ChainInfoWithEmbed & AppChainInfo > { @ observable protected selectedChainId : string ; @ observable protected _isInitializing : boolean = false ; protected deferChainIdSelect : string = "" ; constructor ( embedChainInfos , protected readonly requester , protected readonly kvStore ) { super ( embedChainInfos . map ( ( chainInfo ) => { return { ... chainInfo , ... { embeded : true , } , } ; } ) ) ; this . selectedChainId = embedChainInfos [ 0 ] . chainId ; makeObservable ( this ) ; this . init ( ) ; } get isInitializing ( ) : boolean { return this . _isInitializing ; } get chainInfosInUI ( ) { return this . chainInfos . filter ( ( chainInfo ) => { return ! chainInfo . raw . hideInUI ; } ) ; } @ action selectChain ( chainId ) { if ( this . _isInitializing ) { this . deferChainIdSelect = chainId ; } this . selectedChainId = chainId ; } @ computed get current ( ) : ChainInfoWithEmbed { if ( this . hasChain ( this . selectedChainId ) ) { return this . getChain ( this . selectedChainId ) . raw ; } return this . chainInfos [ 0 ] . raw ; } async saveLastViewChainId ( ) { await this . kvStore . set < string > ( "last_view_chain_id" , this . selectedChainId ) ; } @ flow protected * init ( ) { this . _isInitializing = true ; yield this . getChainInfosFromBackground ( ) ; const lastViewChainId = yield * toGenerator ( this . kvStore . get < string > ( "last_view_chain_id" ) ) ; if ( ! this . deferChainIdSelect ) { if ( lastViewChainId ) { this . selectChain ( lastViewChainId ) ; } } this . _isInitializing = false ; if ( this . deferChainIdSelect ) { this . selectChain ( this . deferChainIdSelect ) ; this . deferChainIdSelect = "" ; } } @ flow protected * getChainInfosFromBackground ( ) { const msg = new GetChainInfosMsg ( ) ; const result = yield * toGenerator ( this . requester . sendMessage ( BACKGROUND_PORT , msg ) ) ; this . setChainInfos ( result . chainInfos ) ; } @ flow * removeChainInfo ( chainId ) { const msg = new RemoveSuggestedChainInfoMsg ( chainId ) ; const chainInfos = yield * toGenerator ( this . requester . sendMessage ( BACKGROUND_PORT , msg ) ) ; this . setChainInfos ( chainInfos ) ; } @ flow * tryUpdateChain ( chainId ) { const msg = new TryUpdateChainMsg ( chainId ) ; yield this . requester . sendMessage ( BACKGROUND_PORT , msg ) ; yield this . getChainInfosFromBackground ( ) ; } }