Skip to content

Commit 81fa104

Browse files
authored
feat(core): getContainerIds method (#37)
1 parent 80add55 commit 81fa104

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

packages/core/src/core/mirror.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,6 +1974,11 @@ export class Mirror<S extends SchemaType> {
19741974

19751975
return undefined;
19761976
}
1977+
1978+
/* Get all container IDs registered with the mirror */
1979+
getContainerIds(): ContainerID[] {
1980+
return Array.from(this.containerRegistry.keys());
1981+
}
19771982
}
19781983

19791984
/**

packages/core/tests/mirror.test.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,4 +887,90 @@ describe("Mirror - State Consistency", () => {
887887
expect(doc.frontiers().length).toBe(0);
888888
expect(doc.toJSON()).toStrictEqual(mirror.getState());
889889
});
890+
891+
it("getContainerIds returns all registered container IDs for complex nested structures", async () => {
892+
const doc = new LoroDoc();
893+
894+
const complexSchema = schema({
895+
profile: schema.LoroMap({
896+
name: schema.String(),
897+
settings: schema.LoroMap({
898+
theme: schema.String(),
899+
preferences: schema.LoroMap({
900+
notifications: schema.Boolean(),
901+
}),
902+
}),
903+
}),
904+
todos: schema.LoroList(
905+
schema.LoroMap({
906+
id: schema.String(),
907+
text: schema.String(),
908+
completed: schema.Boolean(),
909+
}),
910+
),
911+
tree: schema.LoroTree(
912+
schema.LoroMap({
913+
title: schema.String(),
914+
metadata: schema.LoroMap({
915+
priority: schema.Number(),
916+
}),
917+
}),
918+
),
919+
});
920+
921+
const mirror = new Mirror({
922+
doc,
923+
schema: complexSchema,
924+
});
925+
926+
await mirror.setState({
927+
profile: {
928+
name: "John",
929+
settings: {
930+
theme: "dark",
931+
preferences: {
932+
notifications: true,
933+
},
934+
},
935+
},
936+
todos: [
937+
{ id: "1", text: "Task 1", completed: false },
938+
{ id: "2", text: "Task 2", completed: true },
939+
],
940+
tree: [
941+
{
942+
id: "root",
943+
data: {
944+
title: "Root Node",
945+
metadata: {
946+
priority: 1,
947+
},
948+
},
949+
children: [
950+
{
951+
id: "child1",
952+
data: {
953+
title: "Child 1",
954+
metadata: {
955+
priority: 2,
956+
},
957+
},
958+
children: [],
959+
},
960+
],
961+
},
962+
],
963+
});
964+
965+
await waitForSync();
966+
967+
const containerIds = mirror.getContainerIds();
968+
969+
expect(containerIds.length).toBe(11);
970+
971+
const uniqueIds = new Set(containerIds);
972+
expect(uniqueIds.size).toBe(containerIds.length);
973+
974+
mirror.dispose();
975+
});
890976
});

0 commit comments

Comments
 (0)