Skip to content

Commit

Permalink
refactor: extract common code
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris-Hibbert committed Feb 11, 2025
1 parent d8bbbc6 commit 72b8725
Showing 1 changed file with 17 additions and 28 deletions.
45 changes: 17 additions & 28 deletions packages/synthetic-chain/src/lib/vat-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ export const dbTool = db => {
return sql;
};

/**
* @param {import('better-sqlite3').Database} db
*/
/** @param {import('better-sqlite3').Database} db */
const makeSwingstore = db => {
const sql = dbTool(db);

Expand Down Expand Up @@ -79,27 +77,29 @@ const makeSwingstore = db => {
});
};

/**
* @param {string} vatName
*/
export const getVatDetails = async vatName => {
const fullPath = swingstorePath.replace(/^~/, NonNullish(HOME));
const kStore = makeSwingstore(dbOpenAmbient(fullPath, { readonly: true }));
const fullPath = swingstorePath.replace(/^~/, NonNullish(HOME));
const kStore = makeSwingstore(dbOpenAmbient(fullPath, { readonly: true }));

const vatID = kStore.findVat(vatName);
/** @param {string} vatID */
function getVatDetailsFromID(vatID) {
const vatInfo = kStore.lookupVat(vatID);
const vatName = vatInfo.options().name;

const source = vatInfo.source();
// @ts-expect-error sqlite typedefs
const { incarnation } = vatInfo.currentSpan();
const terminated = vatInfo.terminated();

return { vatName, vatID, incarnation, ...source, terminated };
}

/** @param {string} vatName */
export const getVatDetails = async vatName => {
const vatID = kStore.findVat(vatName);
return getVatDetailsFromID(vatID);
};

/**
* @param {string} vatName
*/
/** @param {string} vatName */
export const getIncarnation = async vatName => {
const details = await getVatDetails(vatName);

Expand All @@ -109,23 +109,12 @@ export const getIncarnation = async vatName => {
return details.incarnation;
};

/** @param {string} vatName */
export const getDetailsMatchingVats = async vatName => {
const fullPath = swingstorePath.replace(/^~/, NonNullish(HOME));

const db = dbOpenAmbient(fullPath, { readonly: true });
const kStore = makeSwingstore(db);

const vatIDs = kStore.findVats(vatName);
/** @param {string} vatSubstring substring to search for within the vat name. */
export const getDetailsMatchingVats = async vatSubstring => {
const vatIDs = kStore.findVats(vatSubstring);
const infos = [];
for (const vatID of vatIDs) {
const vatInfo = kStore.lookupVat(vatID);
const name = vatInfo.options().name;
const source = vatInfo.source();
// @ts-expect-error cast
const { incarnation } = vatInfo.currentSpan();
const terminated = vatInfo.terminated();
infos.push({ vatName: name, vatID, incarnation, ...source, terminated });
infos.push(getVatDetailsFromID(vatID));
}

return infos;
Expand Down

0 comments on commit 72b8725

Please sign in to comment.