Skip to content

Commit 8ade443

Browse files
authored
Tweak README + Add further test (#2)
* Tweak README + Add further test * Tweak * Tweak test * Tweak * Tweak
1 parent f9e3f68 commit 8ade443

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ npm i @phntms/use-animation-frame
1616

1717
## API
1818

19-
Accepts first `callback` and second optional `framesPerSecond`:
19+
Accepts first `callback` and second `framesPerSecond`:
2020

2121
```ts
2222
useAnimationFrame(callback, framesPerSecond);
@@ -32,11 +32,11 @@ useAnimationFrame(callback, framesPerSecond);
3232

3333
The callback returns `deltaTime` - the total time in `ms` since the hook last run.
3434

35-
**Note**: Due to the nature of `requestAnimationFrame()` this will always likely slightly differ in time.
35+
**Note**: Due to the nature of `requestAnimationFrame()` this will always likely differ in time.
3636

37-
## Single Shared `requestAnimationFrame()` Instances
37+
## Single Shared `requestAnimationFrame()` Instance
3838

39-
A highlight of this library over similar implementations is that instead of creating a new `requestAnimationFrame()` event listener for every instances of the hook, we're instead using one global instances that we 'hook' into for all instances of the hook used. This means you're getting less of a performance hit for using multiple instances of the hook. Additionally if used with custom `fps` limits, you can get much greater performance out of your components!
39+
Most implementations of this hook create unique `requestAnimationFrame()` loops for all instances of the hook. This is fine when you only use the hook once, but if you start running multiple hooks simultaneously this can get expensive. To counteract this issue, this library only ever creates a single global loop that we 'hook' into for all instances of the hook used. As such, you haven't got to worry about the potential performance hit of using multiple instances of the hook. Additionally if used alongside custom `framesPerSecond`, you can get much greater performance out of your components!
4040

4141
## Examples
4242

test/index.test.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,22 @@ describe("The hook", () => {
3535
expect(lastDeltaTime).toEqual(STEP_TIME_INCREMENT * 2);
3636
});
3737

38+
it("should only use a single requestAnimationFrame() if multiple hooks are used", async () => {
39+
const callback = () => null;
40+
41+
renderHook(() => useAnimationFrame(callback));
42+
renderHook(() => useAnimationFrame(callback, 30));
43+
44+
expect(mockRequestAnimationFrame.requestAnimationFrameCount()).toEqual(1);
45+
});
46+
3847
it("should cleanup after unmounting", async () => {
3948
const callback = () => null;
49+
4050
const { unmount } = renderHook(() => useAnimationFrame(callback));
51+
4152
unmount();
53+
4254
expect(mockRequestAnimationFrame.requestAnimationFrameCount()).toEqual(0);
4355
});
4456
});

0 commit comments

Comments
 (0)