interface: move launch subs to airlock

This commit is contained in:
Liam Fitzgerald 2021-06-09 13:42:10 +10:00
parent f7352e7c4c
commit ba7a6a14c4
No known key found for this signature in database
GPG Key ID: D390E12C61D1CFFB
2 changed files with 73 additions and 64 deletions

View File

@ -1,55 +1,9 @@
import _ from 'lodash';
import { Cage } from '~/types/cage';
import { LaunchUpdate, WeatherState } from '~/types/launch-update';
import { reduceState } from '../state/base';
import useLaunchState, { LaunchState } from '../state/launch';
import { LaunchUpdate } from '~/types/launch-update';
import { LaunchState as State } from '../state/launch';
import { BaseState } from '../state/base';
export default class LaunchReducer {
reduce(json: Cage) {
const data = _.get(json, 'launch-update', false);
if (data) {
reduceState<LaunchState, LaunchUpdate>(useLaunchState, data, [
initial,
changeFirstTime,
changeOrder,
changeFirstTime,
changeIsShown
]);
}
const weatherData: WeatherState | boolean | Record<string, never> = _.get(json, 'weather', false);
if (weatherData) {
useLaunchState.getState().set((state) => {
// @ts-ignore investigate zustand types
state.weather = weatherData;
});
}
const locationData = _.get(json, 'location', false);
if (locationData) {
useLaunchState.getState().set((state) => {
// @ts-ignore investigate zustand types
state.userLocation = locationData;
});
}
const baseHash = _.get(json, 'baseHash', false);
if (baseHash) {
useLaunchState.getState().set((state) => {
// @ts-ignore investigate zustand types
state.baseHash = baseHash;
});
}
const runtimeLag = _.get(json, 'runtimeLag', null);
if (runtimeLag !== null) {
useLaunchState.getState().set(state => {
// @ts-ignore investigate zustand types
state.runtimeLag = runtimeLag;
});
}
}
}
type LaunchState = State & BaseState<State>;
export const initial = (json: LaunchUpdate, state: LaunchState): LaunchState => {
const data = _.get(json, 'initial', false);
@ -87,3 +41,11 @@ export const changeIsShown = (json: LaunchUpdate, state: LaunchState): LaunchSta
}
return state;
};
export const reduce = [
initial,
changeFirstTime,
changeOrder,
changeFirstTime,
changeIsShown
];

View File

@ -1,27 +1,74 @@
import { Tile, WeatherState } from '~/types/launch-update';
import { BaseState, createState } from './base';
import {
createState,
createSubscription,
reduceStateN
} from './base';
import airlock from '~/logic/api';
import { reduce } from '../reducers/launch-update';
import _ from 'lodash';
export interface LaunchState extends BaseState<LaunchState> {
export interface LaunchState {
firstTime: boolean;
tileOrdering: string[];
tiles: {
[app: string]: Tile;
},
weather: WeatherState | null | Record<string, never> | boolean,
};
weather: WeatherState | null | Record<string, never> | boolean;
userLocation: string | null;
baseHash: string | null;
runtimeLag: boolean;
};
getRuntimeLag: () => Promise<void>;
getBaseHash: () => Promise<void>;
}
// @ts-ignore investigate zustand types
const useLaunchState = createState<LaunchState>('Launch', {
firstTime: true,
tileOrdering: [],
tiles: {},
weather: null,
userLocation: null,
baseHash: null,
runtimeLag: false,
});
const useLaunchState = createState<LaunchState>(
'Launch',
(set, get) => ({
firstTime: true,
tileOrdering: [],
tiles: {},
weather: null,
userLocation: null,
baseHash: null,
runtimeLag: false,
getBaseHash: async () => {
const baseHash = await airlock.scry({
app: 'file-server',
path: '/clay/base/hash'
});
set({ baseHash });
},
getRuntimeLag: async () => {
const runtimeLag = await airlock.scry({
app: 'launch',
path: '/runtime-lag'
});
set({ runtimeLag });
}
}),
['weather'],
[
(set, get) =>
createSubscription('weather', '/all', (e) => {
const w = _.get(e, 'weather', false);
if (w) {
set({ weather: w });
}
const l = _.get(e, 'location', false);
if (l) {
set({ userLocation: l });
}
}),
(set, get) =>
createSubscription('launch', '/all', (e) => {
const d = _.get(e, 'launch-update', false);
if (d) {
reduceStateN(get(), d, reduce);
}
})
]
);
export default useLaunchState;