Skip to content

Commit 987e610

Browse files
committed
feat: add dependencies argument to reset the value
1 parent 691bf87 commit 987e610

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

src/use-lifetime/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
## 🪝 `useLifetime`
22

33
```ts
4-
function useLifetime(): Roact.Binding<number>;
4+
function useLifetime(dependencies?: unknown[]): Roact.Binding<number>;
55
```
66

77
Returns the amount of time that has passed since the component mounted. The binding is updated every frame on Heartbeat.
88

9+
If dependencies are provided, the binding will reset to `0` whenever any of the dependencies change.
10+
911
Useful for mapping time to procedural animations and other time-based effects.
1012

1113
### 📕 Parameters
1214

15+
- `dependencies` - An optional array of dependencies. If provided, the binding will reset to `0` whenever any of the dependencies change.
16+
1317
### 📗 Returns
1418

1519
- A binding that will update with the amount of time that has passed since the component mounted.

src/use-lifetime/use-lifetime.spec.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,27 @@ export = () => {
88
const { result, unmount } = renderHook(() => useLifetime());
99

1010
expect(result.current.getValue()).to.equal(0);
11+
12+
const timePassed = task.wait(0.1);
13+
expect(result.current.getValue()).to.be.near(timePassed, 0.05);
14+
15+
unmount();
16+
});
17+
18+
it("should reset when dependencies change", () => {
19+
const { result, rerender, unmount } = renderHook((props: { value: number }) => useLifetime([props.value]), {
20+
initialProps: { value: 0 },
21+
});
22+
23+
expect(result.current.getValue()).to.equal(0);
24+
1125
const timePassed = task.wait(0.1);
12-
expect(result.current.getValue()).to.be.near(timePassed, 0.03);
26+
rerender({ value: 0 });
27+
expect(result.current.getValue()).to.be.near(timePassed, 0.05);
28+
29+
rerender({ value: 1 });
30+
expect(result.current.getValue()).to.equal(0);
31+
1332
unmount();
1433
});
1534
};

src/use-lifetime/use-lifetime.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1-
import { useBinding } from "@rbxts/roact-hooked";
1+
import { useBinding, useEffect } from "@rbxts/roact-hooked";
22
import { RunService } from "@rbxts/services";
33
import { useEventListener } from "../use-event-listener";
44

55
/**
66
* Returns the lifetime of the component in seconds. Updates every frame on
77
* the Heartbeat event.
8+
*
9+
* If the dependency array is provided, the lifetime timer will reset when
10+
* any of the dependencies change.
11+
*
12+
* @param dependencies An optional array of dependencies to reset the timer.
813
* @returns A binding of the component's lifetime.
914
*/
10-
export function useLifetime() {
15+
export function useLifetime(dependencies: unknown[] = []) {
1116
const [lifetime, setLifetime] = useBinding(0);
1217

1318
useEventListener(RunService.Heartbeat, (deltaTime) => {
1419
setLifetime(lifetime.getValue() + deltaTime);
1520
});
1621

22+
useEffect(() => {
23+
setLifetime(0);
24+
}, dependencies);
25+
1726
return lifetime;
1827
}

0 commit comments

Comments
 (0)