2021-11-11 23:37:42 +01:00
|
|
|
// import { useSWR } from "sswr";
|
|
|
|
import LRU from "lru-cache";
|
2021-12-21 14:32:47 +01:00
|
|
|
import { derived, Readable } from "svelte/store";
|
2022-01-28 16:46:08 +01:00
|
|
|
import { UpListing, UpObject } from "upend";
|
2021-12-19 13:54:16 +01:00
|
|
|
import type { ListingResult } from "upend/types";
|
|
|
|
import { useSWR } from "../util/fetch";
|
2021-11-11 23:37:42 +01:00
|
|
|
|
2021-12-19 13:54:16 +01:00
|
|
|
const queryOnceLRU = new LRU<string, UpListing>(128);
|
|
|
|
const inFlightRequests: { [key: string]: Promise<UpListing> } = {};
|
2021-11-11 23:37:42 +01:00
|
|
|
|
2021-12-19 13:54:16 +01:00
|
|
|
export function useEntity(address: string) {
|
|
|
|
const { data, error, revalidate } = useSWR<ListingResult, unknown>(
|
2022-02-06 22:25:23 +01:00
|
|
|
`/api/obj/${address}`
|
2021-11-11 23:37:42 +01:00
|
|
|
);
|
2021-12-19 13:54:16 +01:00
|
|
|
|
|
|
|
const entity: Readable<UpObject | undefined> = derived(data, ($listing) => {
|
|
|
|
if ($listing) {
|
|
|
|
const listing = new UpListing($listing);
|
2021-12-19 19:20:09 +01:00
|
|
|
return listing.getObject(address);
|
2021-12-19 13:54:16 +01:00
|
|
|
}
|
2021-11-11 23:37:42 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
2021-12-19 13:54:16 +01:00
|
|
|
entity,
|
2021-11-11 23:37:42 +01:00
|
|
|
error,
|
|
|
|
revalidate,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-01-14 22:05:17 +01:00
|
|
|
export async function fetchEntry(address: string) {
|
|
|
|
const response = await fetch(`/api/raw/${address}`);
|
|
|
|
const data = await response.json();
|
|
|
|
const listing = new UpListing({ address: data });
|
|
|
|
return listing.entries[0];
|
|
|
|
}
|
|
|
|
|
2021-11-11 23:37:42 +01:00
|
|
|
export function query(query: () => string) {
|
2022-01-28 16:46:08 +01:00
|
|
|
const queryString = typeof query === "string" ? query : query();
|
2021-11-11 23:37:42 +01:00
|
|
|
console.debug(`Querying: ${queryString}`);
|
|
|
|
const { data, error, revalidate } = useSWR<ListingResult, unknown>(
|
2022-02-06 22:25:23 +01:00
|
|
|
"/api/query",
|
|
|
|
{ method: "POST", body: queryString }
|
2021-11-11 23:37:42 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
const result = derived(data, ($values) => {
|
2021-12-19 13:54:16 +01:00
|
|
|
return $values ? new UpListing($values) : undefined;
|
2021-11-11 23:37:42 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
result,
|
|
|
|
error,
|
|
|
|
revalidate,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-19 13:54:16 +01:00
|
|
|
export async function queryOnce(query: string): Promise<UpListing> {
|
2021-11-11 23:37:42 +01:00
|
|
|
const cacheResult = queryOnceLRU.get(query);
|
|
|
|
if (!cacheResult) {
|
2022-02-06 22:25:23 +01:00
|
|
|
if (!inFlightRequests[query]) {
|
2021-11-24 14:55:26 +01:00
|
|
|
console.debug(`Querying: ${query}`);
|
2022-02-06 22:25:23 +01:00
|
|
|
inFlightRequests[query] = new Promise((resolve, reject) => {
|
|
|
|
fetch("/api/query", { method: "POST", body: query, keepalive: true })
|
2022-01-28 16:46:08 +01:00
|
|
|
.then(async (response) => {
|
|
|
|
resolve(new UpListing(await response.json()));
|
|
|
|
})
|
|
|
|
.catch((err) => reject(err));
|
2021-11-24 14:55:26 +01:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.debug(`Chaining request for ${query}...`);
|
|
|
|
}
|
2022-02-06 22:25:23 +01:00
|
|
|
return await inFlightRequests[query];
|
2021-11-11 23:37:42 +01:00
|
|
|
} else {
|
|
|
|
console.debug(`Returning cached: ${query}`);
|
|
|
|
return cacheResult;
|
|
|
|
}
|
|
|
|
}
|