Created
December 21, 2023 15:52
-
-
Save colelawrence/f510d36cf35bffd3b7f2e0d5454de291 to your computer and use it in GitHub Desktop.
Simple callback tracker for changed values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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