Skip to content

Commit

Permalink
examples: format.
Browse files Browse the repository at this point in the history
  • Loading branch information
thruflo committed Nov 27, 2024
1 parent 0f91d92 commit 9492a98
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 35 deletions.
4 changes: 2 additions & 2 deletions examples/write-patterns/patterns/1-online-writes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function OnlineWrites() {
const path = '/todos'
const data = {
id: uuidv4(),
title: title
title: title,
}

await api.request(path, 'POST', data)
Expand All @@ -48,7 +48,7 @@ export default function OnlineWrites() {

const data = {
...todo,
completed: !todo.completed
completed: !todo.completed,
}

await api.request(path, 'PUT', data)
Expand Down
44 changes: 32 additions & 12 deletions examples/write-patterns/patterns/2-optimistic-state/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export default function OptimisticState() {
table: 'todos',
})

const sorted = data ? data.sort((a, b) => a.created_at.localeCompare(b.created_at)) : []
const sorted = data
? data.sort((a, b) => a.created_at.localeCompare(b.created_at))
: []

// Use React's built in `useOptimistic` hook. This provides
// a mechanism to apply local optimistic state whilst writes
Expand All @@ -47,7 +49,9 @@ export default function OptimisticState() {
: [...syncedTodos, value]

case 'update':
return syncedTodos.map((todo) => todo.id === value.id ? value : todo)
return syncedTodos.map((todo) =>
todo.id === value.id ? value : todo
)

case 'delete':
return syncedTodos.filter((todo) => todo.id !== value.id)
Expand Down Expand Up @@ -77,17 +81,25 @@ export default function OptimisticState() {
const path = '/todos'
const data = {
id: uuidv4(),
title: title
title: title,
}

startTransition(async () => {
addOptimisticState({
operation: 'insert',
value: {...data, completed: false, created_at: new Date().toISOString()}
value: {
...data,
completed: false,
created_at: new Date().toISOString(),
},
})

const fetchPromise = api.request(path, 'POST', data)
const syncPromise = matchStream(stream, [`insert`], matchBy('id', data.id))
const syncPromise = matchStream(
stream,
['insert'],
matchBy('id', data.id)
)

await Promise.all([fetchPromise, syncPromise])
})
Expand All @@ -100,17 +112,21 @@ export default function OptimisticState() {

const data = {
...todo,
completed: !todo.completed
completed: !todo.completed,
}

startTransition(async () => {
addOptimisticState({
operation: 'update',
value: data
value: data,
})

const fetchPromise = api.request(path, 'PUT', data)
const syncPromise = matchStream(stream, [`update`], matchBy('id', todo.id))
const syncPromise = matchStream(
stream,
['update'],
matchBy('id', todo.id)
)

await Promise.all([fetchPromise, syncPromise])
})
Expand All @@ -120,16 +136,20 @@ export default function OptimisticState() {
event.preventDefault()

const path = `/todos/${todo.id}`
const data = {...todo}
const data = { ...todo }

startTransition(async () => {
addOptimisticState({
operation: 'delete',
value: data
value: data,
})

const fetchPromise = api.request(path, 'DELETE')
const syncPromise = matchStream(stream, [`delete`], matchBy('id', todo.id))
const syncPromise = matchStream(
stream,
['delete'],
matchBy('id', todo.id)
)

await Promise.all([fetchPromise, syncPromise])
})
Expand Down Expand Up @@ -181,4 +201,4 @@ export default function OptimisticState() {
</form>
</div>
)
}
}
26 changes: 15 additions & 11 deletions examples/write-patterns/shared/app/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,29 @@ const maxRetries = 32
const backoffMultiplier = 1.1
const initialDelayMs = 1_000

async function retryFetch(url: string, options: RequestOptions, retryCount: number) {
async function retryFetch(
url: string,
options: RequestOptions,
retryCount: number
) {
if (retryCount > maxRetries) {
return
}

const delay = retryCount * backoffMultiplier * initialDelayMs

return await new Promise((resolve) => {
setTimeout(
async () => {
resolve(await resilientFetch(url, options, retryCount))
},
delay
)
setTimeout(async () => {
resolve(await resilientFetch(url, options, retryCount))
}, delay)
})
}

async function resilientFetch(url: string, options: RequestOptions, retryCount: number) {
async function resilientFetch(
url: string,
options: RequestOptions,
retryCount: number
) {
try {
const response = await fetch(url, options)

Expand All @@ -39,8 +44,7 @@ async function resilientFetch(url: string, options: RequestOptions, retryCount:

// Could also retry here if you want to be resilient
// to 4xx and 5xx responses as well as network errors
}
catch (err) {
} catch (err) {
return await retryFetch(url, options, retryCount + 1)
}
}
Expand All @@ -62,4 +66,4 @@ async function request(path: string, method: string, data?: object) {
return await resilientFetch(url, options, 0)
}

export default { request }
export default { request }
18 changes: 8 additions & 10 deletions examples/write-patterns/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ export default defineConfig({
VitePWA({
registerType: 'autoUpdate',
devOptions: {
enabled: true
enabled: true,
},
includeAssets: [
'shared/app/icons/*'
],
includeAssets: ['shared/app/icons/*'],
manifest: {
name: 'Write Patterns Example - ElectricSQL ',
short_name: 'Writes',
Expand All @@ -23,15 +21,15 @@ export default defineConfig({
{
src: './shared/app/icons/icon-192.png',
sizes: '192x192',
type: 'image/png'
type: 'image/png',
},
{
src: './shared/app/icons/icon-512.png',
sizes: '512x512',
type: 'image/png'
}
]
}
})
type: 'image/png',
},
],
},
}),
],
})

0 comments on commit 9492a98

Please sign in to comment.