Skip to content

Commit

Permalink
fix(tools-shell): allow once functions to take arguments (#3167)
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 authored May 30, 2024
1 parent 0f4868a commit 9ddb5f8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-rabbits-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rnx-kit/tools-shell": patch
---

Allow `once` functions to take arguments
12 changes: 7 additions & 5 deletions packages/tools-shell/src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ export function idle(ms: number): Promise<void> {
/**
* Wraps the function, making sure it only gets called once.
*/
export function once<R>(func: () => R): () => R {
let result: R | undefined;
return () => {
if (!result) {
result = func();
export function once<R>(
func: (...args: unknown[]) => NonNullable<R>
): (...args: unknown[]) => NonNullable<R> {
let result: NonNullable<R>;
return (...args) => {
if (result != null) {
result = func(...args);
}
return result;
};
Expand Down

0 comments on commit 9ddb5f8

Please sign in to comment.