Skip to content

Electric collection mutations fail to wait for array of txids #793

@godiagonal

Description

@godiagonal
  • I've validated the bug against the latest version of DB packages

Describe the bug
I'm experiencing an issue with awaiting multiple txids in an electric collection mutation handler. I'm getting a TimeoutWaitingForTxIdError as soon as one of the txids are seen on the stream, even though I'm well below the timeout.

To Reproduce

Define a collection like this:

const todosCollection = createCollection(
  electricCollectionOptions({
    id: 'todos',
    getKey: (item) => item.id,
    shapeOptions: {
      url: '/api/todos',
      params: { table: 'todos' },
    },
    onInsert: async ({ transaction }) => {
      const txids = await Promise.all(
        transaction.mutations.map((mutation) =>
          api.todos.create(mutation.modified),
        )
      );

      return { txid: txids };
    }
  })
)

Run a mutation (single or multiple entries):

// ❌ Throws TimeoutWaitingForTxIdError before 5 second timeout
todosCollection.insert({ id: crypto.randomUUID(), title: 'New todo' })

// ❌ Throws TimeoutWaitingForTxIdError before 5 second timeout
todosCollection.insert([{ id: crypto.randomUUID(), title: 'New todo 1' }, { id: crypto.randomUUID(), title: 'New todo 2' }])

Expected behavior
I expect the mutations not to throw TimeoutWaitingForTxIdError unless the transaction takes > 5 seconds to appear on stream.

Desktop (please complete the following information):

  • OS: MacOS
  • Browser: Chrome
  • Version: 141.0.7390.123

Additional context
The waiting works fine when returning a single txid instead of a txid array in mutation handler.

This modification to processMatchingStrategy fixes the issue:

await Promise.all(result.txid.map((txid) => awaitTxId(txid)))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions