Skip to content

Commit 4e2abfd

Browse files
author
Mat Jones
committed
break types out into dedicated files
1 parent 67945c8 commit 4e2abfd

9 files changed

Lines changed: 55 additions & 54 deletions

File tree

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ export { User } from "./interfaces/user";
5454
// #region Types
5555
// -----------------------------------------------------------------------------------------
5656

57-
export { AsyncWorkload } from "./types/do-try-types";
57+
export { AsyncWorkload } from "./types/async-workload";
5858
export { CancellablePromise } from "./types/cancellable-promise";
59-
export { CatchHandler } from "./types/do-try-types";
59+
export { CatchResultHandler } from "./types/catch-result-handler";
6060
export { Constructor } from "./types/constructor";
61-
export { FinallyHandler } from "./types/do-try-types";
62-
export { SyncWorkload } from "./types/do-try-types";
61+
export { FinallyHandler } from "./types/finally-handler";
62+
export { SyncWorkload } from "./types/sync-workload";
6363

6464
// #endregion Types
6565

src/interfaces/do-try-config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { CatchResultHandler } from "../types/catch-result-handler";
2+
3+
interface DoTryConfig {
4+
/**
5+
* A default handler that will always run on error, if configured,
6+
* even if a `catch()` does not exist in the call chain.
7+
* This is useful for adding default error handling in the
8+
* development environment, such as `console.error(err)`.
9+
*/
10+
defaultErrorHandler?: CatchResultHandler<any>;
11+
}
12+
13+
export { DoTryConfig };

src/types/async-workload.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Represents an asynchronous method reference.
3+
*/
4+
type AsyncWorkload<T> = () => Promise<T>;
5+
6+
export { AsyncWorkload };

src/types/catch-result-handler.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ResultRecord } from "../view-models/result-record";
2+
3+
/**
4+
* Handler for a typed error ResultRecord, or any type if a Javascript error occurred.
5+
*/
6+
type CatchResultHandler<T> = (result?: ResultRecord<T>, error?: any) => void;
7+
8+
export { CatchResultHandler };

src/types/do-try-types.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/types/finally-handler.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Handler for Do.try().finally(); Runs whether an error occurred or not.
3+
*/
4+
type FinallyHandler = () => void;
5+
6+
export { FinallyHandler };

src/types/sync-workload.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Represents a synchronous method reference.
3+
*/
4+
type SyncWorkload<T> = () => T;
5+
6+
export { SyncWorkload };

src/utilities/do-try.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Do, DoSync } from "../utilities/do-try";
88
import { PolyfillUtils } from "../utilities/polyfill-utils";
99
import { StubResourceRecord } from "../tests/stubs/stub-resource-record";
1010
import { CoreUtils } from "../utilities/core-utils";
11-
import { CatchHandler } from "../types/do-try-types";
11+
import { CatchResultHandler } from "../types/catch-result-handler";
1212

1313
PolyfillUtils.registerPromiseFinallyPolyfill();
1414

@@ -123,7 +123,7 @@ describe("do-try.ts", () => {
123123
describe("Do.configure", () => {
124124
it("When defaultErrorHandler configured and catch() is in call chain, then defaultErrorHandler is still executed", async () => {
125125
// Arrange
126-
const defaultErrorHandler: CatchHandler<any> = jest.fn();
126+
const defaultErrorHandler: CatchResultHandler<any> = jest.fn();
127127
Do.configure({ defaultErrorHandler });
128128
const catchHandler = jest.fn();
129129
const workload = async () => {
@@ -142,7 +142,7 @@ describe("do-try.ts", () => {
142142

143143
it("When defaultErrorHandler configured and no catch() is in call chain, the defaultErrorHandler is still executed", async () => {
144144
// Arrange
145-
const defaultErrorHandler: CatchHandler<any> = jest.fn();
145+
const defaultErrorHandler: CatchResultHandler<any> = jest.fn();
146146
Do.configure({ defaultErrorHandler });
147147
const workload = async () => {
148148
throw Error();
@@ -245,7 +245,7 @@ describe("do-try.ts", () => {
245245
describe("DoSync.configure", () => {
246246
it("When defaultErrorHandler configured and catch() in call chain, then defaultErrorHandler is still executed", () => {
247247
// Arrange
248-
const defaultErrorHandler: CatchHandler<any> = jest.fn();
248+
const defaultErrorHandler: CatchResultHandler<any> = jest.fn();
249249
DoSync.configure({ defaultErrorHandler });
250250
const catchHandler = jest.fn();
251251
const workload = () => {
@@ -264,7 +264,7 @@ describe("do-try.ts", () => {
264264

265265
it("When defaultErrorHandler configured and no catch() is in call chain, the defaultErrorHandler is still executed", () => {
266266
// Arrange
267-
const defaultErrorHandler: CatchHandler<any> = jest.fn();
267+
const defaultErrorHandler: CatchResultHandler<any> = jest.fn();
268268
DoSync.configure({ defaultErrorHandler });
269269
const workload = () => {
270270
throw Error();

src/utilities/do-try.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { ResultRecord } from "../view-models/result-record";
2-
import {
3-
AsyncWorkload,
4-
CatchHandler,
5-
DoTryConfig,
6-
FinallyHandler,
7-
SyncWorkload,
8-
} from "../types/do-try-types";
2+
import { DoTryConfig } from "../interfaces/do-try-config";
3+
import { AsyncWorkload } from "../types/async-workload";
4+
import { CatchResultHandler } from "../types/catch-result-handler";
5+
import { FinallyHandler } from "../types/finally-handler";
6+
import { SyncWorkload } from "../types/sync-workload";
97

108
// -----------------------------------------------------------------------------------------
119
// #region Do
@@ -46,7 +44,7 @@ class Do<TResourceType, TReturnVal = void> {
4644
* @returns this
4745
*/
4846
public catch(
49-
errorHandler: CatchHandler<TResourceType>
47+
errorHandler: CatchResultHandler<TResourceType>
5048
): Do<TResourceType, TReturnVal> {
5149
this.promise = this.promise.catch((err: any) => {
5250
if (err instanceof ResultRecord) {
@@ -134,7 +132,7 @@ class DoSync<TResourceType, TReturnVal = void> {
134132
* @param errorHandler handle errors, either as a {ResultRecord} or {any}
135133
*/
136134
public catch(
137-
errorHandler: CatchHandler<TResourceType>
135+
errorHandler: CatchResultHandler<TResourceType>
138136
): DoSync<TResourceType, TReturnVal> {
139137
this.catchHandler = (err: any) => {
140138
if (err instanceof ResultRecord) {

0 commit comments

Comments
 (0)