Skip to content

Commit

Permalink
added prettier and export types
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco Rivola committed Feb 3, 2024
1 parent 607669a commit e17a02d
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 50 deletions.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"test-ci": "c8 node --require ts-node/register --test test/*.spec.ts",
"build": "tsc -p tsconfig.json",
"build-watch": "tsc -p tsconfig.json --watch",
"format": "prettier --check **/*.ts",
"lint": "eslint . --ext .ts"
},
"repository": {
Expand All @@ -30,6 +31,7 @@
"@typescript-eslint/parser": "^6.9.1",
"c8": "^9.0.0",
"eslint": "^8.53.0",
"prettier": "^3.2.4",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
Expand Down
49 changes: 25 additions & 24 deletions src/counter.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import { EventEmitter } from "events";

type Counter = {
incrementCounter: () => void;
decrementCounter: () => void;
getCount: () => number;
registerSubscriberToCounterChanges(callback: (count: number) => void): void;
}
incrementCounter: () => void;
decrementCounter: () => void;
getCount: () => number;
registerSubscriberToCounterChanges(callback: (count: number) => void): void;
};

export function createCounter(): Counter {
const eventEmitter: EventEmitter = new EventEmitter();
let count = 0;

const eventEmitter: EventEmitter = new EventEmitter();
let count = 0;

return {
incrementCounter(): void {
count++;
eventEmitter.emit("counterUpdate", count);
},
decrementCounter(): void {
count--;
eventEmitter.emit("counterUpdate", count);
},
getCount(): number {
return count;
},
registerSubscriberToCounterChanges(callback: (count: number) => void): void {
eventEmitter.on("counterUpdate", callback);
},
};
return {
incrementCounter(): void {
count++;
eventEmitter.emit("counterUpdate", count);
},
decrementCounter(): void {
count--;
eventEmitter.emit("counterUpdate", count);
},
getCount(): number {
return count;
},
registerSubscriberToCounterChanges(
callback: (count: number) => void,
): void {
eventEmitter.on("counterUpdate", callback);
},
};
}
2 changes: 1 addition & 1 deletion src/errors/abort-error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default class AbortError extends Error {
export class AbortError extends Error {
public name = "AbortError";
}
4 changes: 2 additions & 2 deletions src/errors/closing-error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default class ClosingError extends Error {
public name = "ClosingError";
export class ClosingError extends Error {
public name = "ClosingError";
}
4 changes: 2 additions & 2 deletions src/errors/timeout-closing-error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default class TimeoutClosingError extends Error {
public name = "TimeoutClosingError";
export class TimeoutClosingError extends Error {
public name = "TimeoutClosingError";
}
22 changes: 11 additions & 11 deletions src/fire-and-forgetter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createCounter } from "./counter";
import AbortError from "./errors/abort-error";
import ClosingError from "./errors/closing-error";
import TimeoutClosingError from "./errors/timeout-closing-error";
import { AbortError } from "./errors/abort-error";
import { ClosingError } from "./errors/closing-error";
import { TimeoutClosingError } from "./errors/timeout-closing-error";

type InternalOptions = {
defaultOnError: (error) => void;
Expand All @@ -10,11 +10,11 @@ type InternalOptions = {

type Options = Partial<InternalOptions>;

type FireAndForgetter = {
export type FireAndForgetter = {
close: (options?: CloseOptions) => Promise<void>;
} & ((
func: (signal: AbortSignal) => Promise<void>,
onError?: (error) => void
onError?: (error) => void,
) => void);

type CloseOptions = {
Expand Down Expand Up @@ -52,7 +52,7 @@ export function fireAndForgetter(options?: Options): FireAndForgetter {
*/
function fireAndForget(
func: (signal: AbortSignal) => Promise<void>,
onError: (error) => void = defaultOnError
onError: (error) => void = defaultOnError,
): void {
if (closing) {
handleClosing(onError);
Expand All @@ -63,7 +63,7 @@ export function fireAndForgetter(options?: Options): FireAndForgetter {

async function fire(
func: (signal: AbortSignal) => Promise<void>,
onError: (error) => void
onError: (error) => void,
): Promise<void> {
try {
counter.incrementCounter();
Expand All @@ -77,7 +77,7 @@ export function fireAndForgetter(options?: Options): FireAndForgetter {

function handleClosing(onError: (error) => void) {
const closingError = new ClosingError(
"Cannot longer execute fire and forget operation as is closing or closed"
"Cannot longer execute fire and forget operation as is closing or closed",
);
if (throwOnClosing) {
throw closingError;
Expand Down Expand Up @@ -116,10 +116,10 @@ export function fireAndForgetter(options?: Options): FireAndForgetter {
() =>
reject(
new TimeoutClosingError(
`Cannot close after ${timeout}ms, ${counter.getCount()} fire and forget operations are still in progress`
)
`Cannot close after ${timeout}ms, ${counter.getCount()} fire and forget operations are still in progress`,
),
),
timeout
timeout,
);
}
});
Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
import { fireAndForgetter } from "./fire-and-forgetter";
export default fireAndForgetter;
export {
fireAndForgetter as default,
FireAndForgetter,
} from "./fire-and-forgetter";
export * from "./errors/abort-error";
export * from "./errors/closing-error";
export * from "./errors/timeout-closing-error";
17 changes: 9 additions & 8 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import ClosingError from "../src/errors/closing-error";
import TimeoutClosingError from "../src/errors/timeout-closing-error";
import fireAndForgetter from "../src/index";
import { describe, test, mock } from "node:test";
import { equal } from "assert/strict";
import { setTimeout as sleep } from "timers/promises";
import AbortError from "../src/errors/abort-error";
import { notEqual } from "assert";
import fireAndForgetter, {
ClosingError,
TimeoutClosingError,
AbortError,
} from "../src/index";

console.error = mock.fn();

Expand Down Expand Up @@ -87,7 +88,7 @@ describe("fire-and-forgetter", () => {
equal(error instanceof TimeoutClosingError, true);
equal(
(error as Error).message,
"Cannot close after 10ms, 3 fire and forget operations are still in progress"
"Cannot close after 10ms, 3 fire and forget operations are still in progress",
);
equal(count, 0);
functionHasThrownError = true;
Expand All @@ -114,7 +115,7 @@ describe("fire-and-forgetter", () => {
equal(error instanceof Error, true);
equal((error as Error).message, "ups, some error happened");
onErrorHasBeenCalled = true;
}
},
);

await fireAndForget.close();
Expand Down Expand Up @@ -163,7 +164,7 @@ describe("fire-and-forgetter", () => {
equal(error instanceof ClosingError, true);
equal(
(error as Error).message,
"Cannot longer execute fire and forget operation as is closing or closed"
"Cannot longer execute fire and forget operation as is closing or closed",
);
functionHasThrownError = true;
}
Expand Down Expand Up @@ -194,7 +195,7 @@ describe("fire-and-forgetter", () => {
equal(reportedError instanceof ClosingError, true);
equal(
(reportedError as Error).message,
"Cannot longer execute fire and forget operation as is closing or closed"
"Cannot longer execute fire and forget operation as is closing or closed",
);
});
});

0 comments on commit e17a02d

Please sign in to comment.