| 1 | export class StringTracker { |
| 2 | private callCount: number = 0; |
| 3 | private intervalId: any; |
| 4 | private displayElement: any; |
| 5 | |
| 6 | constructor(public trackedFunction: (...args: any[]) => any = (...args: any[]) => {}) {} |
| 7 | |
| 8 | public start() { |
| 9 | this.createDisplayElement(); |
| 10 | this.startTracking(); |
| 11 | } |
| 12 | |
| 13 | private createDisplayElement() { |
| 14 | this.displayElement = document.createElement('div'); |
| 15 | this.displayElement.style.position = 'fixed'; |
| 16 | this.displayElement.style.bottom = '10px'; |
| 17 | this.displayElement.style.right = '10px'; |
| 18 | this.displayElement.style.backgroundColor = 'black'; |
| 19 | this.displayElement.style.color = 'green'; |
| 20 | this.displayElement.style.padding = '5px 10px'; |
| 21 | this.displayElement.style.fontFamily = 'Arial, sans-serif'; |
| 22 | this.displayElement.style.fontSize = '16px'; |
| 23 | this.displayElement.style.zIndex = '1000'; |
| 24 | this.displayElement.setAttribute('data-fps', '0'); // Додаємо data-attribute |
| 25 | document.body.appendChild(this.displayElement); |
| 26 | |
| 27 | // Додаємо стилі для псевдоелемента ::after |
| 28 | const style = document.createElement('style'); |
| 29 | style.innerHTML = ` |
| 30 | [data-fps]::after { |
| 31 | content: attr(data-fps) ' FPS'; |
| 32 | position: absolute; |
| 33 | bottom: 0; |
| 34 | right: 0; |
| 35 | background-color: black; |
| 36 | color: green; |
| 37 | padding: 5px 10px; |
| 38 | font-family: Arial, sans-serif; |
| 39 | font-size: 16px; |
| 40 | z-index: 1000; |
| 41 | } |
| 42 | `; |
| 43 | document.head.appendChild(style); |
| 44 | } |
| 45 | |
| 46 | private startTracking() { |
| 47 | this.intervalId = setInterval(() => { |
| 48 | this.displayElement.setAttribute('data-fps', `${this.callCount}`); // Оновлюємо значення data-attribute |
| 49 | this.callCount = 0; |
| 50 | }, 1000); |
| 51 | } |
| 52 | |
| 53 | public getTrackedFunction() { |
| 54 | return (...args: any[]) => { |
| 55 | this.callCount++; |
| 56 | return this.trackedFunction(...args); |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | public stopTracking() { |
nothing calls this directly
no outgoing calls
no test coverage detected