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

Optimistic Mutations #11

Open
4 tasks done
Akryum opened this issue Mar 13, 2025 · 0 comments
Open
4 tasks done

Optimistic Mutations #11

Akryum opened this issue Mar 13, 2025 · 0 comments

Comments

@Akryum
Copy link
Collaborator

Akryum commented Mar 13, 2025

Clear and concise description of the problem

rstore stats only updates after mutations are fully resolved. UI feeling of responsiveness can be improved by optimistically updating the cache with a reasonable expected result from the actual mutation.

Suggested solution

Create

store.Todo.create({
  text: 'Learn Vue.js'
}, {
  optimistic: {
    id: crypto.randomUUID(),
    createdDate: new Date(),
  },
})
store.Todo.createForm({
  optimistic: () => ({
    id: crypto.randomUUID(),
    createdDate: new Date(),
  }),
})

Update

store.Todo.update({
  completed: true, // Auto optimistic update
}, {
  key: 'some-id',
  optimistic: {
    updatedDate: new Date(),
  },
})
store.Todo.updateForm('some-id', {
  optimistic: () => ({
    updatedDate: new Date(),
  }),
})

Model config

const todoModel = {
  name: 'Todo',
  optimistic: {
    create: (item) => ({
      id: crypto.randomUUID(),
      createdDate: new Date(),
    }),
    update: (item) => ({
      updatedDate: new Date(),
    }),
  },
}

Check if item is optimistic (on create):

newTodo.$isOptimistic()

Check which properties are optimistically updated on an item:

editedTodo.$optimisticUpdates()
// => { updatedDate: Date }

Alternative

No response

Additional context

No response

Validations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant