interface: immutable store

This commit is contained in:
Liam Fitzgerald 2021-03-31 14:47:56 +10:00
parent b06b4388c9
commit ed1de34dfc
No known key found for this signature in database
GPG Key ID: D390E12C61D1CFFB
3 changed files with 13 additions and 17 deletions

View File

@ -8,13 +8,7 @@ export const stateSetter = <StateType>(
fn: (state: StateType) => void,
set
): void => {
// fn = (state: StateType) => {
// // TODO this is a stub for the store debugging
// fn(state);
// }
return set(fn);
// TODO we want to use the below, but it makes everything read-only
return set(produce(fn));
set(produce(fn));
};
export const reduceState = <
@ -25,10 +19,10 @@ export const reduceState = <
data: UpdateType,
reducers: ((data: UpdateType, state: StateType) => StateType)[]
): void => {
const oldState = state.getState();
const reducer = compose(reducers.map(reducer => reducer.bind(reducer, data)));
const newState = reducer(oldState);
state.getState().set(state => state = newState);
const reducer = compose(reducers.map(r => sta => r(data, sta)));
state.getState().set(state => {
reducer(state);
});
};
export let stateStorageKeys: string[] = [];
@ -61,4 +55,4 @@ export const createState = <StateType extends BaseState<any>>(
blacklist,
name: stateStorageKey(name),
version: 1, // TODO version these according to base hash
}));
}));

View File

@ -1,15 +1,15 @@
import { Path, JoinRequests } from "@urbit/api";
import { Path, JoinRequests, Group } from "@urbit/api";
import { BaseState, createState } from "./base";
export interface GroupState extends BaseState<GroupState> {
groups: Set<Path>;
groups: { [rid: string]: Group; };
pendingJoin: JoinRequests;
};
const useGroupState = createState<GroupState>('Group', {
groups: new Set(),
groups: {},
pendingJoin: {},
}, ['groups']);
export default useGroupState;
export default useGroupState;

View File

@ -32,7 +32,9 @@ export default class BaseStore<S extends object> {
}
this.reduce(json, this.state);
this.setState(this.state);
if('connection' in json) {
this.setState(this.state);
}
}
reduce(data, state) {