CodeYellowBV/mobx-spine

Only PUT data that has really changed

Closed this issue · 2 comments

Right now, with a PUT request, we send all data to the backend again. It should detect whether a field has changed and only send it to the backend if so.

This is a lot of work however. There are two ways to do this;

  • Some kind of array with all fields that have changed on the model; changes = ['name']. Hardest part about this is that we need to intercept changes to fields (dunno how to do this yet).
  • Duplicate data that came from backend, add in separate dict and check if it is not the same; very inefficient but easy to make.

Going on about the first option;
The pain point is going to be intercepting calls to fields. We use @observable from mobx everywhere, but we can't intercept that easily (I think). We could make our own observable wrapper, that would make it easier. The next problem then is to differentiate between setting data from the backend.

note to self

// An internal property `__changes` on the model will keep track of changes when you use `setInput()`
class Animal extends Model {
  @observable name = '';
}

const animal = new Animal({ id: 1, name: 'Lino', birthDate: '2010-10-10' });
animal.setInput('name', 'Lion');
// __changes = ['name']
// Unsolved problem: what if you dont use / cannot use setInput

// This serves two use cases:

// 1. Minimize data sent to backend

// For now saving only the changes is not the default (needs much real-world testing)
// When the model is not yet saved to the backend, the whole body will be posted
animal.save({ onlyChanges: true });
// data posted to backend: `{ name: 'Lion' }`

// 2. Detect if model has changed so you can show a warning to the user

animal.isChanged();
// returns `true` or `false`