From ed1de34dfccf87319dd685cf8c07a1b636037beb Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Wed, 31 Mar 2021 14:47:56 +1000 Subject: [PATCH] interface: immutable store --- pkg/interface/src/logic/state/base.ts | 18 ++++++------------ pkg/interface/src/logic/state/group.ts | 8 ++++---- pkg/interface/src/logic/store/base.ts | 4 +++- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/pkg/interface/src/logic/state/base.ts b/pkg/interface/src/logic/state/base.ts index 72af05e92b..ea38486eb4 100644 --- a/pkg/interface/src/logic/state/base.ts +++ b/pkg/interface/src/logic/state/base.ts @@ -8,13 +8,7 @@ export const stateSetter = ( 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 = >( blacklist, name: stateStorageKey(name), version: 1, // TODO version these according to base hash -})); \ No newline at end of file +})); diff --git a/pkg/interface/src/logic/state/group.ts b/pkg/interface/src/logic/state/group.ts index 218567ce20..5317665b75 100644 --- a/pkg/interface/src/logic/state/group.ts +++ b/pkg/interface/src/logic/state/group.ts @@ -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 { - groups: Set; + groups: { [rid: string]: Group; }; pendingJoin: JoinRequests; }; const useGroupState = createState('Group', { - groups: new Set(), + groups: {}, pendingJoin: {}, }, ['groups']); -export default useGroupState; \ No newline at end of file +export default useGroupState; diff --git a/pkg/interface/src/logic/store/base.ts b/pkg/interface/src/logic/store/base.ts index b2c894f34e..f798f228f6 100644 --- a/pkg/interface/src/logic/store/base.ts +++ b/pkg/interface/src/logic/store/base.ts @@ -32,7 +32,9 @@ export default class BaseStore { } this.reduce(json, this.state); - this.setState(this.state); + if('connection' in json) { + this.setState(this.state); + } } reduce(data, state) {