Skip to main content

track

@webda/utils


Function: track()

track<T>(obj, onChange?, dirtyProperty?): T & object

Defined in: packages/utils/src/dirty.ts:261

Wraps an object in a dirty-tracking proxy without needing to define a class.

This is useful for ad-hoc data structures or when you don't want to create a full class. The returned object has the same properties as the input, plus a dirty getter and internal dirty state.

Type Parameters​

T​

T extends object

Parameters​

obj​

T

The object to track

onChange?​

(object, state) => void

Called when property change

dirtyProperty?​

string = "dirty"

The name of the property to use for accessing the dirty state (default: "dirty")

Returns​

T & object

A proxy of the input object with dirty-tracking capabilities

Examples​

const user = track({ name: "Alice", age: 30 });
user.name = "Bob";
user.dirty; // DirtyState with "name"
const config = track({ theme: "light", notifications: true }, undefined, "changes");
config.theme = "dark";
config.changes; // DirtyState with "theme"