|
1 | 1 | import { compact } from 'lodash' |
2 | | -import React, { Component, useState } from 'react' |
| 2 | +import React, { useCallback, useRef, useState } from 'react' |
3 | 3 |
|
4 | | -import Stick from '../../src' |
| 4 | +import Stick from '../../es' |
| 5 | +import { useWatcher } from './hooks' |
5 | 6 |
|
6 | 7 | const formPairs = (listA: Array<string>, listB: Array<string>) => |
7 | 8 | compact( |
@@ -42,44 +43,28 @@ const Node = () => ( |
42 | 43 | /> |
43 | 44 | ) |
44 | 45 |
|
45 | | -class FramesPerSecond extends Component { |
46 | | - state = { |
47 | | - fps: 0, |
48 | | - } |
| 46 | +function FramesPerSecond({ updateOnAnimationFrame }) { |
| 47 | + const [fps, setFps] = useState(0) |
| 48 | + const lastUpdated = useRef(Date.now()) |
| 49 | + const framesSinceLastUpdate = useRef(0) |
49 | 50 |
|
50 | | - lastUpdated = Date.now() |
51 | | - framesSinceLastUpdate = 0 |
| 51 | + const measure = useCallback(() => { |
| 52 | + framesSinceLastUpdate.current += 1 |
52 | 53 |
|
53 | | - startTracking() { |
54 | | - const requestCallback = this.props.updateOnAnimationFrame |
55 | | - ? requestAnimationFrame |
56 | | - : requestIdleCallback |
57 | | - this.lastCallbackAsAnimationFrame = this.props.updateOnAnimationFrame |
58 | | - |
59 | | - this.animationId = requestCallback(() => this.startTracking()) |
60 | | - this.measure() |
61 | | - } |
62 | | - |
63 | | - measure() { |
64 | | - this.framesSinceLastUpdate += 1 |
65 | | - let duration = Date.now() - this.lastUpdated |
| 54 | + const duration = Date.now() - lastUpdated.current |
66 | 55 | if (duration >= 1000) { |
67 | | - this.setState({ |
68 | | - fps: this.framesSinceLastUpdate, |
69 | | - }) |
70 | | - this.framesSinceLastUpdate = 0 |
71 | | - this.lastUpdated = Date.now() |
| 56 | + setFps(framesSinceLastUpdate.current) |
| 57 | + |
| 58 | + framesSinceLastUpdate.current = 0 |
| 59 | + lastUpdated.current = Date.now() |
72 | 60 | } |
73 | | - } |
| 61 | + }, []) |
74 | 62 |
|
75 | | - componentDidMount() { |
76 | | - this.startTracking() |
77 | | - } |
| 63 | + useWatcher(measure, { updateOnAnimationFrame }) |
78 | 64 |
|
79 | | - render() { |
80 | | - return <div>FPS: {this.state.fps}</div> |
81 | | - } |
| 65 | + return <div>FPS: {fps}</div> |
82 | 66 | } |
| 67 | + |
83 | 68 | function PositionAlignOverview() { |
84 | 69 | const [updateOnAnimationFrame, setUpdateOnAnimationFrame] = useState(false) |
85 | 70 | const [inline, setInline] = useState(false) |
|
0 commit comments