Skip to content

Commit

Permalink
migrate and deprecate unknown methods to exception
Browse files Browse the repository at this point in the history
  • Loading branch information
andogq committed Jul 15, 2024
1 parent 01a13d1 commit a1d0d48
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async function normalisePromise<T>(value: MaybePromise<T>): Promise<T> {

/**
* Run the given handler, then the returned value will be normalised as an atom and returned. If an
* unhandled error is thrown during the handler, then it will be caught and returned as an `unknown`
* unhandled error is thrown during the handler, then it will be caught and returned as an `exception`
* atom.
*/
export async function handler<T, E>(
Expand All @@ -43,7 +43,7 @@ export async function handler<T, E>(

/**
* Run some callback. If it completes successfully, the value will be returned as `AtomOk`. If an
* error is thrown, it will be caught and returned as an `AtomUnknown`. `AtomError` will never be
* error is thrown, it will be caught and returned as an `AtomException`. `AtomError` will never be
* produced from this helper.
*/
export async function run<T>(
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ export type {
Atom,
AtomOk,
AtomError,
AtomException as AtomUnknown,
AtomException,
AtomUnknown,
VALUE,
ERROR,
EXCEPTION as UNKNOWN,
EXCEPTION,
UNKNOWN,
MaybeAtom,
} from "./atom";

Expand Down
14 changes: 11 additions & 3 deletions src/stream/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class StreamBase {
this.push(normalise(value));
}
} catch (e) {
// Promise was rejected, add as an unknown error
// Promise was rejected, add as an exception
this.push(exception(e, []));
}
},
Expand Down Expand Up @@ -250,14 +250,22 @@ export class StreamBase {
}

/**
* Create a new stream containing a single unknown atom.
* Create a new stream containing a single exception atom.
*
* @group Creation
*/
static ofUnknown<T, E>(value: unknown): Stream<T, E> {
static ofException<T, E>(value: unknown): Stream<T, E> {
return this.of(exception(value, []));
}

/**
* @group Creation
* @deprecated use `ofException` instead
*/
static ofUnknown<T, E>(value: unknown): Stream<T, E> {
return this.ofException(value);
}

/**
* Create a stream and corresponding writable Node stream, where any writes to the writable
* Node stream will be emitted on the returned stream.
Expand Down
20 changes: 15 additions & 5 deletions src/stream/higher-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ export class HigherOrderStream<T, E> extends StreamTransforms<T, E> {
}

/**
* Maps over each unknown error in the stream, producing a new stream from it, and flatten all
* Maps over each exception in the stream, producing a new stream from it, and flatten all
* the value streams together.
*
* @group Higher Order
*/
flatMapUnknown(
flatMapException(
cb: (value: unknown, trace: string[]) => MaybePromise<Stream<T, E>>,
): Stream<T, E> {
const trace = this.trace("flatMapUnknown");
const trace = this.trace("flatMapException");

return this.flatMapAtom(
(atom) => (isException(atom) ? accept(atom) : reject(atom)),
Expand All @@ -125,6 +125,16 @@ export class HigherOrderStream<T, E> extends StreamTransforms<T, E> {
);
}

/**
* @group Higher Order
* @deprecated use `flatMapException` instead
*/
flatMapUnknown(
cb: (value: unknown, trace: string[]) => MaybePromise<Stream<T, E>>,
): Stream<T, E> {
return this.flatMapException(cb);
}

/**
* Map over each value in the stream, produce a stream from it, cache the resultant stream
* and flatten all the value streams together
Expand Down Expand Up @@ -235,7 +245,7 @@ export class HigherOrderStream<T, E> extends StreamTransforms<T, E> {
/**
* Emit items from provided stream if this stream is completely empty.
*
* @note If there are any errors (known or unknown) on the stream, then the new stream won't be
* @note If there are any errors or exceptions on the stream, then the new stream won't be
* consumed.
*
* @group Higher Order
Expand All @@ -262,7 +272,7 @@ export class HigherOrderStream<T, E> extends StreamTransforms<T, E> {

/**
* Consume the entire stream, and completely replace it with a new stream. This will remove
* any errors currently on the stream (both known and unknown).
* any errors and exceptions currently on the stream.
*
* Equivalent to:
*
Expand Down
24 changes: 20 additions & 4 deletions src/stream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,22 @@ export class Stream<T, E> extends HigherOrderStream<T, E> {
}

/**
* Create an `unknown` atom with the provided value.
* Create an `exception` atom with the provided value.
*
* @group Atom
*/
static unknown<T, E>(value: unknown, trace: string[]): Atom<T, E> {
static exception<T, E>(value: unknown, trace: string[]): Atom<T, E> {
return exception(value, trace);
}

/**
* @group Atom
* @deprecated use `exception` instead
*/
static unknown<T, E>(value: unknown, trace: string[]): Atom<T, E> {
return this.exception(value, trace);
}

/**
* Verify if the provided atom is of the `ok` variant.
*
Expand All @@ -66,11 +74,19 @@ export class Stream<T, E> extends HigherOrderStream<T, E> {
}

/**
* Verify if the provided atom is of the `unknown` variant.
* Verify if the provided atom is of the `exception` variant.
*
* @group Atom
*/
static isUnknown<T, E>(atom: Atom<T, E>): atom is AtomException {
static isException<T, E>(atom: Atom<T, E>): atom is AtomException {
return isException(atom);
}

/**
* @group Atom
* @deprecated use `isException` instead
*/
static isUnknown<T, E>(atom: Atom<T, E>): atom is AtomException {
return this.isException(atom);
}
}
14 changes: 11 additions & 3 deletions src/stream/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ export class StreamTransforms<T, E> extends StreamConsumption<T, E> {
}

/**
* Map over each unknown in the stream.
* Map over each exception in the stream.
*
* @group Transform
*/
mapUnknown(cb: (error: unknown) => MaybePromise<MaybeAtom<T, E>>): Stream<T, E> {
const trace = this.trace("mapUnknown");
mapException(cb: (error: unknown) => MaybePromise<MaybeAtom<T, E>>): Stream<T, E> {
const trace = this.trace("mapException");

return this.consume(async function* (it) {
for await (const atom of it) {
Expand All @@ -103,6 +103,14 @@ export class StreamTransforms<T, E> extends StreamConsumption<T, E> {
});
}

/**
* @group Transform
* @deprecated use `mapException` instead
*/
mapUnknown(cb: (error: unknown) => MaybePromise<MaybeAtom<T, E>>): Stream<T, E> {
return this.mapException(cb);
}

/**
* Run a callback for each value in the stream, ideal for side effects on stream items.
*
Expand Down
8 changes: 4 additions & 4 deletions test/consumption.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe.concurrent("stream consumption", () => {
$.error("known"),
2,
3,
$.unknown("$.error", []),
$.exception("$.error", []),
]).toArray();

expect(array).toEqual([1, 2, 3]);
Expand Down Expand Up @@ -50,7 +50,7 @@ describe.concurrent("stream consumption", () => {
$.error("known"),
2,
3,
$.unknown("$.error", []),
$.exception("$.error", []),
]).toArray({
atoms: true,
});
Expand All @@ -60,7 +60,7 @@ describe.concurrent("stream consumption", () => {
$.error("known"),
$.ok(2),
$.ok(3),
$.unknown("$.error", []),
$.exception("$.error", []),
]);
});

Expand Down Expand Up @@ -184,7 +184,7 @@ describe.concurrent("stream consumption", () => {
$.ok("a"),
$.error("an error"),
$.ok("b"),
$.unknown("unknown error", []),
$.exception("unknown error", []),
$.ok("c"),
]).toReadable("object");

Expand Down
11 changes: 8 additions & 3 deletions test/creation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,12 @@ describe.concurrent("stream creation", () => {
test("next atoms produces atoms", async ({ expect }) => {
expect.assertions(1);

const atoms = [$.ok(0), $.error("some error"), $.ok(1), $.unknown("unknown error", [])];
const atoms = [
$.ok(0),
$.error("some error"),
$.ok(1),
$.exception("unknown error", []),
];
const s = $.fromNext(async () => {
if (atoms.length > 0) {
return atoms.shift();
Expand All @@ -164,7 +169,7 @@ describe.concurrent("stream creation", () => {
$.ok(0),
$.error("some error"),
$.ok(1),
$.unknown("unknown error", []),
$.exception("unknown error", []),
]);
});

Expand All @@ -187,7 +192,7 @@ describe.concurrent("stream creation", () => {
});

expect(await s.toArray({ atoms: true })).toEqual([
$.unknown("some error", []),
$.exception("some error", []),
$.ok(2),
]);
});
Expand Down
8 changes: 4 additions & 4 deletions test/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe.concurrent("error handling", () => {

expect(await s.toArray({ atoms: true })).toEqual([
$.ok(1),
$.unknown(new Error("bad number"), ["map"]),
$.exception(new Error("bad number"), ["map"]),
$.ok(3),
]);
});
Expand All @@ -36,7 +36,7 @@ describe.concurrent("error handling", () => {

expect(await s.toArray({ atoms: true })).toEqual([
$.ok(1),
$.unknown(new Error("bad number"), ["map"]),
$.exception(new Error("bad number"), ["map"]),
$.ok(3),
]);
});
Expand All @@ -56,7 +56,7 @@ describe.concurrent("error handling", () => {
.filter((n) => n % 2 === 0);

expect(await s.toArray({ atoms: true })).toEqual([
$.unknown(new Error("bad number"), ["map"]),
$.exception(new Error("bad number"), ["map"]),
$.ok(4),
]);
});
Expand Down Expand Up @@ -84,7 +84,7 @@ describe.concurrent("error handling", () => {
.filter((n) => n % 2 === 0);

expect(await s.toArray({ atoms: true })).toEqual([
$.unknown(new Error("bad number"), ["filter", "map", "map"]),
$.exception(new Error("bad number"), ["filter", "map", "map"]),
$.ok(30),
$.ok(4),
$.ok(50),
Expand Down
10 changes: 5 additions & 5 deletions test/higher-order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe.concurrent("higher order streams", () => {
$.ok(1),
$.error("known error"),
$.ok(2),
$.unknown("bad error", []),
$.exception("bad error", []),
$.ok(3),
]).flatMap((n) => $.from(new Array(n).fill(n)));

Expand All @@ -34,7 +34,7 @@ describe.concurrent("higher order streams", () => {
$.error("known error"),
$.ok(2),
$.ok(2),
$.unknown("bad error", []),
$.exception("bad error", []),
$.ok(3),
$.ok(3),
$.ok(3),
Expand Down Expand Up @@ -121,9 +121,9 @@ describe.concurrent("higher order streams", () => {
test("stream with unknown error", async ({ expect }) => {
expect.assertions(1);

const s = $.from([$.unknown("some error", [])]).otherwise($.from([1]));
const s = $.from([$.exception("some error", [])]).otherwise($.from([1]));

expect(await s.toArray({ atoms: true })).toEqual([$.unknown("some error", [])]);
expect(await s.toArray({ atoms: true })).toEqual([$.exception("some error", [])]);
});
});

Expand Down Expand Up @@ -198,7 +198,7 @@ describe.concurrent("higher order streams", () => {

expect(await s.toArray({ atoms: true })).toEqual([
$.ok(20),
$.unknown("Cannot divide by zero!", ["cachedFlatMap"]),
$.exception("Cannot divide by zero!", ["cachedFlatMap"]),
$.ok(2),
$.ok(20),
$.ok(20),
Expand Down
10 changes: 6 additions & 4 deletions test/transforms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ describe("stream transforms", () => {
test("single unknown", async ({ expect }) => {
expect.assertions(1);

const s = $.from([$.unknown(1, []), $.ok(2), $.ok(3)]).collect();
const s = $.from([$.exception(1, []), $.ok(2), $.ok(3)]).collect();

expect(await s.toArray({ atoms: true })).toEqual([$.unknown(1, []), $.ok([2, 3])]);
expect(await s.toArray({ atoms: true })).toEqual([$.exception(1, []), $.ok([2, 3])]);
});
});

Expand Down Expand Up @@ -116,15 +116,17 @@ describe("stream transforms", () => {
test("single unknown", async ({ expect }) => {
expect.assertions(1);

const s = $.from([$.unknown(1, []), $.ok(2), $.ok(3)]).mapUnknown((e) => $.error(e));
const s = $.from([$.exception(1, []), $.ok(2), $.ok(3)]).mapException((e) =>
$.error(e),
);

expect(await s.toArray({ atoms: true })).toEqual([$.error(1), $.ok(2), $.ok(3)]);
});

test("multiple unknown", async ({ expect }) => {
expect.assertions(1);

const s = $.from([$.unknown(1, []), $.ok(2), $.unknown(3, [])]).mapUnknown((e) =>
const s = $.from([$.exception(1, []), $.ok(2), $.exception(3, [])]).mapException((e) =>
$.error(e),
);

Expand Down

0 comments on commit a1d0d48

Please sign in to comment.