1.0.0-rc.4 • Published 6 months ago

@frui.ts/dirtycheck v1.0.0-rc.4

Weekly downloads
387
License
MIT
Repository
github
Last release
6 months ago

@frui.ts/dirtycheck

Dirty checking is based on the idea that the 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 can 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 the target entity and handles respective dirty flags.

Usage

// direct usage
const target = { firstName: "John" };
const watcher = new AutomaticDirtyWatcher(target, false);

let isTargetDirty = watcher.isDirty; // false
let isPropertyDirty = watcher.dirtyProperties.firstName; // false

target.firstName = "Jane";

isTargetDirty = watcher.isDirty; // true
isPropertyDirty = watcher.dirtyProperties.firstName; // true

watcher.reset();

isTargetDirty = watcher.isDirty; // false
isPropertyDirty = watcher.dirtyProperties.firstName; // false
// with helpers
import {
  attachAutomaticDirtyWatcher,
  isDirty,
  hasVisibleDirtyChanges,
  checkDirtyChanges,
  resetDirty
} from "@frui.ts/dirtycheck";

const target = { firstName: "John" };
attachAutomaticDirtyWatcher(target);

// you can also use:
// const target = attachAutomaticDirtyWatcher({ firstName: "John" });

let dirty = isDirty(target); // false
dirty = isDirty(target, "firstName"); // false

let dirtyDisplayed = hasVisibleDirtyChanges(target); // false

target.firstName = "Jane";

dirty = isDirty(target); // true
dirty = isDirty(target, "firstName"); // true
dirtyDisplayed = hasVisibleDirtyChanges(target); // false!! - the entity is dirty but it is not indicated yet (e.g., UI for new entities)

dirty = checkDirtyChanges(target); // true - sets isDirtyFlagVisible to true and returns isDirty value

dirtyDisplayed = hasVisibleDirtyChanges(target); // true

resetDirty(target);

dirty = isDirty(target); // false
dirty = isDirty(target, "firstName"); // false
dirtyDisplayed = hasVisibleDirtyChanges(target); // false

ManualDirtyWatcher

Implements the interface IDirtyWatcher, but you have to set dirty flags on properties manually.

Usage

// direct usage
const target = { firstName: "John" };
const watcher = new ManualDirtyWatcher(false);

watcher.setDirty("firstName");

let isTargetDirty = watcher.isDirty; // true
let isPropertyDirty = watcher.dirtyProperties.firstName; // true

watcher.reset();

isTargetDirty = watcher.isDirty; // false
isPropertyDirty = watcher.dirtyProperties.firstName; // false
// with helpers
import {
  attachManualDirtyWatcher,
  isDirty,
  hasVisibleDirtyChanges,
  setDirty,
  checkDirtyChanges,
  resetDirty
} from "@frui.ts/dirtycheck";

const target = { firstName: "John" };
attachManualDirtyWatcher(target);

// you can also use:
// const target = attachManualDirtyWatcher({ firstName: "John" });

let dirty = isDirty(target); // false
dirty = isDirty(target, "firstName"); // false

let dirtyDisplayed = hasVisibleDirtyChanges(target); // false

setDirty(target, target, "firstName");

dirty = isDirty(target); // true
dirty = isDirty(target, "firstName"); // true
dirtyDisplayed = hasVisibleDirtyChanges(target); // false!! - the entity is dirty but we don't want to indicate that (e.g., UI for new entities)

dirty = checkDirtyChanges(target); // true - it enables the visibility of dirty changes and returns isDirty value

dirtyDisplayed = hasVisibleDirtyChanges(target); // true

resetDirty(target);

dirty = isDirty(target); // false
dirty = isDirty(target, "firstName"); // false
dirtyDisplayed = hasVisibleDirtyChanges(target); // false
1.0.0-rc.3

8 months ago

1.0.0-rc.4

6 months ago

1.0.0-rc.2

10 months ago

1.0.0-rc.1

11 months ago

1.0.0-beta.5

1 year ago

1.0.0-beta.6

1 year ago

1.0.0-beta.2

1 year ago

1.0.0-beta.3

1 year ago

1.0.0-beta.4

1 year ago

0.17.5

2 years ago

1.0.0-beta.1

2 years ago

0.17.4

2 years ago

1.0.0-alpha.14

2 years ago

1.0.0-alpha.13

2 years ago

1.0.0-alpha.9

2 years ago

1.0.0-alpha.8

2 years ago

1.0.0-alpha.7

2 years ago

0.17.2

2 years ago

0.17.3

2 years ago

1.0.0-alpha.6

2 years ago

1.0.0-alpha.10

2 years ago

1.0.0-alpha.12

2 years ago

1.0.0-alpha.11

2 years ago

0.16.4

2 years ago

1.0.0-alpha.5

2 years ago

1.0.0-alpha.4

2 years ago

1.0.0-alpha.3

2 years ago

0.17.1

3 years ago

0.7.2

2 years ago

1.0.0-alpha.2

3 years ago

0.17.0

3 years ago

0.17.0-rc.2

3 years ago

0.17.0-rc.1

3 years ago

1.0.0-alpha.1

3 years ago

0.17.0-beta.1

3 years ago

0.16.3

3 years ago

0.16.2

3 years ago

0.16.1

3 years ago

0.16.0

3 years ago

0.16.0-rc.2

3 years ago

0.16.0-rc.1

3 years ago

0.16.0-beta.11

3 years ago

0.16.0-beta.10

3 years ago

0.16.0-beta.9

3 years ago

0.16.0-beta.7

3 years ago

0.16.0-beta.8

3 years ago

0.16.0-beta.6

4 years ago

0.16.0-beta.5

4 years ago

0.16.0-beta.4

4 years ago

0.16.0-beta.3

4 years ago

0.16.0-beta.2

4 years ago

0.16.0-beta.1

4 years ago

0.15.0

4 years ago

0.15.0-rc.5

4 years ago

0.15.0-rc.4

4 years ago

0.15.0-rc.3

4 years ago

0.15.0-rc.1

4 years ago

0.15.0-rc.2

4 years ago

0.15.0-beta.6

4 years ago

0.15.0-beta.5

4 years ago

0.15.0-beta.4

4 years ago

0.15.0-beta.3

4 years ago

0.15.0-beta.2

4 years ago

0.15.0-beta.1

4 years ago

0.15.0-alpha.2

4 years ago

0.15.0-alpha.1

4 years ago

0.14.0

4 years ago

0.14.0-beta.3

4 years ago

0.14.0-beta.2

4 years ago

0.14.0-beta.1

4 years ago

0.13.0

4 years ago

0.13.0-beta.2

4 years ago

0.13.0-beta1

4 years ago

0.12.3

4 years ago

0.12.2

4 years ago

0.12.1

4 years ago