-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
112 additions
and
5 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
## 🪝 `useMemoizedCallback` | ||
|
||
```ts | ||
function useMemoizedCallback<T extends Callback>(callback: T): T; | ||
``` | ||
|
||
Returns a memoized callback. When passed a new callback, the memoized callback will not change, but calling it will invoke the new callback. | ||
|
||
### 📕 Parameters | ||
|
||
- `callback` - The callback to memoize. | ||
|
||
### 📗 Returns | ||
|
||
- The memoized callback. | ||
|
||
### 📘 Example | ||
|
||
```tsx | ||
interface Props { | ||
onStep: () => void; | ||
} | ||
|
||
export default function Component({ onStep }: Props) { | ||
const onStepCallback = useMomoizedCallback(onStep); | ||
|
||
useEffect(() => { | ||
const connection = RunService.Heartbeat.Connect(onStepCallback); | ||
|
||
return () => { | ||
connection.Disconnect(); | ||
}; | ||
}, [onStepCallback]); | ||
|
||
return <frame />; | ||
} | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./use-memoized-callback"; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/// <reference types="@rbxts/testez/globals" /> | ||
|
||
import { renderHook } from "../utils/testez"; | ||
import { useMemoizedCallback } from "./use-memoized-callback"; | ||
|
||
export = () => { | ||
it("should memoize the callback on mount", () => { | ||
const callback = () => {}; | ||
const { result } = renderHook(() => useMemoizedCallback(callback)); | ||
expect(result.current).to.be.a("function"); | ||
expect(result.current).never.to.equal(callback); | ||
}); | ||
|
||
it("should return memoized callback after unrelated rerender", () => { | ||
const { result, rerender } = renderHook(() => useMemoizedCallback(() => {})); | ||
const memoizedCallback = result.current; | ||
rerender(); | ||
expect(result.current).to.equal(memoizedCallback); | ||
}); | ||
|
||
it("should return memoized callback after passed callback changes", () => { | ||
const { result, rerender } = renderHook(({ callback }) => useMemoizedCallback(callback), { | ||
initialProps: { callback: () => {} }, | ||
}); | ||
const memoizedCallback = result.current; | ||
rerender({ callback: () => {} }); | ||
expect(result.current).to.equal(memoizedCallback); | ||
rerender({ callback: () => {} }); | ||
expect(result.current).to.equal(memoizedCallback); | ||
}); | ||
|
||
it("should memoize the passed callback", () => { | ||
const { result, rerender } = renderHook(({ callback }) => useMemoizedCallback(callback), { | ||
initialProps: { | ||
callback: (a: number, b: number) => a + b, | ||
}, | ||
}); | ||
const memoizedCallback = result.current; | ||
expect(memoizedCallback(1, 2)).to.equal(3); | ||
rerender({ callback: (a: number, b: number) => a - b }); | ||
expect(memoizedCallback(1, 2)).to.equal(-1); | ||
}); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useCallback, useMutable } from "@rbxts/roact-hooked"; | ||
|
||
export function useMemoizedCallback<T extends Callback>(callback: T): T; | ||
export function useMemoizedCallback(callback: Callback): Callback { | ||
const callbackRef = useMutable(callback); | ||
callbackRef.current = callback; | ||
|
||
return useCallback((...args) => { | ||
return callbackRef.current(...args); | ||
}, []); | ||
} |
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