add data sources implementation
This commit is contained in:
@@ -1,28 +1,62 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { ref } from 'vue';
|
import { ref } from 'vue';
|
||||||
|
|
||||||
|
const TIMEOUT = 15e3;
|
||||||
|
|
||||||
|
const dataSources = [
|
||||||
|
{
|
||||||
|
name: 'blockchain.info',
|
||||||
|
url: 'https://blockchain.info/tobtc?currency=USD&value=1',
|
||||||
|
pick: res => 1 / +res,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'binance',
|
||||||
|
url: 'https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT',
|
||||||
|
pick: res => +res.price,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
const get = async (url) => {
|
const get = async (url) => {
|
||||||
const response = await fetch(url);
|
const response = await fetch(url);
|
||||||
return await response.json();
|
return await response.json();
|
||||||
};
|
};
|
||||||
|
|
||||||
const price = ref();
|
const calcAverage = () => {
|
||||||
|
const arr = prices.value;
|
||||||
const fetchPrice = () => {
|
console.log(arr);
|
||||||
get('https://blockchain.info/tobtc?currency=USD&value=1').then(res => {
|
average.value = arr.reduce((sum, val) => sum + val, 0) / arr.length;
|
||||||
price.value = (1 / res).toFixed(2);
|
|
||||||
console.log(price.value);
|
|
||||||
}).finally(err => {
|
|
||||||
setTimeout(fetchPrice, 15e3);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
fetchPrice();
|
const format = price => {
|
||||||
|
return price.toFixed(2);
|
||||||
|
};
|
||||||
|
|
||||||
|
const average = ref();
|
||||||
|
const prices = ref([]);
|
||||||
|
|
||||||
|
(() => {
|
||||||
|
let idx = -1;
|
||||||
|
const loop = () => {
|
||||||
|
idx = (idx + 1) % dataSources.length;
|
||||||
|
const source = dataSources[idx];
|
||||||
|
get(source.url).then(res => {
|
||||||
|
prices.value[idx] = source.pick(res);
|
||||||
|
}).then(calcAverage);
|
||||||
|
setTimeout(loop, TIMEOUT / dataSources.length);
|
||||||
|
};
|
||||||
|
loop();
|
||||||
|
})();
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
1 BTC = {{ price }} USD
|
<p>1 BTC = {{ format(average) }} USD</p>
|
||||||
|
<p>Data Sources:</p>
|
||||||
|
<ul>
|
||||||
|
<li v-for="(val, i) in dataSources">
|
||||||
|
{{ val.name }}: {{ format(prices[i]) }}
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|||||||
Reference in New Issue
Block a user