Skip to content

Commit

Permalink
Merge pull request #374 from TypeCellOS/staging
Browse files Browse the repository at this point in the history
fix: server reflistener for child document_relations
  • Loading branch information
YousefED authored Oct 17, 2023
2 parents 343d59c + 92e81d4 commit ea7d7fe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ export class SupabaseHocuspocus extends Database {
.from("documents")
.update(
{ data: "\\x" + data.state.toString("hex") }, // add \x for postgres binary data
{ count: "exact" }
{ count: "exact" },
)
.eq("nano_id", data.documentName)
.select();
if (ret.data?.length !== 1) {
throw new Error(
"unexpected: not found when storing " + data.documentName
"unexpected: not found when storing " + data.documentName,
);
}
},
Expand Down Expand Up @@ -164,7 +164,7 @@ export class SupabaseHocuspocus extends Database {
socketId: string,
documentId: string,
event: Y.YEvent<any>[],
tr: Y.Transaction
tr: Y.Transaction,
) => {
const supabase = this.supabaseMap.get(socketId);
if (!supabase) {
Expand All @@ -191,9 +191,15 @@ export class SupabaseHocuspocus extends Database {
.filter(
(r) =>
r.namespace === ChildReference.namespace &&
r.type === ChildReference.type
r.type === ChildReference.type,
)
.map((r) => r.target as string);
.map((r) => {
const target = r.target as string;
if (!target.startsWith("typecell:typecell.org/")) {
console.error("invalid target", target);
}
return target.split("/", 2)[1];
});

const refsIds = await serviceClient
.from("documents")
Expand Down Expand Up @@ -229,19 +235,20 @@ export class SupabaseHocuspocus extends Database {

if (ret.error) {
throw new Error(
"error executing supabase request (remove) " + ret.error.message
"error executing supabase request (remove) " + ret.error.message,
);
}
}

if (toAdd.length) {
const ret = await supabase
.from("document_relations")
.insert(toAdd.map((e) => ({ parent_id: documentId, child_id: e })));
const ret = await supabase.from("document_relations").upsert(
toAdd.map((e) => ({ parent_id: documentId, child_id: e })),
{ ignoreDuplicates: true },
);

if (ret.error) {
throw new Error(
"error executing supabase request (add) " + ret.error.message
"error executing supabase request (add) " + ret.error.message,
);
}
}
Expand All @@ -259,7 +266,7 @@ export class SupabaseHocuspocus extends Database {
data.socketId,
documentIdByDocument.get(data.document)!,
event,
tr
tr,
);

data.document.getMap("refs").observeDeep(refListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe("SupabaseHocuspocus", () => {
docId,
ydoc,
alice.session?.access_token + "$" + alice.session?.refresh_token,
wsProvider
wsProvider,
);

ydoc.getMap("mymap").set("hello", "world");
Expand All @@ -75,7 +75,7 @@ describe("SupabaseHocuspocus", () => {
docId,
ydoc2,
alice.session?.access_token + "$" + alice.session?.refresh_token,
wsProvider
wsProvider,
);

await async.timeout(100);
Expand All @@ -92,7 +92,7 @@ describe("SupabaseHocuspocus", () => {
docId,
ydoc,
alice.session?.access_token + "$" + alice.session?.refresh_token,
wsProvider
wsProvider,
);

ydoc.getMap("mymap").set("hello", "world");
Expand All @@ -102,7 +102,7 @@ describe("SupabaseHocuspocus", () => {
docId,
ydoc2,
alice.session?.access_token + "$" + alice.session?.refresh_token,
wsProvider
wsProvider,
);

ydoc2.getMap("anothermap").set("hello", "world");
Expand All @@ -122,7 +122,7 @@ describe("SupabaseHocuspocus", () => {
docId,
ydoc,
alice.session?.access_token + "$" + alice.session?.refresh_token,
wsProvider
wsProvider,
);

ydoc.getMap("mymap").set("hello", "world");
Expand All @@ -132,7 +132,7 @@ describe("SupabaseHocuspocus", () => {
docId,
ydoc2,
bob.session?.access_token + "", // TODO
wsProvider
wsProvider,
);
ydoc2.getMap("anothermap").set("hello", "world");
await async.timeout(100);
Expand Down Expand Up @@ -184,7 +184,7 @@ describe("SupabaseHocuspocus", () => {
const docB = createDocument(bob.user!.id, "", "no-access");
const retB = await bob.supabase.from("documents").insert(docB).select();
expect(retB.error).toBeNull();
docBId = retB.data![0].nano_id;
docBId = "typecell:typecell.org/" + retB.data![0].nano_id;
docBDbID = retB.data![0].id;
});

Expand All @@ -197,7 +197,7 @@ describe("SupabaseHocuspocus", () => {
docId,
ydoc,
alice.session?.access_token + "$" + alice.session?.refresh_token,
wsProvider
wsProvider,
);

ydoc.getMap("refs").set("fakekey", {
Expand Down

1 comment on commit ea7d7fe

@vercel
Copy link

@vercel vercel bot commented on ea7d7fe Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.