From f47ef0ec5c63c9fb764f793fd65f9e8c10e67a0c Mon Sep 17 00:00:00 2001 From: James Arthur Date: Tue, 3 Dec 2024 22:27:59 +0100 Subject: [PATCH] example: format. --- .../patterns/2-optimistic-state/index.tsx | 12 ++----- .../patterns/4-through-the-db/index.tsx | 4 ++- .../patterns/4-through-the-db/sync.ts | 32 +++++++++---------- examples/write-patterns/shared/app/App.tsx | 2 +- examples/write-patterns/shared/app/db.ts | 9 +++--- examples/write-patterns/vite.config.ts | 4 +-- 6 files changed, 29 insertions(+), 34 deletions(-) diff --git a/examples/write-patterns/patterns/2-optimistic-state/index.tsx b/examples/write-patterns/patterns/2-optimistic-state/index.tsx index 325bac2437..9338b14e93 100644 --- a/examples/write-patterns/patterns/2-optimistic-state/index.tsx +++ b/examples/write-patterns/patterns/2-optimistic-state/index.tsx @@ -126,11 +126,7 @@ export default function OptimisticState() { }) const fetchPromise = api.request(path, 'PUT', data) - const syncPromise = matchStream( - stream, - ['update'], - matchBy('id', id) - ) + const syncPromise = matchStream(stream, ['update'], matchBy('id', id)) await Promise.all([fetchPromise, syncPromise]) }) @@ -152,11 +148,7 @@ export default function OptimisticState() { }) const fetchPromise = api.request(path, 'DELETE') - const syncPromise = matchStream( - stream, - ['delete'], - matchBy('id', id) - ) + const syncPromise = matchStream(stream, ['delete'], matchBy('id', id)) await Promise.all([fetchPromise, syncPromise]) }) diff --git a/examples/write-patterns/patterns/4-through-the-db/index.tsx b/examples/write-patterns/patterns/4-through-the-db/index.tsx index 1b3ec880a8..e353ac856b 100644 --- a/examples/write-patterns/patterns/4-through-the-db/index.tsx +++ b/examples/write-patterns/patterns/4-through-the-db/index.tsx @@ -51,7 +51,9 @@ export default function Wrapper() { function ThroughTheDB() { const db = usePGlite() - const results = useLiveQuery('SELECT * FROM p4_todos ORDER BY created_at') + const results = useLiveQuery( + 'SELECT * FROM p4_todos ORDER BY created_at' + ) async function createTodo(event: React.FormEvent) { event.preventDefault() diff --git a/examples/write-patterns/patterns/4-through-the-db/sync.ts b/examples/write-patterns/patterns/4-through-the-db/sync.ts index 9fc214fe21..dc046c2766 100644 --- a/examples/write-patterns/patterns/4-through-the-db/sync.ts +++ b/examples/write-patterns/patterns/4-through-the-db/sync.ts @@ -16,10 +16,7 @@ type Change = { transaction_id: TransactionId } -type SendResult = - 'accepted' | - 'rejected' | - 'retry' +type SendResult = 'accepted' | 'rejected' | 'retry' /* * Minimal, naive synchronization utility, just to illustrate the pattern of @@ -48,7 +45,10 @@ export default class LocalChangeSynchronizer { async start(): Promise { console.log('start') - this.#unsubscribe = await this.#db.listen('p4_changes', this.handle.bind(this)) + this.#unsubscribe = await this.#db.listen( + 'p4_changes', + this.handle.bind(this) + ) this.process() } @@ -86,17 +86,17 @@ export default class LocalChangeSynchronizer { case 'accepted': await this.proceed(position) - break; + break case 'rejected': await this.rollback() - break; + break case 'retry': this.#hasChangedWhileProcessing = true - break; + break } } @@ -110,7 +110,7 @@ export default class LocalChangeSynchronizer { /* * Fetch the current batch of changes */ - async query(): Promise<{ changes: Change[], position: TransactionId}> { + async query(): Promise<{ changes: Change[]; position: TransactionId }> { console.log('query') const { rows } = await this.#db.sql` @@ -123,13 +123,11 @@ export default class LocalChangeSynchronizer { console.log('rows', rows) - const position = rows.length - ? rows.at(-1)!.transaction_id - : this.#position + const position = rows.length ? rows.at(-1)!.transaction_id : this.#position return { changes: rows, - position + position, } } @@ -141,12 +139,14 @@ export default class LocalChangeSynchronizer { const path = '/changes' - const groups = Object.groupBy(changes, x => x.transaction_id) - const sorted = Object.entries(groups).sort((a,b) => b[0].localeCompare(a[0])) + const groups = Object.groupBy(changes, (x) => x.transaction_id) + const sorted = Object.entries(groups).sort((a, b) => + b[0].localeCompare(a[0]) + ) const transactions = sorted.map(([transaction_id, changes]) => { return { id: transaction_id, - changes: changes + changes: changes, } }) diff --git a/examples/write-patterns/shared/app/App.tsx b/examples/write-patterns/shared/app/App.tsx index 6c55878b92..29af4b18b9 100644 --- a/examples/write-patterns/shared/app/App.tsx +++ b/examples/write-patterns/shared/app/App.tsx @@ -4,7 +4,7 @@ import { CombineOnRead, OnlineWrites, OptimisticState, - ThroughTheDB + ThroughTheDB, } from '../../patterns' const App = () => { diff --git a/examples/write-patterns/shared/app/db.ts b/examples/write-patterns/shared/app/db.ts index cef8387bcd..85ea196934 100644 --- a/examples/write-patterns/shared/app/db.ts +++ b/examples/write-patterns/shared/app/db.ts @@ -3,12 +3,13 @@ import { PGliteWithLive, live } from '@electric-sql/pglite/live' import { electricSync } from '@electric-sql/pglite-sync' const pglite: PGliteWithLive = await PGlite.create( - 'idb://electric-write-patterns', { + 'idb://electric-write-patterns', + { extensions: { electric: electricSync(), - live - } + live, + }, } ) -export default pglite \ No newline at end of file +export default pglite diff --git a/examples/write-patterns/vite.config.ts b/examples/write-patterns/vite.config.ts index 08823db572..3ed5dd7be0 100644 --- a/examples/write-patterns/vite.config.ts +++ b/examples/write-patterns/vite.config.ts @@ -5,7 +5,7 @@ import react from '@vitejs/plugin-react' // https://vitejs.dev/config/ export default defineConfig({ build: { - target: 'esnext' + target: 'esnext', }, optimizeDeps: { exclude: ['@electric-sql/pglite'], @@ -15,7 +15,7 @@ export default defineConfig({ VitePWA({ registerType: 'autoUpdate', workbox: { - maximumFileSizeToCacheInBytes: 10 * 1024 ** 2 + maximumFileSizeToCacheInBytes: 10 * 1024 ** 2, }, devOptions: { enabled: false,