Dirty Check

The DirtyCheckPlugin is useful for cases when you want an indication whether the state is dirty (data in the store has been modified). For example, you may want to display a save button only if the user changes something.

To activate the plugin you need to create a new instance of DirtyCheckPlugin, providing it with the Query:

widgets.component.ts
import { DirtyCheckPlugin } from '@datorama/akita';
export class WidgetsComponent {
dirtyCheck: DirtyCheckPlugin;
constructor(private widgetsQuery: WidgetsQuery) {}
ngOnInit() {
this.widgets$ = this.widgetsQuery.selectAll();
this.dirtyCheck = new DirtyCheckPlugin(this.widgetsQuery).setHead();
}
updateWidget(id: ID, name: string) {
this.widgetService.updateWidget(id, name);
}
reset() {
this.dirtyCheck.reset();
}
ngOnDestroy() {
this.dirtyCheck.destroy();
}
}

From the moment you call setHead(), Akita's DirtyCheckPlugin takes the current store snapshot and saves it as the head (the value that we compare against). With every change to the store the plugin will compare it to the head value and notify you whether the state is dirty.

widgets.component.html
<button
[class.disabled]="!(dirtyCheck.isDirty$| async)"
(click)="reset()">
Reset Changes
</button>

By calling reset() you are telling the plugin to update the store with the head value.

The plugin also provides a special method called isPathDirty() that checks whether a given path is dirty. For example:

this.dirtyCheck = new DirtyCheckPlugin(this.widgetsQuery).setHead();
this.dirtyCheck.isPathDirty('check.this.path');

Plugin options

The default comparator compares the object by using the native JSON.stringify() method, but you can pass a custom comparator, for example:

widgets.component.ts
import { isEqual } from 'lodash.isequal';
this.dirtyCheck = new DirtyCheckPlugin(this.widgetsQuery, { comparator: (a, b) => !isEqual(a, b) });

The dirty check plugin can watch specific properties in your store's state and not just the entire store, this can be achieved by passing the properties keys to DirtyCheckPlugin, for example:

// Tracks entire store
new DirtyCheckPlugin(this.widgetsQuery);
// Tracks the store's state name property
new DirtyCheckPlugin(this.widgetsQuery, {watchProperty: 'name'});
// Tracks a set of properties
new DirtyCheckPlugin(this.widgetsQuery, {watchProperty: ['name', 'color',...]});
// In case of an EntityStore we can also track all the entities
new DirtyCheckPlugin(this.widgetsQuery, {watchProperty: 'entities'});

After the first time you call setHead(), each subsequent call to this method will re-set the current store value as the head and update the dirtiness to false.

EntityDirtyCheckPlugin

In addition to the general dirty check functionality, Akita also provides a powerful API to help keep track of one or many entities, instead of the entire store.

A good example is when you have a table or a list of entities that the users can modify, and you want to give them a way to revert it per entity. Here is how you can do it:

widgets.component.ts
export class WidgetsComponent {
collection: EntityDirtyCheckPlugin;
constructor(private widgetsQuery: WidgetsQuery) {}
ngOnInit() {
this.widgets$ = this.widgetsQuery.selectAll();
/** Track all entities in the store */
this.collection = new EntityDirtyCheckPlugin(this.widgetsQuery);
/** Track specific entities by passing entityIds in the params: */
// this.collection = new EntityDirtyCheckPlugin(this.widgetsQuery, { entityIds : 1 });
// this.collection = new EntityDirtyCheckPlugin(this.widgetsQuery, { entityIds: [ 1,2 ]);
this.collection.setHead();
}
updateWidget(id: ID, name: string) {
this.widgetService.updateWidget(id, name);
}
reset(ids: ID | ID[]) {
this.collection.reset(ids);
// this.collection.reset();
}
ngOnDestroy() {
this.collection.destroy();
}
}

With this setup you can track the dirtiness per entity and revert it.

Note: EntityDirtyCheckPlugin doesn't track the entities count. It only tracks changes on the entities themselves. If you want to track the addition or removal of entities, you can do so by using the DirtyCheckPlugin and watch the entities property.

widgets.component.html
<tbody>
<tr *ngFor="let widget of widgets$ | async">
<td>
<input [value]="widget.name" #name>
</td>
<td>
<button (click)="updateWidget(widget.id, name.value)">save</button>
</td>
<td>
<button
(click)="revert(widget.id)"
[class.disabled]="!(collection.isDirty(widget.id) | async)">
Revert
</button>
</td>
</tr>
</tbody>

The isDirty() method supports both observable and raw value:

const asObservable = true/false;
return isDirty(id: ID, asObservable);

Sometimes it's useful to partially reset the entity value when clicking on revert. The revert() method can accept a custom update function which receives as parameters the current head and the current entity value, and returns the modified entity. For example:

const updateFn = (head, current) => {
return {
...head,
title: current.title
};
};
collection.reset(1, { updateFn });

In the above example we are reverting the whole entity except for the title (note that this will still mark the entity as dirty).

Sometimes it's also useful to check whether at least one of the entities is dirty. For this you can use the someDirty() method.