Skip to content

Commit

Permalink
example: format.
Browse files Browse the repository at this point in the history
  • Loading branch information
thruflo committed Dec 3, 2024
1 parent 7f6f140 commit f47ef0e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 34 deletions.
12 changes: 2 additions & 10 deletions examples/write-patterns/patterns/2-optimistic-state/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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])
})
Expand All @@ -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])
})
Expand Down
4 changes: 3 additions & 1 deletion examples/write-patterns/patterns/4-through-the-db/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export default function Wrapper() {

function ThroughTheDB() {
const db = usePGlite()
const results = useLiveQuery<Todo>('SELECT * FROM p4_todos ORDER BY created_at')
const results = useLiveQuery<Todo>(
'SELECT * FROM p4_todos ORDER BY created_at'
)

async function createTodo(event: React.FormEvent) {
event.preventDefault()
Expand Down
32 changes: 16 additions & 16 deletions examples/write-patterns/patterns/4-through-the-db/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -48,7 +45,10 @@ export default class LocalChangeSynchronizer {
async start(): Promise<void> {
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()
}
Expand Down Expand Up @@ -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
}
}

Expand All @@ -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<Change>`
Expand All @@ -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,
}
}

Expand All @@ -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,
}
})

Expand Down
2 changes: 1 addition & 1 deletion examples/write-patterns/shared/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
CombineOnRead,
OnlineWrites,
OptimisticState,
ThroughTheDB
ThroughTheDB,
} from '../../patterns'

const App = () => {
Expand Down
9 changes: 5 additions & 4 deletions examples/write-patterns/shared/app/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
export default pglite
4 changes: 2 additions & 2 deletions examples/write-patterns/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -15,7 +15,7 @@ export default defineConfig({
VitePWA({
registerType: 'autoUpdate',
workbox: {
maximumFileSizeToCacheInBytes: 10 * 1024 ** 2
maximumFileSizeToCacheInBytes: 10 * 1024 ** 2,
},
devOptions: {
enabled: false,
Expand Down

0 comments on commit f47ef0e

Please sign in to comment.