interface: move graph subs to airlock

This commit is contained in:
Liam Fitzgerald 2021-06-10 12:00:44 +10:00
parent 6fece992b3
commit fb36238d07
No known key found for this signature in database
GPG Key ID: D390E12C61D1CFFB
3 changed files with 104 additions and 35 deletions

View File

@ -2,6 +2,7 @@ import Urbit from '@urbit/http-api';
import useHarkState from '~/logic/state/hark';
import useMetadataState from '~/logic/state/metadata';
import useContactState from '../state/contact';
import useGraphState from '../state/graph';
import useGroupState from '../state/group';
import useInviteState from '../state/invite';
import useLaunchState from '../state/launch';
@ -25,7 +26,8 @@ export const bootstrapApi = async () => {
useContactState,
useSettingsState,
useLaunchState,
useInviteState
useInviteState,
useGraphState
].forEach((state) => {
state.getState().initialize(api);
});

View File

@ -7,8 +7,10 @@ import BigIntArrayOrderedMap, {
import bigInt, { BigInteger } from 'big-integer';
import produce from 'immer';
import _ from 'lodash';
import { reduceState } from '../state/base';
import useGraphState, { GraphState } from '../state/graph';
import { BaseState, reduceState } from '../state/base';
import useGraphState, { GraphState as State } from '../state/graph';
type GraphState = State & BaseState<State>;
const mapifyChildren = (children) => {
return new BigIntOrderedMap().gas(
@ -445,6 +447,12 @@ const removePosts = (json, state: GraphState): GraphState => {
return state;
};
export const reduceDm = [
acceptOrRejectDm,
pendings,
setScreen
];
export const GraphReducer = (json) => {
const data = _.get(json, 'graph-update', false);
@ -471,13 +479,4 @@ export const GraphReducer = (json) => {
if (thread) {
reduceState<GraphState, any>(useGraphState, thread, [addNodesThread]);
}
const dm = _.get(json, 'dm-hook-action', false);
if(dm) {
console.log(dm);
reduceState<GraphState, any>(useGraphState, dm, [
acceptOrRejectDm,
pendings,
setScreen
]);
}
};

View File

@ -1,11 +1,14 @@
import BigIntOrderedMap from '@urbit/api/lib/BigIntOrderedMap';
import { patp2dec } from 'urbit-ob';
import { Association, deSig, GraphNode, Graphs, FlatGraphs, resourceFromPath, ThreadGraphs } from '@urbit/api';
import { Association, deSig, GraphNode, Graphs, FlatGraphs, resourceFromPath, ThreadGraphs, getGraph, getShallowChildren } from '@urbit/api';
import { useCallback } from 'react';
import { BaseState, createState } from './base';
import { createState, createSubscription, reduceStateN } from './base';
import airlock from '~/logic/api';
import { getDeepOlderThan, getFirstborn, getNewest, getNode, getOlderSiblings, getYoungerSiblings } from '@urbit/api/graph';
import { GraphReducer, reduceDm } from '../reducers/graph-update';
export interface GraphState extends BaseState<GraphState> {
export interface GraphState {
graphs: Graphs;
graphKeys: Set<string>;
looseNodes: {
@ -19,18 +22,21 @@ export interface GraphState extends BaseState<GraphState> {
pendingDms: Set<string>;
screening: boolean;
graphTimesentMap: Record<number, string>;
getDeepOlderThan: (ship: string, name: string, count: number, start?: number) => Promise<void>;
// getKeys: () => Promise<void>;
// getTags: () => Promise<void>;
// getTagQueries: () => Promise<void>;
// getGraph: (ship: string, resource: string) => Promise<void>;
// getNewest: (ship: string, resource: string, count: number, index?: string) => Promise<void>;
// getOlderSiblings: (ship: string, resource: string, count: number, index?: string) => Promise<void>;
// getYoungerSiblings: (ship: string, resource: string, count: number, index?: string) => Promise<void>;
getNewest: (ship: string, resource: string, count: number, index?: string) => Promise<void>;
getOlderSiblings: (ship: string, resource: string, count: number, index?: string) => Promise<void>;
getYoungerSiblings: (ship: string, resource: string, count: number, index?: string) => Promise<void>;
// getGraphSubset: (ship: string, resource: string, start: string, end: string) => Promise<void>;
// getNode: (ship: string, resource: string, index: string) => Promise<void>;
getNode: (ship: string, resource: string, index: string) => Promise<void>;
getFirstborn: (ship: string, resource: string, index: string) => Promise<void>;
getGraph: (ship: string, name: string) => Promise<void>;
}
// @ts-ignore investigate zustand types
const useGraphState = createState<GraphState>('Graph', {
const useGraphState = createState<GraphState>('Graph', (set, get) => ({
graphs: {},
flatGraphs: {},
threadGraphs: {},
@ -39,7 +45,68 @@ const useGraphState = createState<GraphState>('Graph', {
pendingIndices: {},
graphTimesentMap: {},
pendingDms: new Set(),
screening: false
screening: false,
getDeepOlderThan: async (ship, name, count, start) => {
const data = await airlock.scry(getDeepOlderThan(ship, name, count, start));
data['graph-update'].fetch = true;
const node = data['graph-update'];
GraphReducer({
'graph-update': node,
'graph-update-flat': node
});
},
getFirstborn: async (ship, name,index) => {
const data = await airlock.scry(getFirstborn(ship, name, index));
data['graph-update'].fetch = true;
const node = data['graph-update'];
GraphReducer({
'graph-update-thread': {
index,
...node
},
'graph-update': node
});
},
getNode: (ship: string, name: string, index: string) => {
const data = await airlock.scry(getNode(ship, name, index));
data['graph-update'].fetch = true;
const node = data['graph-update'];
GraphReducer({
'graph-update-loose': node
});
},
getOlderSiblings: async (ship: string, name: string, count: number, index: string) => {
const data = await airlock.scry(getOlderSiblings(ship, name, count, index));
data['graph-update'].fetch = true;
GraphReducer(data);
},
getYoungerSiblings: async (ship: string, name: string, count: number, index: string) => {
const data = await airlock.scry(getYoungerSiblings(ship, name, count, index));
data['graph-update'].fetch = true;
GraphReducer(data);
},
getNewest: async (
ship: string,
name: string,
count: number,
index = ''
) => {
const data = await airlock.scry(getNewest(ship, name, count, index));
data['graph-update'].fetch = true;
GraphReducer(data);
},
getGraph: async (ship, name) => {
const data = await airlock.scry(getGraph(ship, name));
GraphReducer(data);
},
getShallowChildren: async (ship: string, name: string, index = '') => {
const data = await airlock.scry(getShallowChildren(ship, name, index));
data['graph-update'].fetch = true;
GraphReducer(data);
}
// getKeys: async () => {
// const api = useApi();
// const keys = await api.scry({
@ -72,19 +139,6 @@ const useGraphState = createState<GraphState>('Graph', {
// });
// graphReducer(graph);
// },
// getNewest: async (
// ship: string,
// resource: string,
// count: number,
// index: string = ''
// ) => {
// const api = useApi();
// const data = await api.scry({
// app: 'graph-store',
// path: `/newest/${ship}/${resource}/${count}${index}`
// });
// graphReducer(data);
// },
// getOlderSiblings: async (
// ship: string,
// resource: string,
@ -139,7 +193,7 @@ const useGraphState = createState<GraphState>('Graph', {
// });
// graphReducer(node);
// },
}, [
}), [
'graphs',
'graphKeys',
'looseNodes',
@ -147,6 +201,20 @@ const useGraphState = createState<GraphState>('Graph', {
'flatGraphs',
'threadGraphs',
'pendingDms'
], [
(set, get) => createSubscription('graph-store', '/updates', (e) => {
const j = _.get(e, 'graph-update', false);
if(j) {
GraphReducer(j);
}
}),
(set, get) => createSubscription('dm-hook', '/updates', (e) => {
const j = _.get(e, 'dm-hook-action', false);
if(j) {
reduceStateN(get(), j, reduceDm);
}
})
]);
export function useGraph(ship: string, name: string) {