Skip to content

Commit

Permalink
Rename to useLatestCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
littensy committed Apr 23, 2023
1 parent 5221a21 commit 97ee874
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export * from "./use-event-listener";
export * from "./use-interval";
export * from "./use-key-press";
export * from "./use-latest";
export * from "./use-latest-callback";
export * from "./use-lifetime";
export * from "./use-memoized-callback";
export * from "./use-mount-effect";
export * from "./use-mouse";
export * from "./use-previous";
Expand Down
4 changes: 2 additions & 2 deletions src/use-binding-listener/use-binding-listener.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Binding } from "@rbxts/roact";
import { useEffect, useMemo } from "@rbxts/roact-hooked";
import { useMemoizedCallback } from "../use-memoized-callback";
import { useLatestCallback } from "../use-latest-callback";
import { getBindingApi, isBinding } from "../utils/binding";

/**
Expand All @@ -19,7 +19,7 @@ export function useBindingListener<T>(binding: T | Binding<T>, listener: (value:
return isBinding<T>(binding) ? getBindingApi(binding) : undefined;
}, [binding]);

const listenerCallback = useMemoizedCallback(listener);
const listenerCallback = useLatestCallback(listener);

useEffect(() => {
if (api) {
Expand Down
4 changes: 2 additions & 2 deletions src/use-interval/use-interval.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useMutable } from "@rbxts/roact-hooked";
import { setInterval } from "@rbxts/set-timeout";
import { useMemoizedCallback } from "../use-memoized-callback";
import { useLatestCallback } from "../use-latest-callback";

export interface UseIntervalOptions {
/**
Expand All @@ -22,7 +22,7 @@ export interface UseIntervalOptions {
export function useInterval(callback: () => void, delay?: number, options: UseIntervalOptions = {}) {
const { immediate = false } = options;

const callbackMemo = useMemoizedCallback(callback);
const callbackMemo = useLatestCallback(callback);
const cancel = useMutable<() => void>();

const clear = useCallback(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## 🪝 `useMemoizedCallback`
## 🪝 `useLatestCallback`

```ts
function useMemoizedCallback<T extends Callback>(callback: T): T;
function useLatestCallback<T extends Callback>(callback: T): T;
```

Returns a memoized callback that always points to the latest version of the callback.
Expand All @@ -24,7 +24,7 @@ interface Props {
}

function Stepper({ onStep }: Props) {
const onStepCallback = useMomoizedCallback(onStep);
const onStepCallback = useLatestCallback(onStep);

useEffect(() => {
// Will always call the latest version of `onStep`
Expand Down
1 change: 1 addition & 0 deletions src/use-latest-callback/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./use-latest-callback";
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
/// <reference types="@rbxts/testez/globals" />

import { renderHook } from "../utils/testez";
import { useMemoizedCallback } from "./use-memoized-callback";
import { useLatestCallback } from "./use-latest-callback";

export = () => {
it("should memoize the callback on mount", () => {
const callback = () => {};
const { result } = renderHook(() => useMemoizedCallback(callback));
const { result } = renderHook(() => useLatestCallback(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 { result, rerender } = renderHook(() => useLatestCallback(() => {}));
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), {
const { result, rerender } = renderHook(({ callback }) => useLatestCallback(callback), {
initialProps: { callback: () => {} },
});
const memoizedCallback = result.current;
Expand All @@ -29,8 +29,8 @@ export = () => {
expect(result.current).to.equal(memoizedCallback);
});

it("should memoize the passed callback", () => {
const { result, rerender } = renderHook(({ callback }) => useMemoizedCallback(callback), {
it("should memoize new callbacks", () => {
const { result, rerender } = renderHook(({ callback }) => useLatestCallback(callback), {
initialProps: {
callback: (a: number, b: number) => a + b,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useCallback, useMutable } from "@rbxts/roact-hooked";
* @param callback The callback to memoize.
* @returns The memoized callback.
*/
export function useMemoizedCallback<T extends Callback>(callback: T): T {
export function useLatestCallback<T extends Callback>(callback: T): T {
const callbackRef = useMutable(callback);
callbackRef.current = callback;

Expand Down
1 change: 0 additions & 1 deletion src/use-memoized-callback/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/use-timeout/use-timeout.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useMutable } from "@rbxts/roact-hooked";
import { setTimeout } from "@rbxts/set-timeout";
import { useMemoizedCallback } from "../use-memoized-callback";
import { useLatestCallback } from "../use-latest-callback";

/**
* Sets a timeout that runs the callback function after `delay` seconds. If
Expand All @@ -11,7 +11,7 @@ import { useMemoizedCallback } from "../use-memoized-callback";
* @returns A function that clears the timeout.
*/
export function useTimeout(callback: () => void, delay?: number) {
const callbackMemo = useMemoizedCallback(callback);
const callbackMemo = useLatestCallback(callback);
const cancel = useMutable<() => void>();

const clear = useCallback(() => {
Expand Down

0 comments on commit 97ee874

Please sign in to comment.