Skip to content

Instantly share code, notes, and snippets.

@liam-fitzgerald
Created October 5, 2020 04:01
Show Gist options
  • Save liam-fitzgerald/50f66d7d62dd225fe7087e25dd53a4e6 to your computer and use it in GitHub Desktop.
Save liam-fitzgerald/50f66d7d62dd225fe7087e25dd53a4e6 to your computer and use it in GitHub Desktop.
//
// @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
@tylershuster
Copy link

Zustand fur Immer! Complete the system of German idealism.

I like this. Seems eloquent, modular, and fits well into Urbit’s data model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment