Created
October 5, 2020 04:01
-
-
Save liam-fitzgerald/50f66d7d62dd225fe7087e25dd53a4e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// @urbit/groups | |
// | |
// import Airlock, { Poke, Watch, scry } from '@urbit/airlock' | |
// declare types | |
// | |
interface Group {} | |
type Groups = Record<string, Group>; | |
export type GroupUpdate = GroupUpdateAddMembers | GroupUpdateRemoveMembers; | |
// declare functions for pokes/watches/scries over airlock | |
export function addMembers( | |
group: Path, | |
members: Patp[] | |
): Poke<"group-update", GroupUpdateAddMembers> { | |
return { | |
action: "poke", | |
app: "group-store", | |
mark: "group-update", | |
json: { | |
addMembers: { | |
group, | |
members, | |
}, | |
}, | |
}; | |
} | |
export function watchGroups(): Watch { | |
return { | |
action: "subscribe", | |
app: "group-store", | |
mark: "group-update", | |
path: "/groups", | |
}; | |
} | |
export async function getGroup(group: string): Promise<Group> { | |
return scry<Group>("group-store", `/groups${group}`); | |
} | |
// @tlon/landscape-groups | |
// | |
// | |
// import { Group, GroupUpdate, watch, scry, poke } from '@urbit/groups' | |
// import create from 'zustand/vanilla' | |
// import Airlock from '@urbit/airlock' | |
// import produce from 'immer'; | |
// | |
const addMembersReducer = ( | |
gs: Groups, | |
group: Path, | |
members: Patp[] | |
): Groups => { | |
return produce((groups) => { | |
groups[group].members.concat(members); | |
}); | |
}; | |
export const groupStore = create((set) => ({ | |
groups: {} as Record<string, Group>, | |
addMembers: (group: Path, members: Patp[]) => { | |
await Airlock.poke(poke.addMembers(group, members)); | |
return set(({ groups }) => ({ | |
groups: addMembersReducer(groups, group, members), | |
})); | |
}, | |
})); | |
/// landscape then depends on @tlon/landscape-groups |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Zustand fur Immer! Complete the system of German idealism.
I like this. Seems eloquent, modular, and fits well into Urbit’s data model.