hd-utils
Version:
A handy utils for modern JS developers
27 lines (22 loc) • 818 B
TypeScript
import { ChangeCallback } from "../types";
export interface Watcher<T> {
target: T;
observe(callback: ChangeCallback<T>): void;
unobserve(): void;
}
/**
* @description will call the passed object whenever the returned object is changed and will pass the prev and the current object.target.
* @example const watcher = createWatcher({ name: 'John', age: 25 });
// Observer function
const callbackFunction: ChangeCallback<{ name: string; age: number }> = (prev, current) => {
console.log('Object changed:', prev, '->', current);
};
// Start observing
watcher.observe(callbackFunction);
// Trigger changes
watcher.target.name = 'Alice';
watcher.target.age = 30;
// Stop observing
watcher.unobserve(callbackFunction);
*/
export default function createWatchers<T extends object>(target: T): Watcher<T>;