Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sync): wait until persistence adapters ready before first sync #949

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions packages/signaldb/__tests__/helpers/memoryPersistenceAdapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import type { PersistenceAdapter } from '../../src'

export default function memoryPersistenceAdapter<
T extends { id: I } & Record<string, any>,
I = any,
>(
initialData: T[] = [],
transmitChanges = false,
) {
// not really a "persistence adapter", but it works for testing
let items = [...initialData]
const changes: {
added: T[],
modified: T[],
removed: T[],
} = {
added: [],
modified: [],
removed: [],
}
let onChange: () => void | Promise<void> = () => { /* do nothing */ }
return {
register: (changeCallback: () => void | Promise<void>) => {
onChange = changeCallback
return Promise.resolve()
},
load: () => {
const currentChanges = { ...changes }
changes.added = []
changes.modified = []
changes.removed = []
const hasChanges = currentChanges.added.length > 0
|| currentChanges.modified.length > 0
|| currentChanges.removed.length > 0
if (transmitChanges && hasChanges) {
return Promise.resolve({ changes: currentChanges })
}
return Promise.resolve({ items })
},
save: (newSnapshot: T[]) => {
items = [...newSnapshot]
return Promise.resolve()
},
addNewItem: (item: T) => {
items.push(item)
changes.added.push(item)
void onChange()
},
changeItem: (item: T) => {
items = items.map(i => (i.id === item.id ? item : i))
changes.modified.push(item)
void onChange()
},
removeItem: (item: T) => {
items = items.filter(i => i.id !== item.id)
changes.removed.push(item)
void onChange()
},
} as (PersistenceAdapter<T, I> & {
addNewItem: (item: T) => void,
changeItem: (item: T) => void,
removeItem: (item: T) => void,
})
}
63 changes: 1 addition & 62 deletions packages/signaldb/__tests__/persistence.spec.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,8 @@
import fs from 'fs/promises'
import { describe, it, expect, vi } from 'vitest'
import type { PersistenceAdapter } from '../src'
import { Collection, createFilesystemAdapter } from '../src'
import waitForEvent from './helpers/waitForEvent'

// eslint-disable-next-line max-len
function memoryPersistenceAdapter<T extends { id: I } & Record<string, any>, I = any>(
initialData: T[] = [],
transmitChanges = false,
) {
// not really a "persistence adapter", but it works for testing
let items = [...initialData]
const changes: {
added: T[],
modified: T[],
removed: T[],
} = {
added: [],
modified: [],
removed: [],
}
let onChange: () => void | Promise<void> = () => { /* do nothing */ }
return {
register: (changeCallback: () => void | Promise<void>) => {
onChange = changeCallback
return Promise.resolve()
},
load: () => {
const currentChanges = { ...changes }
changes.added = []
changes.modified = []
changes.removed = []
const hasChanges = currentChanges.added.length > 0
|| currentChanges.modified.length > 0
|| currentChanges.removed.length > 0
if (transmitChanges && hasChanges) {
return Promise.resolve({ changes: currentChanges })
}
return Promise.resolve({ items })
},
save: (newSnapshot: T[]) => {
items = [...newSnapshot]
return Promise.resolve()
},
addNewItem: (item: T) => {
items.push(item)
changes.added.push(item)
void onChange()
},
changeItem: (item: T) => {
items = items.map(i => (i.id === item.id ? item : i))
changes.modified.push(item)
void onChange()
},
removeItem: (item: T) => {
items = items.filter(i => i.id !== item.id)
changes.removed.push(item)
void onChange()
},
} as (PersistenceAdapter<T, I> & {
addNewItem: (item: T) => void,
changeItem: (item: T) => void,
removeItem: (item: T) => void,
})
}
import memoryPersistenceAdapter from './helpers/memoryPersistenceAdapter'

describe('Persistence', () => {
it('should load items from persistence adapter', async () => {
Expand Down
Loading
Loading