mirror of
https://github.com/urbit/shrub.git
synced 2024-12-19 16:51:42 +03:00
interface: immutable store
This commit is contained in:
parent
b06b4388c9
commit
ed1de34dfc
@ -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
|
||||
}));
|
||||
}));
|
||||
|
@ -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;
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user