Skip to content

Commit

Permalink
fix!: create fetch identity
Browse files Browse the repository at this point in the history
  • Loading branch information
2nthony committed Nov 6, 2024
1 parent 1e1b9e3 commit a45c5f6
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions src/services/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,10 @@ import debounce from "lodash.debounce";
import { identityChainMap } from "@osn/constants";
import { Deferred, encodeNetworkAddress } from "../utils";

const identityServerHost =
(typeof process !== "undefined"
? process.env.REACT_APP_IDENTITY_SERVER_HOST
: undefined) ||
(typeof process !== "undefined"
? process.env.NEXT_PUBLIC_IDENTITY_SERVER_HOST
: undefined) ||
"https://id.statescan.io";

const cachedIdentities = new Map();
let pendingQueries = new Map();

const delayQuery = debounce((fetchOptions) => {
const delayQuery = debounce((identityServerHost, fetchOptions) => {
const pending = pendingQueries;
if (pending.size < 1) {
return;
Expand Down Expand Up @@ -76,23 +67,27 @@ const delayQuery = debounce((fetchOptions) => {
}
}, 0);

/**
* @param {RequestInit} fetchOptions
*/
export async function fetchIdentity(chain, address, fetchOptions = {}) {
const targetChain = identityChainMap[chain] || chain;
const targetAddress = encodeNetworkAddress(address, targetChain);
const idName = `${targetChain}/${targetAddress}`;
if (cachedIdentities.has(idName)) {
return cachedIdentities.get(idName);
}
export function createFetchIdentity(
identityServerHost = "https://id.statescan.io",
) {
/**
* @param {RequestInit} fetchOptions
*/
return async function fetchIdentity(chain, address, fetchOptions = {}) {
const targetChain = identityChainMap[chain] || chain;
const targetAddress = encodeNetworkAddress(address, targetChain);
const idName = `${targetChain}/${targetAddress}`;
if (cachedIdentities.has(idName)) {
return cachedIdentities.get(idName);
}

const pending = pendingQueries;
const pending = pendingQueries;

if (!pending.has(idName)) {
pending.set(idName, new Deferred());
delayQuery(fetchOptions);
}
if (!pending.has(idName)) {
pending.set(idName, new Deferred());
delayQuery(identityServerHost, fetchOptions);
}

return await pending.get(idName).promise;
return await pending.get(idName).promise;
};
}

0 comments on commit a45c5f6

Please sign in to comment.