Skip to content

Commit

Permalink
start pulling apart concerns
Browse files Browse the repository at this point in the history
  • Loading branch information
sammynave committed Jan 4, 2024
1 parent c27d41c commit 8bceb5e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 39 deletions.
22 changes: 13 additions & 9 deletions src/routes/app/offline-first/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
<script lang="ts">
import { nanoid } from 'nanoid';
import { db } from './sync-db-store.js';
import { setupWs } from './sync-db-store.js';
import schemaContent from '$lib/sync/schema.sql?raw';
import Todos from './Todos.svelte';
import { Database } from './server-sync-db.js';
export let data;
const databasePromise = Database.load({
schema: { name: 'schema.sql', schemaContent },
name: data.dbName
});
const wsPromise = setupWs({ url: data.url, database: databasePromise });
</script>

<Todos
name="todos 1"
dbConfig={{
schema: { name: 'schema.sql', schemaContent },
name: data.dbName,
wsUrl: data.url,
databasePromise,
wsPromise,
serverSiteId: data.serverSiteId
}}
/>

<Todos
name="todos 2"
dbConfig={{
schema: { name: 'schema.sql', schemaContent },
name: data.dbName,
wsUrl: data.url,
databasePromise,
wsPromise,
serverSiteId: data.serverSiteId
}}
/>;
/>
20 changes: 1 addition & 19 deletions src/routes/app/offline-first/Todos.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
},
identifier: 'todos'
});
const todonts = store({
query: async (db) => await db.execO('SELECT * FROM todonts'),
commands: {
Expand Down Expand Up @@ -120,25 +121,6 @@
*
* NOTE: This might be a better basis for reacting to `merges` rather than these two separate systems but 🤷‍♂️
*/
/*
TODO
TODO
TODO
TODO
TODO
TODO
This isn't working because `databasePromise` is created in `sync-db-store.ts`. There needs to be one app level instance
of `Database.load({schema, name})` (and we really should have only one instance of `setupWs`) that can be shared across all components.
These should probably be a top-level context. That way, registering `.onUpdate` will work for every change in every component.
(and of course we don't want multiple instances of the DB or websocket open)
TODO
TODO
TODO
TODO
TODO
TODO
*/
database.then((db) => {
db.db.onUpdate(async (type, dbName, tblName, rowid) => {
if (tblName === 'todos') {
Expand Down
15 changes: 4 additions & 11 deletions src/routes/app/offline-first/sync-db-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ async function pushChangesSince({ database, ws, sinceVersion, serverSiteId }) {
function wsMessageHandler({
database,
updates,
serverSiteId,
identifier
serverSiteId
}: {
database: Database;
updates: Set<() => Promise<void>>;
serverSiteId: string;
identifier?: string;
}) {
// TODO
// TODO
Expand Down Expand Up @@ -76,7 +74,7 @@ function wsMessageHandler({
}

// TODO: probably need re-connect/retry logic if WS server closes connection
async function setupWs({ url, database }: { url: string; database: Promise<Database> }) {
export async function setupWs({ url, database }: { url: string; database: Promise<Database> }) {
const db = await database;
const u = new URL(url);
const features = JSON.parse(u.searchParams.get('features') as string);
Expand All @@ -88,9 +86,7 @@ async function setupWs({ url, database }: { url: string; database: Promise<Datab
return ws;
}

export function db({ schema, name, wsUrl, serverSiteId, identifier }) {
const databasePromise = Database.load({ schema, name });
const wsPromise = setupWs({ url: wsUrl, database: databasePromise });
export function db({ databasePromise, wsPromise, serverSiteId }) {
let listenerAdded = false;
const updates = new Set([]);

Expand All @@ -105,10 +101,7 @@ export function db({ schema, name, wsUrl, serverSiteId, identifier }) {
// every `store`
updates.add(update);
if (listenerAdded === false) {
ws.addEventListener(
'message',
wsMessageHandler({ database, identifier, serverSiteId, updates })
);
ws.addEventListener('message', wsMessageHandler({ database, serverSiteId, updates }));
listenerAdded = true;
}
await update();
Expand Down

0 comments on commit 8bceb5e

Please sign in to comment.