From 9ddb5f8de301a7d330b622ce4c53dec42c646739 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Thu, 30 May 2024 08:39:01 +0200 Subject: [PATCH] fix(tools-shell): allow `once` functions to take arguments (#3167) --- .changeset/spicy-rabbits-smash.md | 5 +++++ packages/tools-shell/src/async.ts | 12 +++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 .changeset/spicy-rabbits-smash.md diff --git a/.changeset/spicy-rabbits-smash.md b/.changeset/spicy-rabbits-smash.md new file mode 100644 index 000000000..3ec9b0bb3 --- /dev/null +++ b/.changeset/spicy-rabbits-smash.md @@ -0,0 +1,5 @@ +--- +"@rnx-kit/tools-shell": patch +--- + +Allow `once` functions to take arguments diff --git a/packages/tools-shell/src/async.ts b/packages/tools-shell/src/async.ts index 84a21fb09..fff08973a 100644 --- a/packages/tools-shell/src/async.ts +++ b/packages/tools-shell/src/async.ts @@ -8,11 +8,13 @@ export function idle(ms: number): Promise { /** * Wraps the function, making sure it only gets called once. */ -export function once(func: () => R): () => R { - let result: R | undefined; - return () => { - if (!result) { - result = func(); +export function once( + func: (...args: unknown[]) => NonNullable +): (...args: unknown[]) => NonNullable { + let result: NonNullable; + return (...args) => { + if (result != null) { + result = func(...args); } return result; };