@frui.ts/dirtycheck
Dirty checking is based on the idea that dirty flag of a particular property of an entity can be handled as a computed value comparing the current value of the property with the value at the time of dirty check initialization. Dirty watcher is thus just a factory that is able to generate such computed properties.
This feature is usually used to indicate which UI fields have been changed during the current session and have not been saved yet.
A dirty checker not only maintains information about whether an entity is dirty, but also if the dirty flag should be displayed to the user (the isDirtyFlagVisible
property). For example, you don't want to display dirty flags when creating a new entity. You can set the isDirtyFlagVisible
property when instantiating / attaching a watcher or anytime later. The visibility is also turned on when checkDirtyChanges()
is called for the first time.
AutomaticDirtyWatcher
Dirty watcher that automatically observes target entity and handles respective dirty flags.
Usage
const target = { firstName: "John" };
const watcher = new AutomaticDirtyWatcher(target, false);
let isTargetDirty = watcher.isDirty;
let isPropertyDirty = watcher.dirtyProperties.firstName;
target.firstName = "Jane";
isTargetDirty = watcher.isDirty;
isPropertyDirty = watcher.dirtyProperties.firstName;
watcher.reset();
isTargetDirty = watcher.isDirty;
isPropertyDirty = watcher.dirtyProperties.firstName;
import {
attachAutomaticDirtyWatcher,
isDirty,
hasVisibleDirtyChanges,
checkDirtyChanges,
resetDirty
} from "@frui.ts/dirtycheck";
const target = { firstName: "John" };
attachAutomaticDirtyWatcher(target);
let dirty = isDirty(target);
dirty = isDirty(target, "firstName");
let dirtyDisplayed = hasVisibleDirtyChanges(target);
target.firstName = "Jane";
dirty = isDirty(target);
dirty = isDirty(target, "firstName");
dirtyDisplayed = hasVisibleDirtyChanges(target);
dirty = checkDirtyChanges(target);
dirtyDisplayed = hasVisibleDirtyChanges(target);
resetDirty(target);
dirty = isDirty(target);
dirty = isDirty(target, "firstName");
dirtyDisplayed = hasVisibleDirtyChanges(target);
ManualDirtyWatcher
Implements the interface IDirtyWatcher
but you have to manually set dirty flags on properties.
Usage
const target = { firstName: "John" };
const watcher = new ManualDirtyWatcher(target, false);
watcher.setDirty("firstName");
let isTargetDirty = watcher.isDirty;
let isPropertyDirty = watcher.dirtyProperties.firstName;
watcher.reset();
isTargetDirty = watcher.isDirty;
isPropertyDirty = watcher.dirtyProperties.firstName;
import {
attachManualDirtyWatcher,
isDirty,
hasVisibleDirtyChanges,
setDirty,
checkDirtyChanges,
resetDirty
} from "@frui.ts/dirtycheck";
const target = { firstName: "John" };
attachManualDirtyWatcher(target);
let dirty = isDirty(target);
dirty = isDirty(target, "firstName");
let dirtyDisplayed = hasVisibleDirtyChanges(target);
setDirty(target, target, "firstName");
dirty = isDirty(target);
dirty = isDirty(target, "firstName");
dirtyDisplayed = hasVisibleDirtyChanges(target);
dirty = checkDirtyChanges(target);
dirtyDisplayed = hasVisibleDirtyChanges(target);
resetDirty(target);
dirty = isDirty(target);
dirty = isDirty(target, "firstName");
dirtyDisplayed = hasVisibleDirtyChanges(target);