Created
December 17, 2021 11:05
-
-
Save dawiidio/2e76d6ea237380d9ef34fcd1ff6732c6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class Counter { | |
| private child: Counter[] = []; | |
| private value: number = 0; | |
| private destroyed: boolean = false; | |
| increase(): number { | |
| if (this.destroyed) | |
| throw new Error(`destroyed counter can not be increased`); | |
| return ++this.value; | |
| } | |
| getCount(): number { | |
| return this.value; | |
| } | |
| destroy() { | |
| this.destroyed = true; | |
| this.child.forEach(c => c.destroy()); | |
| } | |
| spawn(): Counter { | |
| const child = new Counter(); | |
| this.child.push(child); | |
| return child; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment