Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
chore: housekeeping: remove some promise callbacks (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT authored Oct 7, 2023
1 parent 0a4e641 commit 9d6969b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 32 deletions.
11 changes: 5 additions & 6 deletions examples/async/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import { tsonOptions } from "./shared.js";

const tsonStringifyAsync = createTsonStringifyAsync(tsonOptions);

const randomNumber = (min: number, max: number) => {
return Math.floor(Math.random() * (max - min + 1) + min);
};
const randomNumber = (min: number, max: number) =>
Math.floor(Math.random() * (max - min + 1) + min);

const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

/**
* This function returns the object we will be sending to the client.
Expand All @@ -18,15 +17,15 @@ export function getResponseShape() {
async function* bigintGenerator() {
const iterate = new Array(10).fill(0).map((_, i) => BigInt(i));
for (const number of iterate) {
await wait(randomNumber(1, 400));
await sleep(randomNumber(1, 400));
yield number;
}
}

async function* numberGenerator() {
const iterate = new Array(10).fill(0).map((_, i) => i);
for (const number of iterate) {
await wait(randomNumber(1, 400));
await sleep(randomNumber(1, 400));
yield number;
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/async/deserializeAsync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test("deserialize variable chunk length", async () => {
});
{
const iterable = (async function* () {
await new Promise((resolve) => setTimeout(resolve, 1));
await sleep(1);
yield '[\n{"json":{"foo":"bar"},"nonce":"__tson"}';
yield "\n,\n[\n]\n]";
})();
Expand All @@ -37,7 +37,7 @@ test("deserialize variable chunk length", async () => {

{
const iterable = (async function* () {
await new Promise((resolve) => setTimeout(resolve, 1));
await sleep(1);
yield '[\n{"json":{"foo":"bar"},"nonce":"__tson"}\n,\n[\n]\n]';
})();
const result = await tson.parse(iterable);
Expand All @@ -46,7 +46,7 @@ test("deserialize variable chunk length", async () => {

{
const iterable = (async function* () {
await new Promise((resolve) => setTimeout(resolve, 1));
await sleep(1);
yield '[\n{"json"';
yield ':{"foo":"b';
yield 'ar"},"nonce":"__tson"}\n,\n';
Expand Down Expand Up @@ -100,13 +100,13 @@ test("stringify async iterable + promise", async () => {
});

async function* iterable() {
await new Promise((resolve) => setTimeout(resolve, 1));
await sleep(1);
yield 1n;
await new Promise((resolve) => setTimeout(resolve, 1));
await sleep(1);
yield 2n;
yield 3n;

await new Promise((resolve) => setTimeout(resolve, 2));
await sleep(1);
yield 4n;
yield 5n;
}
Expand Down Expand Up @@ -138,7 +138,7 @@ test("e2e: stringify async iterable and promise over the network", async () => {
function createMockObj() {
async function* generator() {
for (const number of [1n, 2n, 3n, 4n, 5n]) {
await new Promise((resolve) => setTimeout(resolve, 1));
await sleep(1);
yield number;
}
}
Expand Down Expand Up @@ -234,7 +234,7 @@ test("iterator error", async () => {
for (let index = 0; index < 3; index++) {
yield `item: ${index}`;

await new Promise((resolve) => setTimeout(resolve, 1));
await sleep(1);
}

// resolve the deferred after crash
Expand Down
14 changes: 1 addition & 13 deletions src/async/handlers/tsonPromise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,14 @@ import {
sleep,
waitError,
} from "../../internals/testUtils.js";
import { createPromise } from "../../internals/testUtils.js";
import { createTsonParseAsyncInner } from "../deserializeAsync.js";
import {
mapIterable,
readableStreamToAsyncIterable,
} from "../iterableUtils.js";
import { TsonAsyncValueTuple } from "../serializeAsync.js";

const createPromise = <T>(result: () => T, wait = 1) => {
return new Promise<T>((resolve, reject) => {
setTimeout(() => {
try {
const res = result();
resolve(res);
} catch (err) {
reject(err);
}
}, wait);
});
};

const tsonError: TsonType<
Error,
{
Expand Down
7 changes: 4 additions & 3 deletions src/async/serializeAsync.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, test } from "vitest";

import { tsonAsyncIterator, tsonBigint, tsonPromise } from "../index.js";
import { sleep } from "../internals/testUtils.js";
import {
createAsyncTsonSerialize,
createTsonStringifyAsync,
Expand Down Expand Up @@ -106,9 +107,9 @@ test("serialize async iterable", async () => {
});

async function* iterable() {
await new Promise((resolve) => setTimeout(resolve, 1));
await sleep(1);
yield 42;
await new Promise((resolve) => setTimeout(resolve, 1));
await sleep(1);
yield 43;
}

Expand Down Expand Up @@ -163,7 +164,7 @@ test("stringify async iterable + promise", async () => {
});

async function* iterable() {
await new Promise((resolve) => setTimeout(resolve, 1));
await sleep(1);
yield 1n;
yield 2n;
}
Expand Down
15 changes: 13 additions & 2 deletions src/internals/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ export const waitFor = async (fn: () => unknown) => {
await fn();
return;
} catch {
// wait 5ms
await new Promise((resolve) => setTimeout(resolve, 5));
await sleep(5);
}
}
};
Expand Down Expand Up @@ -90,3 +89,15 @@ export function createDeferred<T>() {

export const sleep = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));
export const createPromise = <T>(result: () => T, wait = 1) => {
return new Promise<T>((resolve, reject) => {
setTimeout(() => {
try {
const res = result();
resolve(res);
} catch (err) {
reject(err);
}
}, wait);
});
};

0 comments on commit 9d6969b

Please sign in to comment.