From cb4c5a875e8b38667588ab3c63b21d84935b2bfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludv=C3=ADk=20Prokopec?= Date: Fri, 2 Dec 2022 09:04:44 +0100 Subject: [PATCH] delete, create , update events --- src/lib/database.ts | 36 ++++++++++++++++++++++++++++++------ src/routes/index.svelte | 9 +++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/lib/database.ts b/src/lib/database.ts index 6dde66e..35c05c4 100644 --- a/src/lib/database.ts +++ b/src/lib/database.ts @@ -8,13 +8,37 @@ export const createCollectionSubscriber = (databaseId: string, collectionId: str databases.listDocuments(databaseId, collectionId).then(data => store.set(data.documents)) client.subscribe(`databases.${databaseId}.collections.${collectionId}.documents`, (response: RealtimeResponseEvent) => { - store.update(current => { - const index = current.findIndex(item => item.$id === response.payload.$id) - if (index === -1) return current - current[index] = response.payload - return current - }) + if (response.events.includes(`databases.${databaseId}.collections.${collectionId}.documents.*.delete`)) { + store.update(current => { + const index = current.findIndex(item => item.$id === response.payload.$id) + if (index === -1) return current + + current.splice(index, 1) + return current + }) + return + } + + if (response.events.includes(`databases.${databaseId}.collections.${collectionId}.documents.*.update`)) { + store.update(current => { + const index = current.findIndex(item => item.$id === response.payload.$id) + if (index === -1) return current + + current[index] = response.payload + return current + }) + return + } + + if (response.events.includes(`databases.${databaseId}.collections.${collectionId}.documents.*.create`)) { + store.update(current => { + current.push(response.payload) + return current + }) + return + } + }) return store diff --git a/src/routes/index.svelte b/src/routes/index.svelte index 25bed26..e6ac18e 100644 --- a/src/routes/index.svelte +++ b/src/routes/index.svelte @@ -1,7 +1,16 @@

Home

+ +
+ {#each $names as { name }} +

{name}

+ {/each} +