Skip to content

Commit

Permalink
Move files into core directory (#12)
Browse files Browse the repository at this point in the history
Only index and resonate remain at top level.
  • Loading branch information
dfarr authored Dec 8, 2023
1 parent a666f38 commit 1f90793
Show file tree
Hide file tree
Showing 24 changed files with 98 additions and 63 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
testPathIgnorePatterns: ["lib/loggers/test.ts"],
testPathIgnorePatterns: ["lib/core/loggers/test.ts"],
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
75 changes: 75 additions & 0 deletions lib/core/loggers/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { ILogger, ITrace } from "../logger";

type LogLevel = "debug" | "info" | "warn" | "error";

export class Logger implements ILogger {
private _level: number;

constructor(public level: LogLevel = "info") {
switch (level) {
case "debug":
this._level = 0;
break;
case "info":
this._level = 1;
break;
case "warn":
this._level = 2;
break;
case "error":
this._level = 3;
break;
}
}

startTrace(id: string, attrs?: Record<string, string>): ITrace {
return new Trace(this, id, attrs);
}

private log(level: number, args: any[]): void {
if (this._level <= level) {
console.log(...args);
}
}

debug(...args: any[]): void {
this.log(0, args);
}

info(...args: any[]): void {
this.log(1, args);
}

warn(...args: any[]): void {
this.log(2, args);
}

error(...args: any[]): void {
this.log(3, args);
}
}

class Trace implements ITrace {
private timestamp: number = Date.now();

constructor(
private logger: Logger,
private id: string,
private attrs?: Record<string, string>,
private parent?: string,
) {}

start(id: string, attrs?: Record<string, string>): ITrace {
return new Trace(this.logger, id, attrs, this.id);
}

end(): void {
this.logger.debug({
id: this.id,
parentId: this.parent,
timestamp: this.timestamp,
duration: Date.now() - this.timestamp,
attrs: this.attrs,
});
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/retries/retry.ts → lib/core/retries/retry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context } from "../resonate";
import { Context } from "../../resonate";
import { IRetry } from "../retry";

export class Retry implements IRetry {
Expand Down
2 changes: 1 addition & 1 deletion lib/retry.ts → lib/core/retry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context } from "./resonate";
import { Context } from "../resonate";

export interface IRetry {
next(context: Context): { done: boolean; delay?: number };
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
40 changes: 0 additions & 40 deletions lib/loggers/logger.ts

This file was deleted.

30 changes: 15 additions & 15 deletions lib/resonate.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { Opts, isPartialOpts } from "./opts";
import { IPromiseStore } from "./store";
import { Opts, isPartialOpts } from "./core/opts";
import { IPromiseStore } from "./core/store";
import {
DurablePromise,
isCanceledPromise,
isPendingPromise,
isRejectedPromise,
isResolvedPromise,
isTimedoutPromise,
} from "./promise";
import { IRetry } from "./retry";
import { Retry } from "./retries/retry";
import { IBucket } from "./bucket";
import { Bucket } from "./buckets/bucket";
import { VolatilePromiseStore } from "./stores/volatile";
import { DurablePromiseStore } from "./stores/durable";
import { ILogger, ITrace } from "./logger";
import { Logger } from "./loggers/logger";
import { IEncoder } from "./encoder";
import { JSONEncoder } from "./encoders/json";
import { ErrorEncoder } from "./encoders/error";
import { ErrorCodes, ResonateError } from "./error";
} from "./core/promise";
import { IRetry } from "./core/retry";
import { Retry } from "./core/retries/retry";
import { IBucket } from "./core/bucket";
import { Bucket } from "./core/buckets/bucket";
import { VolatilePromiseStore } from "./core/stores/volatile";
import { DurablePromiseStore } from "./core/stores/durable";
import { ILogger, ITrace } from "./core/logger";
import { Logger } from "./core/loggers/logger";
import { IEncoder } from "./core/encoder";
import { JSONEncoder } from "./core/encoders/json";
import { ErrorEncoder } from "./core/encoders/error";
import { ErrorCodes, ResonateError } from "./core/error";

// Types

Expand Down
4 changes: 2 additions & 2 deletions test/retry.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { jest, describe, test, expect, beforeEach } from "@jest/globals";
import { Resonate, Context } from "../lib/resonate";
import { IRetry } from "../lib/retry";
import { Retry } from "../lib/retries/retry";
import { IRetry } from "../lib/core/retry";
import { Retry } from "../lib/core/retries/retry";

// Set a larger timeout for hooks (e.g., 10 seconds)
jest.setTimeout(10000);
Expand Down
4 changes: 2 additions & 2 deletions test/store.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { jest, describe, test, expect } from "@jest/globals";

import { DurablePromiseStore } from "../lib/stores/durable";
import { VolatilePromiseStore } from "../lib/stores/volatile";
import { DurablePromiseStore } from "../lib/core/stores/durable";
import { VolatilePromiseStore } from "../lib/core/stores/volatile";

// Set a larger timeout for hooks (e.g., 10 seconds)
jest.setTimeout(10000);
Expand Down
2 changes: 1 addition & 1 deletion test/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { jest, describe, test, expect } from "@jest/globals";
import { Resonate, Context } from "../lib/resonate";
import { TestLogger } from "../lib/loggers/test";
import { TestLogger } from "../lib/core/loggers/test";

jest.setTimeout(100000); // 100 seconds

Expand Down

0 comments on commit 1f90793

Please sign in to comment.