applesauce-core 0.0.0-next-20250208161600 → 0.0.0-next-20250210144729
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
import { MonoTypeOperatorFunction } from "rxjs";
|
|
2
|
-
export declare
|
|
2
|
+
export declare class TimeoutError extends Error {
|
|
3
|
+
}
|
|
4
|
+
export declare function simpleTimeout<T extends unknown>(first: number, message?: string): MonoTypeOperatorFunction<T>;
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { throwError, timeout } from "rxjs";
|
|
2
|
+
export class TimeoutError extends Error {
|
|
3
|
+
}
|
|
2
4
|
export function simpleTimeout(first, message) {
|
|
3
|
-
return timeout({ first, with: () => throwError(() => new
|
|
5
|
+
return timeout({ first, with: () => throwError(() => new TimeoutError(message ?? "Timeout")) });
|
|
4
6
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { Observable, Subject, firstValueFrom } from "rxjs";
|
|
3
|
+
import { simpleTimeout, TimeoutError } from "./simple-timeout.js";
|
|
4
|
+
describe("simpleTimeout operator", () => {
|
|
5
|
+
it("should throw TimeoutError after specified timeout period", async () => {
|
|
6
|
+
const subject = new Subject();
|
|
7
|
+
const obs = subject.pipe(simpleTimeout(10));
|
|
8
|
+
const promise = firstValueFrom(obs);
|
|
9
|
+
await expect(promise).rejects.toThrow(TimeoutError);
|
|
10
|
+
await expect(promise).rejects.toThrow("Timeout");
|
|
11
|
+
});
|
|
12
|
+
it("should throw TimeoutError with custom message", async () => {
|
|
13
|
+
const subject = new Subject();
|
|
14
|
+
const customMessage = "Custom timeout message";
|
|
15
|
+
const obs = subject.pipe(simpleTimeout(10, customMessage));
|
|
16
|
+
const promise = firstValueFrom(obs);
|
|
17
|
+
await expect(promise).rejects.toThrow(TimeoutError);
|
|
18
|
+
await expect(promise).rejects.toThrow(customMessage);
|
|
19
|
+
});
|
|
20
|
+
it("should not throw when value emitted before timeout", async () => {
|
|
21
|
+
const subject = new Subject();
|
|
22
|
+
const obs = subject.pipe(simpleTimeout(1000));
|
|
23
|
+
const promise = firstValueFrom(obs);
|
|
24
|
+
subject.next("test value");
|
|
25
|
+
await expect(promise).resolves.toBe("test value");
|
|
26
|
+
});
|
|
27
|
+
it("should complete without error when source emits non-null value before timeout", async () => {
|
|
28
|
+
const source = new Observable((subscriber) => {
|
|
29
|
+
subscriber.next("test value");
|
|
30
|
+
});
|
|
31
|
+
const result = await firstValueFrom(source.pipe(simpleTimeout(10)));
|
|
32
|
+
expect(result).toBe("test value");
|
|
33
|
+
});
|
|
34
|
+
});
|