mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-05-19 06:14:47 -07:00
global: fixes
This commit is contained in:
@@ -7,7 +7,6 @@
|
||||
* @import { Options } from './options/full.js'
|
||||
*
|
||||
* @import { PersistedValue } from './utils/persisted.js'
|
||||
* @import { MapCache } from './utils/cache.js'
|
||||
*
|
||||
* @import { SingleValueData, CandlestickData, Series, AnySeries, ISeries, HistogramData, LineData, BaselineData, LineSeriesPartialOptions, BaselineSeriesPartialOptions, HistogramSeriesPartialOptions, CandlestickSeriesPartialOptions, Chart, Legend } from "./utils/chart/index.js"
|
||||
*
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { brk } from "../utils/client.js";
|
||||
import { createMapCache } from "../utils/cache.js";
|
||||
import { latestPrice } from "../utils/price.js";
|
||||
import { createRow, formatBtc, renderTx, showPanel, hidePanel, TX_PAGE_SIZE } from "./render.js";
|
||||
|
||||
@@ -8,9 +7,6 @@ import { createRow, formatBtc, renderTx, showPanel, hidePanel, TX_PAGE_SIZE } fr
|
||||
/** @type {HTMLDivElement} */ let txSection;
|
||||
/** @type {string} */ let currentAddr = "";
|
||||
|
||||
const statsCache = createMapCache(50);
|
||||
const txCache = createMapCache(200);
|
||||
|
||||
const ROW_LABELS = [
|
||||
"Address",
|
||||
"Confirmed Balance",
|
||||
@@ -63,9 +59,7 @@ export async function update(address, signal) {
|
||||
while (txSection.children.length > 1) txSection.lastChild?.remove();
|
||||
|
||||
try {
|
||||
const stats = await statsCache.fetch(address, () =>
|
||||
brk.getAddress(address, { signal }),
|
||||
);
|
||||
const stats = await brk.getAddress(address, { signal });
|
||||
if (signal.aborted || currentAddr !== address) return;
|
||||
|
||||
const chain = stats.chainStats;
|
||||
@@ -118,11 +112,8 @@ export async function update(address, signal) {
|
||||
async function loadMore() {
|
||||
if (currentAddr !== address) return;
|
||||
loading = true;
|
||||
const key = `${address}:${pageIndex}`;
|
||||
try {
|
||||
const txs = await txCache.fetch(key, () =>
|
||||
brk.getAddressTxs(address, afterTxid, { signal }),
|
||||
);
|
||||
const txs = await brk.getAddressTxs(address, afterTxid, { signal });
|
||||
if (currentAddr !== address) return;
|
||||
for (const tx of txs) txSection.append(renderTx(tx));
|
||||
pageIndex++;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { brk } from "../utils/client.js";
|
||||
import { createMapCache } from "../utils/cache.js";
|
||||
import { createPersistedValue } from "../utils/persisted.js";
|
||||
import { createRow, formatFeeRate, formatHeightPrefix, renderTx, showPanel, hidePanel, TX_PAGE_SIZE } from "./render.js";
|
||||
|
||||
@@ -68,7 +67,6 @@ const ROW_DEFS = [
|
||||
let txTotalPages = 0;
|
||||
let txLoading = false;
|
||||
let txLoaded = false;
|
||||
const txPageCache = createMapCache(200);
|
||||
|
||||
const txPageParam = createPersistedValue({
|
||||
defaultValue: 0,
|
||||
@@ -200,11 +198,8 @@ async function loadTxPage(page, pushUrl = true) {
|
||||
txLoaded = true;
|
||||
if (pushUrl) txPageParam.setImmediate(page);
|
||||
updateTxNavs(page);
|
||||
const key = `${block.id}:${page}`;
|
||||
try {
|
||||
const txs = await txPageCache.fetch(key, () =>
|
||||
brk.getBlockTxsFromIndex(block.id, page * TX_PAGE_SIZE),
|
||||
);
|
||||
const txs = await brk.getBlockTxsFromIndex(block.id, page * TX_PAGE_SIZE);
|
||||
txList.innerHTML = "";
|
||||
const ascii = block.extras.coinbaseSignatureAscii;
|
||||
for (const tx of txs) txList.append(renderTx(tx, ascii));
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { explorerElement } from "../utils/elements.js";
|
||||
import { brk } from "../utils/client.js";
|
||||
import { createMapCache } from "../utils/cache.js";
|
||||
import {
|
||||
initChain,
|
||||
goToCube,
|
||||
@@ -30,7 +29,6 @@ import {
|
||||
|
||||
/** @type {number | undefined} */ let pollInterval;
|
||||
let navController = new AbortController();
|
||||
const txCache = createMapCache(50);
|
||||
let lastLoadedUrl = "";
|
||||
|
||||
function navigate() {
|
||||
@@ -134,7 +132,7 @@ async function load() {
|
||||
if (kind === "tx" && value) {
|
||||
const txid = await resolveTxid(value, { signal });
|
||||
if (signal.aborted) return;
|
||||
const tx = await txCache.fetch(txid, () => brk.getTx(txid, { signal }));
|
||||
const tx = await brk.getTx(txid, { signal });
|
||||
if (signal.aborted) return;
|
||||
await goToCube(tx.status?.blockHash ?? tx.status?.blockHeight ?? null, { silent: true });
|
||||
updateTx(tx);
|
||||
@@ -178,7 +176,7 @@ async function navigateToTx(txidOrIndex) {
|
||||
try {
|
||||
const txid = await resolveTxid(txidOrIndex, { signal });
|
||||
if (signal.aborted) return;
|
||||
const tx = await txCache.fetch(txid, () => brk.getTx(txid, { signal }));
|
||||
const tx = await brk.getTx(txid, { signal });
|
||||
if (signal.aborted) return;
|
||||
await goToCube(tx.status?.blockHash ?? tx.status?.blockHeight ?? null, { silent: true });
|
||||
updateTx(tx);
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
/**
|
||||
* @template V
|
||||
* @param {number} [maxSize]
|
||||
*/
|
||||
export function createMapCache(maxSize = 100) {
|
||||
/** @type {Map<string, V>} */
|
||||
const map = new Map();
|
||||
|
||||
/** @param {string} key @param {V} value */
|
||||
const set = (key, value) => {
|
||||
if (map.size >= maxSize && !map.has(key)) {
|
||||
const first = map.keys().next().value;
|
||||
if (first !== undefined) map.delete(first);
|
||||
}
|
||||
map.set(key, value);
|
||||
};
|
||||
|
||||
return {
|
||||
/** @param {string} key @returns {V | undefined} */
|
||||
get: (key) => map.get(key),
|
||||
/** @param {string} key @returns {boolean} */
|
||||
has: (key) => map.has(key),
|
||||
set,
|
||||
/** @param {string} key @param {() => Promise<V>} fetcher @returns {Promise<V>} */
|
||||
async fetch(key, fetcher) {
|
||||
const hit = map.get(key);
|
||||
if (hit !== undefined) return hit;
|
||||
const value = await fetcher();
|
||||
set(key, value);
|
||||
return value;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @template V
|
||||
* @typedef {ReturnType<typeof createMapCache<V>>} MapCache
|
||||
*/
|
||||
@@ -10,7 +10,6 @@ import { capture } from "./capture.js";
|
||||
import { colors } from "../colors.js";
|
||||
import { createRadios, createSelect, getElementById } from "../dom.js";
|
||||
import { createPersistedValue } from "../persisted.js";
|
||||
import { createMapCache } from "../cache.js";
|
||||
import { onChange as onThemeChange } from "../theme.js";
|
||||
import { throttle, debounce } from "../timing.js";
|
||||
import { serdeBool, INDEX_FROM_LABEL } from "../serde.js";
|
||||
@@ -193,7 +192,8 @@ export function createChart({ parent, brk, fitContent }) {
|
||||
},
|
||||
};
|
||||
|
||||
const cache = createMapCache(Infinity);
|
||||
/** @type {Map<string, AnySeriesData>} */
|
||||
const cache = new Map();
|
||||
|
||||
// Range state: localStorage stores all ranges per-index, URL stores current range only
|
||||
/** @typedef {{ from: number, to: number }} Range */
|
||||
|
||||
Reference in New Issue
Block a user