1import _get from 'lodash/get';
2import { compose } from 'lodash/fp';
3import type { Apps } from 'types/store';
4import store from 'stores/store';
5import { sleep } from 'lib/utils';
6import { api } from 'lib/api';
7import { normalizeId } from 'lib/id';
9export function getProvidedApps(patp: string): Promise<any> {
10 return api.scry<any>({ app: 'treaty', path: `/treaties/${patp}` });
13// Unused, use subscription
14export function getKnownApps(): Promise<any> {
15 return api.scry<any>({ app: 'treaty', path: `/allies` }).then(processAllies);
18export function getApp(ship: string, desk: string): Promise<any> {
19 return getAppByPath(`${normalizeId(ship)}/${desk}`);
22// @param path - ship/desk e.g. ~mister-dister-dozzod-dozzod/webterm
23export async function getAppByPath(path: string, retry: number = 2): Promise<any> {
25 return await api.scry<any>({ app: 'treaty', path: `/treaty/${path}` });
28 console.log(`Got error when fetching app: ${path}.\nTrying at most ${retry} more time(s)`);
30 return getAppByPath(path, retry - 1);
37export function getInstalledApps(): Promise<any> {
38 return api.scry<any>({ app: 'docket', path: `/charges` });
41export async function sendAllyUpdate(cmd: string, ship: string) {
42 let result = await api.poke({
44 mark: 'ally-update-0',
45 json: { [cmd]: normalizeId(ship) },
50export function addAlly(patp: string) {
51 return sendAllyUpdate('add', patp);
54export function delAlly(patp: string) {
55 return sendAllyUpdate('del', patp);
58export async function addAllies(ships: string[]) {
59 let results = await Promise.allSettled(ships.map((ship: string) => {
60 return addAlly(normalizeId(ship));
62 results = results.filter(r => r.status === 'fulfilled').map((r) => {
63 return (r as PromiseFulfilledResult<any>).value;
68export async function delAllies(ships: string[], retry: number = 0) {
69 let results = await Promise.allSettled(ships.map((ship: string) => {
70 return delAlly(normalizeId(ship));
72 results = results.filter(r => r.status === 'fulfilled').map((r) => {
73 return (r as PromiseFulfilledResult<any>).value;
78export function processAllies(rawPals: any): Apps {
79 let allies = rawPals.ini;
80 let paths = Object.values(allies).flat() as string[];
81 let set = new Set(paths);
88export type AllyUpdate = AllyUpdateIni | AllyUpdateAdd | AllyUpdateDel | AllyUpdateNew;
90export interface AllyUpdateIni {
92 [ship: string]: string[];
96export interface AllyUpdateNew {
103export interface AllyUpdateAdd {
107export interface AllyUpdateDel {
111export function handleAllyUpdateIni(json: AllyUpdate, apps: Apps): Apps {
112 const data = _get(json, 'ini', false);
114 apps = processAllies(json)
119export function handleAllyUpdateNew(json: AllyUpdate, apps: Apps): Apps {
120 const data = _get(json, 'new', false);
122 apps = processAllies({
125 [data.ship]: data.alliance,
132export function handleAllyUpdateAdd(json: AllyUpdate, apps: Apps): Apps {
133 const data = _get(json, 'add', false);
135 apps = processAllies({
145export function handleAllyUpdateDel(json: AllyUpdate, apps: Apps): Apps {
146 const data = _get(json, 'del', false);
147 if (data && (data in apps.allies)) {
148 delete apps.allies[data];
149 apps = processAllies({
156export function handleAllyUpdateEvent(updateApps) {
157 return (allyUpdate: AllyUpdate) => {
158 console.log(`received ally-update: ${Object.keys(allyUpdate)}`);
159 const reducers = [handleAllyUpdateIni, handleAllyUpdateNew, handleAllyUpdateAdd, handleAllyUpdateDel];
160 const reducer = compose(reducers.map(r => sta => r(allyUpdate, sta)));
165export function subscribeToApps(e: (data: any) => void): Promise<any> {
166 return api.subscribe({
169 event: handleAllyUpdateEvent(e),
171 console.error(`Subscription to treaty/all just got "err"`);
174 console.error(`Subscription to treaty/all just got "quit"`);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list