Skip to content

Commit 75d33e0

Browse files
committed
split up entity.ts
1 parent ad89938 commit 75d33e0

File tree

11 files changed

+465
-451
lines changed

11 files changed

+465
-451
lines changed

apps/events/src/components/todos.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export const Todos = () => {
1515
const [assignees, setAssignees] = useState<{ value: string; label: string }[]>([]);
1616

1717
useEffect(() => {
18-
// filter out assignees that are not in the users array
1918
setAssignees((prevFilteredAssignees) => {
19+
// filter out assignees that are not in the users array whenever users change
2020
return prevFilteredAssignees.filter((assignee) => users.some((user) => user.id === assignee.value));
2121
});
2222
}, [users]);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { DocHandle } from '@automerge/automerge-repo';
2+
import * as Schema from 'effect/Schema';
3+
import { generateId } from '../utils/generateId.js';
4+
import type { AnyNoContext, DocumentContent, Entity, Insert } from './types.js';
5+
6+
/**
7+
* Creates an entity model of given type and stores it in the repo.
8+
*/
9+
export const create = <const S extends AnyNoContext>(handle: DocHandle<DocumentContent>, type: S) => {
10+
// TODO: what's the right way to get the name of the type?
11+
// @ts-expect-error name is defined
12+
const typeName = type.name;
13+
const entityId = generateId();
14+
const encode = Schema.encodeSync(type.insert);
15+
16+
return (data: Readonly<Schema.Schema.Type<Insert<S>>>): Entity<S> => {
17+
const encoded = encode(data);
18+
// apply changes to the repo -> adds the entity to the repo entities document
19+
handle.change((doc) => {
20+
doc.entities ??= {};
21+
doc.entities[entityId] = { ...encoded, '@@types@@': [typeName] };
22+
});
23+
24+
return { id: entityId, ...encoded, type: typeName };
25+
};
26+
};

packages/hypergraph/src/entity/decodedEntitiesCache.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,3 @@ type DecodedEntitiesCache = Map<
3333
>;
3434

3535
export const decodedEntitiesCache: DecodedEntitiesCache = new Map();
36-
37-
export const relationParentQueries: Map<
38-
string, // entity ID
39-
Array<QueryEntry>
40-
> = new Map();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { DocHandle } from '@automerge/automerge-repo';
2+
import type { DocumentContent } from './types.js';
3+
4+
/**
5+
* Deletes the exiting entity from the repo.
6+
*/
7+
export const delete$ = (handle: DocHandle<DocumentContent>) => {
8+
return (id: string): boolean => {
9+
let result = false;
10+
11+
// apply changes to the repo -> removes the existing entity by its id
12+
handle.change((doc) => {
13+
if (doc.entities?.[id] !== undefined) {
14+
delete doc.entities[id];
15+
result = true;
16+
}
17+
});
18+
19+
return result;
20+
};
21+
};
22+
23+
export { delete$ as delete };

0 commit comments

Comments
 (0)