-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(react): reduce unnecessary promise creation
- Loading branch information
Showing
10 changed files
with
121 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@reactive-dot/react": patch | ||
--- | ||
|
||
Reduced unnecessary promise creation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { maybePromiseAll } from "./maybe-promise-all.js"; | ||
import { expect, it } from "vitest"; | ||
|
||
it("returns array as is when no promises are present", () => { | ||
const input = [1, 2, 3, "string", {}, []]; | ||
const result = maybePromiseAll(input); | ||
|
||
expect(result).toBe(input); | ||
}); | ||
|
||
it("returns Promise.all result when all items are promises", async () => { | ||
const input = [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)]; | ||
const result = maybePromiseAll(input); | ||
|
||
expect(result).toBeInstanceOf(Promise); | ||
await expect(result).resolves.toEqual([1, 2, 3]); | ||
}); | ||
|
||
it("returns Promise.all result when some items are promises", async () => { | ||
const input = [1, Promise.resolve(2), 3]; | ||
const result = maybePromiseAll(input); | ||
|
||
expect(result).toBeInstanceOf(Promise); | ||
await expect(result).resolves.toEqual([1, 2, 3]); | ||
}); | ||
|
||
it("handles an empty array", () => { | ||
const input: unknown[] = []; | ||
const result = maybePromiseAll(input); | ||
|
||
expect(result).toBe(input); | ||
}); | ||
|
||
it("handles array with undefined/null values", () => { | ||
const input = [undefined, null]; | ||
const result = maybePromiseAll(input); | ||
|
||
expect(result).toBe(input); | ||
}); | ||
|
||
it("propagates rejection when a promise rejects", async () => { | ||
const error = new Error("Test error"); | ||
const input = [Promise.resolve(1), Promise.reject(error), Promise.resolve(3)]; | ||
const result = maybePromiseAll(input); | ||
|
||
await expect(result).rejects.toBe(error); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export function maybePromiseAll<T extends unknown | Promise<unknown>>( | ||
maybePromises: T[], | ||
) { | ||
return maybePromises.some((maybePromise) => maybePromise instanceof Promise) | ||
? Promise.all(maybePromises) | ||
: maybePromises; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters