Skip to content

Instantly share code, notes, and snippets.

@colelawrence
Created December 21, 2023 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colelawrence/f510d36cf35bffd3b7f2e0d5454de291 to your computer and use it in GitHub Desktop.
Save colelawrence/f510d36cf35bffd3b7f2e0d5454de291 to your computer and use it in GitHub Desktop.
Simple callback tracker for changed values
export class ChangeNotifier {
callbacks: Array<() => void> = [];
onChange(callback: () => void) {
this.callbacks.push(callback);
return () => {
const index = this.callbacks.indexOf(callback);
if (index >= 0) {
this.callbacks.splice(index, 1);
}
};
}
notify() {
this.callbacks.forEach((cb) => cb());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment