update endpoint stratigy

This commit is contained in:
liangping 2023-05-15 18:43:26 +08:00
parent 77cb720bc7
commit aef6e5f0e3
3 changed files with 18 additions and 8 deletions

View File

@ -5,7 +5,7 @@ const baseStore = useBaseStore();
chainStore.initial(); chainStore.initial();
chainStore.$subscribe((m, s) => { chainStore.$subscribe((m, s) => {
if (!Array.isArray(m.events) && m.events.key === 'endpoint') { if (!Array.isArray(m.events) && m.events.key === 'endpoint') {
chainStore.initial(); // chainStore.initial();
} }
}); });
</script> </script>

View File

@ -13,8 +13,10 @@ router.beforeEach((to) => {
const { chain } = to.params const { chain } = to.params
if(chain){ if(chain){
const blockchain = useBlockchain() const blockchain = useBlockchain()
if(chain !== blockchain.chainName) {
blockchain.setCurrent(chain.toString()) blockchain.setCurrent(chain.toString())
} }
}
}) })
// Docs: https://router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards // Docs: https://router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards

View File

@ -122,21 +122,29 @@ export const useBlockchain = defineStore('blockchain', {
}, },
async randomSetupEndpoint() { async randomSetupEndpoint() {
const end = localStorage.getItem(`endpoint-${this.chainName}`)
if(end) {
this.setRestEndpoint(JSON.parse(end))
} else {
const all = this.current?.endpoints?.rest; const all = this.current?.endpoints?.rest;
if (all) { if (all) {
const rn = Math.random(); const rn = Math.random();
const endpoint = all[Math.floor(rn * all.length)]; const endpoint = all[Math.floor(rn * all.length)];
await this.setRestEndpoint(endpoint); await this.setRestEndpoint(endpoint);
} }
}
}, },
async setRestEndpoint(endpoint: Endpoint) { async setRestEndpoint(endpoint: Endpoint) {
this.connErr = ''; this.connErr = '';
this.endpoint = endpoint; this.endpoint = endpoint;
this.rpc = new CosmosRestClient(endpoint.address, DEFAULT); this.rpc = new CosmosRestClient(endpoint.address, DEFAULT);
localStorage.setItem(`endpoint-${this.chainName}`, JSON.stringify(endpoint))
}, },
setCurrent(name: string) { setCurrent(name: string) {
if(name !== this.chainName) {
this.chainName = name; this.chainName = name;
}
}, },
}, },
}); });