urbit/pkg/npm/api/hood/lib.ts
2022-11-03 23:46:55 -07:00

128 lines
2.1 KiB
TypeScript

import { Poke, Scry } from '../lib';
import { Vats, Vat, Pike } from './types';
export const getVats: Scry = {
app: 'hood',
path: '/kiln/vats'
};
export const getPikes: Scry = {
app: 'hood',
path: '/kiln/pikes'
};
/**
* Install a foreign desk
*/
export function kilnInstall(
ship: string,
desk: string,
local?: string
): Poke<any> {
return {
app: 'hood',
mark: 'kiln-install',
json: {
ship,
desk,
local: local || desk
}
};
}
/**
* Uninstall a desk
*/
export function kilnUninstall(
desk: string
): Poke<any> {
return {
app: 'hood',
mark: 'kiln-uninstall',
json: desk
};
}
export function kilnSuspend(
desk: string
): Poke<any> {
return {
app: 'hood',
mark: 'kiln-suspend',
json: desk
};
}
export function kilnRevive(
desk: string
): Poke<any> {
return {
app: 'hood',
mark: 'kiln-revive',
json: desk
};
}
export function kilnBump() {
return {
app: 'hood',
mark: 'kiln-bump',
json: {}
};
}
export function kilnPause(desk: string) {
return {
app: 'hood',
mark: 'kiln-pause',
json: desk
};
}
export function kilnResume(desk: string) {
return {
app: 'hood',
mark: 'kiln-resume',
json: desk
};
}
export const scryLag: Scry = ({ app: 'hood', path: '/kiln/lag' });
export function getBlockers(vats: Vats): string[] {
const blockers: string[] = [];
const base = vats?.base;
if(!base) {
return blockers;
}
const blockedOn = base.arak.rail?.next?.[0]?.weft?.kelvin;
if(!blockedOn) {
return blockers;
}
Object.entries(vats)
.filter(([desk]) => desk !== 'base')
.forEach(([desk, vat]) => {
// assuming only %zuse
const woofs = vat.arak.rail?.next || [];
const kelvins = woofs.map(n => n.weft.kelvin);
if(!(kelvins.includes(blockedOn))) {
blockers.push(desk);
}
});
return blockers;
}
export function getVatPublisher(vat: Vat): string | undefined {
if (vat.arak.rail) {
const { rail } = vat.arak;
return (rail?.publisher || rail?.ship || undefined);
}
return undefined;
}
export function getPikePublisher(pike: Pike) {
return pike.sync?.ship;
}