mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-28 16:49:58 -07:00
general: snapshot
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { requestIdleCallbackPossible } from "/src/env";
|
||||
import { ONE_HOUR_IN_MS, ONE_MINUTE_IN_MS } from "/src/scripts/utils/time";
|
||||
import { createRWS } from "/src/solid/rws";
|
||||
|
||||
@@ -22,6 +23,13 @@ export function createResourceDataset<
|
||||
"https://api.satonomics.xyz"
|
||||
}${path}`;
|
||||
|
||||
const backupURL = `${
|
||||
// location.hostname === "localhost"
|
||||
// ? "http://localhost:3110"
|
||||
// : "https://api.satonomics.xyz"
|
||||
"https://api-bkp.satonomics.xyz"
|
||||
}${path}`;
|
||||
|
||||
const fetchedJSONs = new Array(
|
||||
(new Date().getFullYear() - new Date("2009-01-01").getFullYear() + 2) *
|
||||
(scale === "date" ? 1 : 6),
|
||||
@@ -114,6 +122,7 @@ export function createResourceDataset<
|
||||
let cache: Cache | undefined;
|
||||
|
||||
const urlWithQuery = `${baseURL}?chunk=${id}`;
|
||||
const backupUrlWithQuery = `${backupURL}?chunk=${id}`;
|
||||
|
||||
if (!fetched.json()) {
|
||||
try {
|
||||
@@ -138,68 +147,87 @@ export function createResourceDataset<
|
||||
return;
|
||||
}
|
||||
|
||||
let fetchedResponse: Response | undefined;
|
||||
|
||||
try {
|
||||
const fetchedResponse = await fetch(urlWithQuery);
|
||||
fetchedResponse = await fetch(urlWithQuery);
|
||||
|
||||
if (!fetchedResponse.ok) {
|
||||
throw Error;
|
||||
}
|
||||
} catch {
|
||||
try {
|
||||
fetchedResponse = await fetch(backupUrlWithQuery);
|
||||
} catch {
|
||||
fetched.loading = false;
|
||||
return;
|
||||
}
|
||||
|
||||
const clonedResponse = fetchedResponse.clone();
|
||||
if (!fetchedResponse || !fetchedResponse.ok) {
|
||||
fetched.loading = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const json = await convertResponseToJSON<Scale, Type>(fetchedResponse);
|
||||
const clonedResponse = fetchedResponse.clone();
|
||||
|
||||
if (json) {
|
||||
console.log(`fetch: ${path}?chunk=${id}`);
|
||||
const json = await convertResponseToJSON<Scale, Type>(fetchedResponse);
|
||||
|
||||
const previousMap = fetched.json()?.dataset.map;
|
||||
const newMap = json.dataset.map;
|
||||
if (!json) {
|
||||
fetched.loading = false;
|
||||
return;
|
||||
}
|
||||
|
||||
const previousLength = Object.keys(previousMap || []).length;
|
||||
const newLength = Object.keys(newMap).length;
|
||||
console.log(`fetch: ${path}?chunk=${id}`);
|
||||
|
||||
if (!newLength) {
|
||||
const previousMap = fetched.json()?.dataset.map;
|
||||
const newMap = json.dataset.map;
|
||||
|
||||
const previousLength = Object.keys(previousMap || []).length;
|
||||
const newLength = Object.keys(newMap).length;
|
||||
|
||||
if (!newLength) {
|
||||
fetched.loading = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (previousLength && previousLength === newLength) {
|
||||
const previousLastValue = Object.values(previousMap || []).at(-1);
|
||||
const newLastValue = Object.values(newMap).at(-1);
|
||||
|
||||
if (typeof newLastValue === "number") {
|
||||
if (previousLastValue === newLastValue) {
|
||||
fetched.at = new Date();
|
||||
fetched.loading = false;
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
const previousLastOHLC = previousLastValue as OHLC;
|
||||
const newLastOHLC = newLastValue as OHLC;
|
||||
|
||||
if (previousLength && previousLength === newLength) {
|
||||
const previousLastValue = Object.values(previousMap || []).at(-1);
|
||||
const newLastValue = Object.values(newMap).at(-1);
|
||||
|
||||
if (typeof newLastValue === "number") {
|
||||
if (previousLastValue === newLastValue) {
|
||||
fetched.at = new Date();
|
||||
fetched.loading = false;
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
const previousLastOHLC = previousLastValue as OHLC;
|
||||
const newLastOHLC = newLastValue as OHLC;
|
||||
|
||||
if (
|
||||
previousLastOHLC.open === newLastOHLC.open &&
|
||||
previousLastOHLC.high === newLastOHLC.high &&
|
||||
previousLastOHLC.low === newLastOHLC.low &&
|
||||
previousLastOHLC.close === newLastOHLC.close
|
||||
) {
|
||||
fetched.loading = false;
|
||||
fetched.at = new Date();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fetched.json.set(() => json);
|
||||
|
||||
if (cache) {
|
||||
cache.put(urlWithQuery, clonedResponse);
|
||||
if (
|
||||
previousLastOHLC.open === newLastOHLC.open &&
|
||||
previousLastOHLC.high === newLastOHLC.high &&
|
||||
previousLastOHLC.low === newLastOHLC.low &&
|
||||
previousLastOHLC.close === newLastOHLC.close
|
||||
) {
|
||||
fetched.loading = false;
|
||||
fetched.at = new Date();
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
fetched.loading = false;
|
||||
return;
|
||||
}
|
||||
|
||||
fetched.json.set(() => json);
|
||||
|
||||
function saveToCache() {
|
||||
cache?.put(urlWithQuery, clonedResponse);
|
||||
}
|
||||
|
||||
if (requestIdleCallbackPossible) {
|
||||
requestIdleCallback(saveToCache);
|
||||
} else {
|
||||
setTimeout(saveToCache, 1);
|
||||
}
|
||||
|
||||
fetched.at = new Date();
|
||||
|
||||
@@ -33,6 +33,13 @@ export const createBaseLineSeries = ({
|
||||
|
||||
const bottomLineColor = bottomColor || color || DEFAULT_BASELINE_BOTTOM_COLOR;
|
||||
|
||||
function computeColors() {
|
||||
return {
|
||||
topLineColor: topLineColor(dark),
|
||||
bottomLineColor: bottomLineColor(dark),
|
||||
} as const;
|
||||
}
|
||||
|
||||
const seriesOptions: DeepPartialBaselineOptions = {
|
||||
priceScaleId: "right",
|
||||
...defaultSeriesOptions,
|
||||
@@ -45,15 +52,13 @@ export const createBaseLineSeries = ({
|
||||
topFillColor2: transparent,
|
||||
bottomFillColor1: transparent,
|
||||
bottomFillColor2: transparent,
|
||||
...computeColors(),
|
||||
};
|
||||
|
||||
const series = chart.addBaselineSeries(seriesOptions);
|
||||
|
||||
createEffect(() => {
|
||||
series.applyOptions({
|
||||
topLineColor: topLineColor(dark),
|
||||
bottomLineColor: bottomLineColor(dark),
|
||||
});
|
||||
series.applyOptions(computeColors());
|
||||
});
|
||||
|
||||
return series;
|
||||
|
||||
@@ -11,6 +11,22 @@ export const createCandlesticksSeries = ({
|
||||
}): [ISeriesApi<"Candlestick">, Color[]] => {
|
||||
const { inverseColors } = options;
|
||||
|
||||
const _upColor = inverseColors ? colors.loss : colors.profit;
|
||||
|
||||
const _downColor = inverseColors ? colors.profit : colors.loss;
|
||||
|
||||
function computeColors() {
|
||||
const upColor = _upColor(dark);
|
||||
|
||||
const downColor = _downColor(dark);
|
||||
|
||||
return {
|
||||
upColor,
|
||||
wickUpColor: upColor,
|
||||
downColor,
|
||||
wickDownColor: downColor,
|
||||
} as const;
|
||||
}
|
||||
const candlestickSeries = chart.addCandlestickSeries({
|
||||
baseLineVisible: false,
|
||||
borderVisible: false,
|
||||
@@ -20,23 +36,11 @@ export const createCandlesticksSeries = ({
|
||||
borderDownColor: "",
|
||||
borderUpColor: "",
|
||||
...options.seriesOptions,
|
||||
...computeColors(),
|
||||
});
|
||||
|
||||
const _upColor = inverseColors ? colors.loss : colors.profit;
|
||||
|
||||
const _downColor = inverseColors ? colors.profit : colors.loss;
|
||||
|
||||
createEffect(() => {
|
||||
const upColor = _upColor(dark);
|
||||
|
||||
const downColor = _downColor(dark);
|
||||
|
||||
candlestickSeries.applyOptions({
|
||||
upColor,
|
||||
wickUpColor: upColor,
|
||||
downColor,
|
||||
wickDownColor: downColor,
|
||||
});
|
||||
candlestickSeries.applyOptions(computeColors());
|
||||
});
|
||||
|
||||
return [candlestickSeries, [_upColor, _downColor]];
|
||||
|
||||
@@ -11,15 +11,20 @@ export const createLineSeries = ({
|
||||
color: Color;
|
||||
options?: DeepPartialLineOptions;
|
||||
}) => {
|
||||
function computeColors() {
|
||||
return {
|
||||
color: color(dark),
|
||||
} as const;
|
||||
}
|
||||
|
||||
const series = chart.addLineSeries({
|
||||
...defaultSeriesOptions,
|
||||
...options,
|
||||
...computeColors(),
|
||||
});
|
||||
|
||||
createEffect(() => {
|
||||
series.applyOptions({
|
||||
color: color(dark),
|
||||
});
|
||||
series.applyOptions(computeColors());
|
||||
});
|
||||
|
||||
return series;
|
||||
|
||||
Reference in New Issue
Block a user