Skip to content

Commit

Permalink
Better docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
iwoplaza committed Jul 7, 2024
1 parent a067d83 commit 5f515b7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/derive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ type AwaitAtomsValues<
[Index in keyof TTuple]: Awaited<ExtractAtomValue<TTuple[Index]>>;
};

/**
* Awaits all `deps` if necessary, then runs `op` given all deps in the same order.
* If computing the value fails (throws), a rejected Promise is returned no matter if
* the processing happened synchronously or not.
*/
export function derive<
TDeps extends readonly [Atom<unknown>, ...Atom<unknown>[]],
TValue,
Expand Down
13 changes: 9 additions & 4 deletions src/soonAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ function isKnown<T, S>(value: ExtraPromise<T> | S): boolean {
return true; // not a promise, we know the value.
}

/**
* Given array `values`, if all elements are known (are not unresolved promises),
* returns an array of the same length with Awaited `values`. Otherwise, it returns a
* promise to that array.
*/
export function soonAll<T extends readonly [unknown, ...unknown[]]>(
input: T,
values: T,
): SoonAll<T> {
if (input.every(isKnown)) {
return input.map((el) =>
if (values.every(isKnown)) {
return values.map((el) =>
isPromise(el) ? el.value : el,
) as unknown as SoonAll<T>;
}

return Promise.all(input);
return Promise.all(values);
}

0 comments on commit 5f515b7

Please sign in to comment.