Skip to content

Commit

Permalink
feat(utils): replace newPromiseWithResolvers with `createManualProm…
Browse files Browse the repository at this point in the history
…ise`
  • Loading branch information
hi-ogawa committed Dec 13, 2023
1 parent ad44599 commit 9568bde
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
8 changes: 4 additions & 4 deletions packages/tiny-rpc/src/adapter-message-port.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
type Result,
newPromiseWithResolvers,
createManualPromise,
wrapErrorAsync,
} from "@hiogawa/utils";
import {
Expand Down Expand Up @@ -72,16 +72,16 @@ export function messagePortClientAdapter({
id: generateId(),
data,
};
const promiseResolvers = newPromiseWithResolvers<ResponsePayload>();
const responsePromise = createManualPromise<ResponsePayload>();
const unlisten = listen(port, (ev) => {
const res = ev.data as ResponsePayload;
if (res.id === req.id) {
promiseResolvers.resolve(res);
responsePromise.resolve(res);
unlisten();
}
});
port.postMessage(req);
const res = await promiseResolvers.promise;
const res = await responsePromise;
if (!res.result.ok) {
throw TinyRpcError.deserialize(res.result.value);
}
Expand Down
12 changes: 6 additions & 6 deletions packages/utils/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
typedBoolean,
} from "./misc";
import { mapOption, none } from "./option";
import { newPromiseWithResolvers } from "./promise";
import { createManualPromise } from "./promise";
import { escapeRegExp, mapRegExp, regExpRaw } from "./regexp";
import {
Err,
Expand Down Expand Up @@ -509,10 +509,10 @@ describe(none, () => {
});
});

describe(newPromiseWithResolvers, () => {
describe(createManualPromise, () => {
it("resolve", async () => {
const { promise, resolve } = newPromiseWithResolvers<number>();
resolve(123);
const promise = createManualPromise<number>();
promise.resolve(123);
const result = await wrapErrorAsync(() => promise);
expect(result).toMatchInlineSnapshot(`
{
Expand All @@ -523,8 +523,8 @@ describe(newPromiseWithResolvers, () => {
});

it("reject", async () => {
const { promise, reject } = newPromiseWithResolvers<number>();
reject(123);
const promise = createManualPromise<number>();
promise.reject(123);
const result = await wrapErrorAsync(() => promise);
expect(result).toMatchInlineSnapshot(`
{
Expand Down
22 changes: 21 additions & 1 deletion packages/utils/src/promise.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
// TODO: how is it different from this? https://github.com/remix-run/remix/blob/1d3d86eaceb7784c56d34d963310ecbcb8c9637a/packages/remix-dev/channel.ts#L1-L2
// inspired by
// ManualPromise https://github.com/microsoft/playwright/blob/10dda30c7f417a92f782e21368fbb211a679838a/packages/playwright-core/src/utils/manualPromise.ts#L31-L38
// createDefer https://github.com/vitest-dev/vitest/blob/9c552b6f8decb78677b20e870eb430184e0b78ea/packages/utils/src/helpers.ts#L155
// channel https://github.com/remix-run/remix/blob/9a845b6576fbaf112161f6f97295f6dbb44d913f/packages/remix-dev/channel.ts#L1-L2
interface ManualPromise<T> extends PromiseLike<T> {
promise: Promise<T>;
resolve: (value: T | PromiseLike<T>) => void;
reject: (value: unknown) => void;
}

export function createManualPromise<T>(): ManualPromise<T> {
let resolve!: ManualPromise<T>["resolve"];
let reject!: ManualPromise<T>["reject"];
const promise = new Promise<T>((resolve_, reject_) => {
resolve = resolve_;
reject = reject_;
});
return { promise, resolve, reject, then: promise.then.bind(promise) };
}

/** @deprecated use createManualPromise instead */
export function newPromiseWithResolvers<T>() {
let resolve!: (value: T | PromiseLike<T>) => void;
let reject!: (value: unknown) => void;
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function wrapError<T>(getValue: () => T): Result<T, unknown> {
}

export async function wrapErrorAsync<T>(
getValue: () => Promise<T>
getValue: () => PromiseLike<T>
): Promise<Result<T, unknown>> {
try {
return Ok(await getValue());
Expand All @@ -36,7 +36,7 @@ export function okToOption<T>(result: Result<T, unknown>): T | undefined {
* @deprecated use `wrapErrorAsync` instead
*/
export async function wrapPromise<T>(
value: Promise<T>
value: PromiseLike<T>
): Promise<Result<T, unknown>> {
try {
return Ok(await value);
Expand Down

0 comments on commit 9568bde

Please sign in to comment.