From 41863f47901f0c3aaa935ed421e52a4f3c9654b0 Mon Sep 17 00:00:00 2001 From: Semen Loktionov Date: Tue, 24 Oct 2023 23:34:38 +0300 Subject: [PATCH 1/6] add go and event-bus --- package.json | 5 +- packages/tools/src/const/event-bus.ts | 6 ++ packages/tools/src/const/index.ts | 1 + packages/tools/src/event-bus.ts | 102 ++++++++++++++++++++++++++ packages/tools/src/go.ts | 10 +++ packages/tools/src/types/event-bus.ts | 22 ++++++ packages/tools/src/types/index.ts | 1 + yarn.lock | 8 ++ 8 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 packages/tools/src/const/event-bus.ts create mode 100644 packages/tools/src/event-bus.ts create mode 100644 packages/tools/src/go.ts create mode 100644 packages/tools/src/types/event-bus.ts diff --git a/package.json b/package.json index 4dde5bd7..4f909af9 100644 --- a/package.json +++ b/package.json @@ -69,5 +69,8 @@ "typescript": "^5.0.4", "yorkie": "^2.0.0" }, - "packageManager": "yarn@3.4.1" + "packageManager": "yarn@3.4.1", + "dependencies": { + "loglevel": "^1.8.1" + } } diff --git a/packages/tools/src/const/event-bus.ts b/packages/tools/src/const/event-bus.ts new file mode 100644 index 00000000..c935bdfc --- /dev/null +++ b/packages/tools/src/const/event-bus.ts @@ -0,0 +1,6 @@ +export const EVENT_BUS_EVENTS = { + warning: 'warning', + success: 'success', + error: 'error', + info: 'info', +} as const diff --git a/packages/tools/src/const/index.ts b/packages/tools/src/const/index.ts index 5441e611..af5bf064 100644 --- a/packages/tools/src/const/index.ts +++ b/packages/tools/src/const/index.ts @@ -1 +1,2 @@ export * from './bn' +export * from './event-bus' diff --git a/packages/tools/src/event-bus.ts b/packages/tools/src/event-bus.ts new file mode 100644 index 00000000..122b70f6 --- /dev/null +++ b/packages/tools/src/event-bus.ts @@ -0,0 +1,102 @@ +import log from 'loglevel' + +import { EVENT_BUS_EVENTS } from '@/const' +import type { + EventBusEvent, + EventBusEventEmitterEventMap, + EventBusEventHandler, + EventBusEventMap, + EventBusEventName, + EventHandler, +} from '@/types' + +import { EventEmitter } from './event-emitter' + +export class EventBus { + readonly #events: EventBusEventMap + readonly #emitter: EventEmitter< + EventBusEventEmitterEventMap + > + #backlog: EventBusEvent>[] + + constructor(events?: AdditionalEventBusMap) { + this.#backlog = [] + this.#emitter = new EventEmitter< + EventBusEventEmitterEventMap + >() + + this.#events = { + ...EVENT_BUS_EVENTS, + ...(events || {}), + } as EventBusEventMap + } + + public get events(): EventBusEventMap { + return this.#events + } + + public isEventExists( + event: EventBusEventName, + ): boolean { + const values = Object.values( + this.#events, + ) as EventBusEventName[] + return values.includes(event) + } + + public on( + event: EventBusEventName>, + handler: EventBusEventHandler, + ): void { + if (!this.isEventExists(event)) { + throw new Error(`EventBus.list has no ${event} event`) + } + + const backloggedEvents = this.#backlog.filter(e => e.name === event) + + for (const [index, eventObj] of backloggedEvents.entries()) { + handler(eventObj.payload as Payload) + this.#backlog.splice(index, 1) + log.debug(`Event ${event} is backlogged. Handling...`) + } + this.#emitter.on(event, handler as EventHandler) + } + + public emit( + event: EventBusEventName>, + payload?: Payload, + ): void { + if (!this.isEventExists(event)) { + throw new Error(`EventBus.list has no ${event.toString()} event`) + } + + this.#emitter.emit(event, payload) + } + + public reset( + event: EventBusEventName>, + handler: EventBusEventHandler, + ): void { + if (!this.isEventExists(event)) { + throw new Error(`EventBus.list has no ${event.toString()} event`) + } + this.#emitter.off(event, handler as EventHandler) + this.#backlog = [] + } + + public success(payload: Payload): void { + this.#emitter.emit(this.#events.success, payload) + } + + public warning(payload: Payload): void { + this.#emitter.emit(this.#events.warning, payload) + } + + public error(payload: Payload): void { + this.#emitter.emit(this.#events.error, payload) + } + + public info(payload: Payload): void { + this.#emitter.emit(this.#events.info, payload) + } +} diff --git a/packages/tools/src/go.ts b/packages/tools/src/go.ts new file mode 100644 index 00000000..bd1c7159 --- /dev/null +++ b/packages/tools/src/go.ts @@ -0,0 +1,10 @@ +export const go = async unknown, E = Error>( + cb: C, +): Promise<[E | null, Awaited> | null]> => { + try { + const res = await cb() + return [null, res as Awaited>] + } catch (e) { + return [e as E, null] + } +} diff --git a/packages/tools/src/types/event-bus.ts b/packages/tools/src/types/event-bus.ts new file mode 100644 index 00000000..17446b3f --- /dev/null +++ b/packages/tools/src/types/event-bus.ts @@ -0,0 +1,22 @@ +import { EVENT_BUS_EVENTS } from '@/const/event-bus' + +export type EventBusEventMap = + typeof EVENT_BUS_EVENTS & AdditionalEventBusMap + +export type EventBusEventName = string & + keyof EventBusEventMap + +export type EventBusEvent< + AdditionalEventBusMap extends object, + Payload = unknown, +> = { + name: EventBusEventName> + payload?: Payload +} + +export type EventBusEventHandler = (payload: Payload) => void + +export type EventBusEventEmitterEventMap = + { + [key in keyof EventBusEventMap]: unknown + } diff --git a/packages/tools/src/types/index.ts b/packages/tools/src/types/index.ts index 8e54b931..a2a26ed0 100644 --- a/packages/tools/src/types/index.ts +++ b/packages/tools/src/types/index.ts @@ -1,3 +1,4 @@ export * from './bn' +export * from './event-bus' export * from './event-emitter' export * from './time' diff --git a/yarn.lock b/yarn.lock index ad746985..d55794d5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6247,6 +6247,13 @@ __metadata: languageName: node linkType: hard +"loglevel@npm:^1.8.1": + version: 1.8.1 + resolution: "loglevel@npm:1.8.1" + checksum: a1a62db40291aaeaef2f612334c49e531bff71cc1d01a2acab689ab80d59e092f852ab164a5aedc1a752fdc46b7b162cb097d8a9eb2cf0b299511106c29af61d + languageName: node + linkType: hard + "lowercase-keys@npm:^2.0.0": version: 2.0.0 resolution: "lowercase-keys@npm:2.0.0" @@ -8466,6 +8473,7 @@ __metadata: eslint-plugin-prettier: ^4.2.1 eslint-plugin-simple-import-sort: ^10.0.0 jest: ^29.5.0 + loglevel: ^1.8.1 prettier: ^2.8.4 rollup: ^3.18.0 rollup-plugin-polyfill-node: ^0.12.0 From c3d2a35e4ef92ede5541eed3ccf5c711ae4f8b87 Mon Sep 17 00:00:00 2001 From: Semen Loktionov Date: Wed, 25 Oct 2023 12:43:26 +0300 Subject: [PATCH 2/6] add initial problem implementation and extend runtime error --- packages/tools/src/errors.ts | 66 +++++++++++++++++++++---- packages/tools/src/problem.ts | 76 +++++++++++++++++++++++++++++ packages/tools/src/types/index.ts | 1 + packages/tools/src/types/problem.ts | 5 ++ 4 files changed, 138 insertions(+), 10 deletions(-) create mode 100644 packages/tools/src/problem.ts create mode 100644 packages/tools/src/types/problem.ts diff --git a/packages/tools/src/errors.ts b/packages/tools/src/errors.ts index 9d61d1fe..60241d1a 100644 --- a/packages/tools/src/errors.ts +++ b/packages/tools/src/errors.ts @@ -1,23 +1,69 @@ +import { Problem } from '@/problem' + export class RuntimeError extends Error { public name = 'RuntimeError' public originalError?: Error + public errorFields: object[] = [] public constructor(errorOrMessage: Error | string) public constructor(message: string, error: Error) + public constructor(message: string, error: Error, ...errorFields: object[]) + public constructor( + errorOrMessage: Error | string, + error?: Error, + ...errorFields: object[] + ) { + const isErrorOrMessageString = typeof errorOrMessage === 'string' + const message = isErrorOrMessageString + ? errorOrMessage + : errorOrMessage?.message + + super(message) - public constructor(errorOrMessage: Error | string, error?: Error) { - if (error) { - super(errorOrMessage as string) + this.originalError = + error || isErrorOrMessageString ? undefined : errorOrMessage + + this.errorFields = errorFields + } - this.originalError = error - } else { - if (typeof errorOrMessage === 'string') { - super(errorOrMessage) - } else { - super(errorOrMessage?.message) + public toString() { + let name = this.name + + const originalError = Problem.isRuntimeError(this.originalError) + ? Problem.cause(this.originalError) + : this.originalError + + if (originalError && !Problem.isRuntimeError(originalError)) { + name = `${name}: ${originalError.name}` + } - this.originalError = errorOrMessage + let message = this.message + let err = this.originalError + const fields = [...this.errorFields] + + for (;;) { + if (!err) break + + if (!Problem.isRuntimeError(err)) { + message += `: ${err.message}` + break } + + err = err.originalError + fields.push(...(err as RuntimeError).errorFields) + message += ` :${err!.message}` } + + const fieldObj = fields.reduce((acc, field) => { + acc = { ...acc, ...field } + return acc + }, {}) + + message = Object.entries(fieldObj).reduce((acc, [key, value]) => { + acc += ` ${key}: ${JSON.stringify(value)}` + return acc + }, message) + + return `${name}: ${message}` } } diff --git a/packages/tools/src/problem.ts b/packages/tools/src/problem.ts new file mode 100644 index 00000000..2e16ff2c --- /dev/null +++ b/packages/tools/src/problem.ts @@ -0,0 +1,76 @@ +import log from 'loglevel' + +import { RuntimeError } from './errors' +import { EventBus } from './event-bus' +import type { ProblemConfig } from './types' + +let config: ProblemConfig = { + eventBus: new EventBus(), +} + +export class Problem { + /** + * `setConfig` overrides default config. + */ + public static setConfig(cfg: ProblemConfig): void { + config = { ...config, ...cfg } + } + + /** + * `new` returns an error with the supplied message. + */ + public static new(message: string): RuntimeError { + return new RuntimeError(message) + } + + /** + * `wrap` returns an error annotating err with a stack trace + * at the point wrap is called, and the supplied message. + * + * + * Fields can optionally be added. If provided, multiple fields will be merged. + * + * If err is null, Wrap returns null. + */ + public static wrap( + error: Error | RuntimeError | null | undefined, + message: string, + ...errorFields: object[] + ): RuntimeError | null { + return error ? new RuntimeError(message, error, ...errorFields) : null + } + + /** + * `cause` returns the underlying cause of the error, if possible. + * If the error is null, null will be returned without further + * investigation. + */ + public static cause(error?: Error | null): Error | null { + if (!error) return null + if (!Problem.isRuntimeError(error)) return error + return error.originalError ? Problem.cause(error.originalError) : error + } + + public static isRuntimeError(error?: Error): error is RuntimeError { + return error instanceof RuntimeError + } + + /** + * `handle` handles provided error, error could be logged by default, and + * emitted via event bus if `eventBus` is set in the config. + * + * optional message parameter will be emitted with the error if presented + */ + public static handle(error: unknown, message?: string): void { + if (!(error instanceof Error)) return + config?.eventBus?.error?.({ error, message }) + Problem.handleWithoutFeedback(error) + } + + /** + * `handleWithoutFeedback` logs provided an error without emitting it via event bus. + */ + public static handleWithoutFeedback(error: Error | RuntimeError): void { + log.error(error) + } +} diff --git a/packages/tools/src/types/index.ts b/packages/tools/src/types/index.ts index a2a26ed0..87e5b3f6 100644 --- a/packages/tools/src/types/index.ts +++ b/packages/tools/src/types/index.ts @@ -1,4 +1,5 @@ export * from './bn' export * from './event-bus' export * from './event-emitter' +export * from './problem' export * from './time' diff --git a/packages/tools/src/types/problem.ts b/packages/tools/src/types/problem.ts new file mode 100644 index 00000000..41886857 --- /dev/null +++ b/packages/tools/src/types/problem.ts @@ -0,0 +1,5 @@ +import type { EventBus } from '@/event-bus' + +export type ProblemConfig = { + eventBus?: EventBus +} From 3f1d37b0fb65cefe4df4ef0c0f62fa9d789789c9 Mon Sep 17 00:00:00 2001 From: Semen Loktionov Date: Wed, 25 Oct 2023 12:50:08 +0300 Subject: [PATCH 3/6] refactor tools package structure --- packages/tools/src/errors/index.ts | 2 ++ packages/tools/src/{ => errors}/problem.ts | 7 ++++--- packages/tools/src/{errors.ts => errors/runtime-error.ts} | 2 +- packages/tools/src/{ => events}/event-bus.ts | 0 packages/tools/src/{ => events}/event-emitter.test.ts | 0 packages/tools/src/{ => events}/event-emitter.ts | 0 packages/tools/src/events/index.ts | 2 ++ packages/tools/src/index.ts | 5 ++--- packages/tools/src/{bn => math}/assertions.ts | 0 packages/tools/src/{bn => math}/bn.test.ts | 0 packages/tools/src/{bn => math}/bn.ts | 0 packages/tools/src/{bn => math}/decimals.ts | 0 packages/tools/src/{bn => math}/format.ts | 0 packages/tools/src/{bn => math}/index.ts | 0 packages/tools/src/{bn => math}/parsers.ts | 0 packages/tools/src/{bn => math}/round.ts | 4 ++-- packages/tools/src/{ => time}/duration.test.ts | 0 packages/tools/src/{ => time}/duration.ts | 0 packages/tools/src/time/index.ts | 2 ++ packages/tools/src/{ => time}/time.test.ts | 0 packages/tools/src/{ => time}/time.ts | 0 packages/tools/src/types/bn.ts | 3 ++- packages/tools/src/types/problem.ts | 2 +- 23 files changed, 18 insertions(+), 11 deletions(-) create mode 100644 packages/tools/src/errors/index.ts rename packages/tools/src/{ => errors}/problem.ts (94%) rename packages/tools/src/{errors.ts => errors/runtime-error.ts} (97%) rename packages/tools/src/{ => events}/event-bus.ts (100%) rename packages/tools/src/{ => events}/event-emitter.test.ts (100%) rename packages/tools/src/{ => events}/event-emitter.ts (100%) create mode 100644 packages/tools/src/events/index.ts rename packages/tools/src/{bn => math}/assertions.ts (100%) rename packages/tools/src/{bn => math}/bn.test.ts (100%) rename packages/tools/src/{bn => math}/bn.ts (100%) rename packages/tools/src/{bn => math}/decimals.ts (100%) rename packages/tools/src/{bn => math}/format.ts (100%) rename packages/tools/src/{bn => math}/index.ts (100%) rename packages/tools/src/{bn => math}/parsers.ts (100%) rename packages/tools/src/{bn => math}/round.ts (95%) rename packages/tools/src/{ => time}/duration.test.ts (100%) rename packages/tools/src/{ => time}/duration.ts (100%) create mode 100644 packages/tools/src/time/index.ts rename packages/tools/src/{ => time}/time.test.ts (100%) rename packages/tools/src/{ => time}/time.ts (100%) diff --git a/packages/tools/src/errors/index.ts b/packages/tools/src/errors/index.ts new file mode 100644 index 00000000..283161a4 --- /dev/null +++ b/packages/tools/src/errors/index.ts @@ -0,0 +1,2 @@ +export * from './problem' +export * from './runtime-error' diff --git a/packages/tools/src/problem.ts b/packages/tools/src/errors/problem.ts similarity index 94% rename from packages/tools/src/problem.ts rename to packages/tools/src/errors/problem.ts index 2e16ff2c..bc34752e 100644 --- a/packages/tools/src/problem.ts +++ b/packages/tools/src/errors/problem.ts @@ -1,8 +1,9 @@ import log from 'loglevel' -import { RuntimeError } from './errors' -import { EventBus } from './event-bus' -import type { ProblemConfig } from './types' +import { EventBus } from '@/events' +import type { ProblemConfig } from '@/types' + +import { RuntimeError } from './runtime-error' let config: ProblemConfig = { eventBus: new EventBus(), diff --git a/packages/tools/src/errors.ts b/packages/tools/src/errors/runtime-error.ts similarity index 97% rename from packages/tools/src/errors.ts rename to packages/tools/src/errors/runtime-error.ts index 60241d1a..4d26d665 100644 --- a/packages/tools/src/errors.ts +++ b/packages/tools/src/errors/runtime-error.ts @@ -1,4 +1,4 @@ -import { Problem } from '@/problem' +import { Problem } from './problem' export class RuntimeError extends Error { public name = 'RuntimeError' diff --git a/packages/tools/src/event-bus.ts b/packages/tools/src/events/event-bus.ts similarity index 100% rename from packages/tools/src/event-bus.ts rename to packages/tools/src/events/event-bus.ts diff --git a/packages/tools/src/event-emitter.test.ts b/packages/tools/src/events/event-emitter.test.ts similarity index 100% rename from packages/tools/src/event-emitter.test.ts rename to packages/tools/src/events/event-emitter.test.ts diff --git a/packages/tools/src/event-emitter.ts b/packages/tools/src/events/event-emitter.ts similarity index 100% rename from packages/tools/src/event-emitter.ts rename to packages/tools/src/events/event-emitter.ts diff --git a/packages/tools/src/events/index.ts b/packages/tools/src/events/index.ts new file mode 100644 index 00000000..5a968d0c --- /dev/null +++ b/packages/tools/src/events/index.ts @@ -0,0 +1,2 @@ +export * from './event-bus' +export * from './event-emitter' diff --git a/packages/tools/src/index.ts b/packages/tools/src/index.ts index 31bd08d6..5470f2dc 100644 --- a/packages/tools/src/index.ts +++ b/packages/tools/src/index.ts @@ -1,8 +1,7 @@ -export * from '@/bn' -export * from '@/duration' export * from '@/enums' export * from '@/errors' -export * from '@/event-emitter' +export * from '@/events' export * from '@/helpers' +export * from '@/math' export * from '@/time' export * from '@/types' diff --git a/packages/tools/src/bn/assertions.ts b/packages/tools/src/math/assertions.ts similarity index 100% rename from packages/tools/src/bn/assertions.ts rename to packages/tools/src/math/assertions.ts diff --git a/packages/tools/src/bn/bn.test.ts b/packages/tools/src/math/bn.test.ts similarity index 100% rename from packages/tools/src/bn/bn.test.ts rename to packages/tools/src/math/bn.test.ts diff --git a/packages/tools/src/bn/bn.ts b/packages/tools/src/math/bn.ts similarity index 100% rename from packages/tools/src/bn/bn.ts rename to packages/tools/src/math/bn.ts diff --git a/packages/tools/src/bn/decimals.ts b/packages/tools/src/math/decimals.ts similarity index 100% rename from packages/tools/src/bn/decimals.ts rename to packages/tools/src/math/decimals.ts diff --git a/packages/tools/src/bn/format.ts b/packages/tools/src/math/format.ts similarity index 100% rename from packages/tools/src/bn/format.ts rename to packages/tools/src/math/format.ts diff --git a/packages/tools/src/bn/index.ts b/packages/tools/src/math/index.ts similarity index 100% rename from packages/tools/src/bn/index.ts rename to packages/tools/src/math/index.ts diff --git a/packages/tools/src/bn/parsers.ts b/packages/tools/src/math/parsers.ts similarity index 100% rename from packages/tools/src/bn/parsers.ts rename to packages/tools/src/math/parsers.ts diff --git a/packages/tools/src/bn/round.ts b/packages/tools/src/math/round.ts similarity index 95% rename from packages/tools/src/bn/round.ts rename to packages/tools/src/math/round.ts index ad83d747..d1996277 100644 --- a/packages/tools/src/bn/round.ts +++ b/packages/tools/src/math/round.ts @@ -1,6 +1,6 @@ -import { BN } from '@/bn/bn' -import { toDecimals } from '@/bn/decimals' import { BN_ROUNDING } from '@/enums' +import { BN } from '@/math/bn' +import { toDecimals } from '@/math/decimals' export const round = (bn: BN, decimals: number, mode: BN_ROUNDING) => { const precisioned = toDecimals(bn.raw, BN.precision, decimals + 1).toString() diff --git a/packages/tools/src/duration.test.ts b/packages/tools/src/time/duration.test.ts similarity index 100% rename from packages/tools/src/duration.test.ts rename to packages/tools/src/time/duration.test.ts diff --git a/packages/tools/src/duration.ts b/packages/tools/src/time/duration.ts similarity index 100% rename from packages/tools/src/duration.ts rename to packages/tools/src/time/duration.ts diff --git a/packages/tools/src/time/index.ts b/packages/tools/src/time/index.ts new file mode 100644 index 00000000..ece339bc --- /dev/null +++ b/packages/tools/src/time/index.ts @@ -0,0 +1,2 @@ +export * from './duration' +export * from './time' diff --git a/packages/tools/src/time.test.ts b/packages/tools/src/time/time.test.ts similarity index 100% rename from packages/tools/src/time.test.ts rename to packages/tools/src/time/time.test.ts diff --git a/packages/tools/src/time.ts b/packages/tools/src/time/time.ts similarity index 100% rename from packages/tools/src/time.ts rename to packages/tools/src/time/time.ts diff --git a/packages/tools/src/types/bn.ts b/packages/tools/src/types/bn.ts index 9b584774..fa34e2e8 100644 --- a/packages/tools/src/types/bn.ts +++ b/packages/tools/src/types/bn.ts @@ -1,4 +1,5 @@ -import { BN } from '@/bn' +import { BN } from 'src/math' + import { BN_ROUNDING } from '@/enums' export type BnConfigLike = number | BnConfig diff --git a/packages/tools/src/types/problem.ts b/packages/tools/src/types/problem.ts index 41886857..7e8c9837 100644 --- a/packages/tools/src/types/problem.ts +++ b/packages/tools/src/types/problem.ts @@ -1,4 +1,4 @@ -import type { EventBus } from '@/event-bus' +import type { EventBus } from '@/events' export type ProblemConfig = { eventBus?: EventBus From 3cefe835b39677631e4dc081698857c3655b6ca1 Mon Sep 17 00:00:00 2001 From: Semen Loktionov Date: Wed, 25 Oct 2023 12:52:41 +0300 Subject: [PATCH 4/6] fix loglevel dep location --- package.json | 5 +---- packages/tools/package.json | 1 + yarn.lock | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 4f909af9..4dde5bd7 100644 --- a/package.json +++ b/package.json @@ -69,8 +69,5 @@ "typescript": "^5.0.4", "yorkie": "^2.0.0" }, - "packageManager": "yarn@3.4.1", - "dependencies": { - "loglevel": "^1.8.1" - } + "packageManager": "yarn@3.4.1" } diff --git a/packages/tools/package.json b/packages/tools/package.json index 0994c0e6..af567db0 100644 --- a/packages/tools/package.json +++ b/packages/tools/package.json @@ -56,6 +56,7 @@ "dependencies": { "bignumber.js": "^9.1.1", "dayjs": "^1.11.7", + "loglevel": "^1.8.1", "tslib": "^2.5.0" }, "typedoc": { diff --git a/yarn.lock b/yarn.lock index d55794d5..f6f023b9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -478,6 +478,7 @@ __metadata: bignumber.js: ^9.1.1 dayjs: ^1.11.7 jest: ^29.5.0 + loglevel: ^1.8.1 tsc-alias: ^1.8.2 tslib: ^2.5.0 languageName: unknown @@ -8473,7 +8474,6 @@ __metadata: eslint-plugin-prettier: ^4.2.1 eslint-plugin-simple-import-sort: ^10.0.0 jest: ^29.5.0 - loglevel: ^1.8.1 prettier: ^2.8.4 rollup: ^3.18.0 rollup-plugin-polyfill-node: ^0.12.0 From 62de727100f2095a36b5f119ffb6dc50b30f57f8 Mon Sep 17 00:00:00 2001 From: Semen Loktionov Date: Wed, 25 Oct 2023 12:55:17 +0300 Subject: [PATCH 5/6] fix import pathes --- packages/tools/src/math/round.ts | 5 +++-- packages/tools/src/types/bn.ts | 3 +-- packages/tools/src/types/event-bus.ts | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/tools/src/math/round.ts b/packages/tools/src/math/round.ts index d1996277..b66846d7 100644 --- a/packages/tools/src/math/round.ts +++ b/packages/tools/src/math/round.ts @@ -1,6 +1,7 @@ import { BN_ROUNDING } from '@/enums' -import { BN } from '@/math/bn' -import { toDecimals } from '@/math/decimals' + +import { BN } from './bn' +import { toDecimals } from './decimals' export const round = (bn: BN, decimals: number, mode: BN_ROUNDING) => { const precisioned = toDecimals(bn.raw, BN.precision, decimals + 1).toString() diff --git a/packages/tools/src/types/bn.ts b/packages/tools/src/types/bn.ts index fa34e2e8..463c5802 100644 --- a/packages/tools/src/types/bn.ts +++ b/packages/tools/src/types/bn.ts @@ -1,6 +1,5 @@ -import { BN } from 'src/math' - import { BN_ROUNDING } from '@/enums' +import { BN } from '@/math' export type BnConfigLike = number | BnConfig diff --git a/packages/tools/src/types/event-bus.ts b/packages/tools/src/types/event-bus.ts index 17446b3f..5e1835a7 100644 --- a/packages/tools/src/types/event-bus.ts +++ b/packages/tools/src/types/event-bus.ts @@ -1,4 +1,4 @@ -import { EVENT_BUS_EVENTS } from '@/const/event-bus' +import { EVENT_BUS_EVENTS } from '@/const' export type EventBusEventMap = typeof EVENT_BUS_EVENTS & AdditionalEventBusMap From fc95fc8814a11b8c5faee18adc2006e62e2ae75d Mon Sep 17 00:00:00 2001 From: Semen Loktionov Date: Wed, 25 Oct 2023 13:20:38 +0300 Subject: [PATCH 6/6] add go test --- .eslintrc.js | 1 + packages/tools/src/go.test.ts | 53 +++++++++++++++++++++++++++++++++++ packages/tools/src/index.ts | 1 + 3 files changed, 55 insertions(+) create mode 100644 packages/tools/src/go.test.ts diff --git a/.eslintrc.js b/.eslintrc.js index 59bfea1e..85b2274f 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -44,6 +44,7 @@ module.exports = { '@typescript-eslint/no-non-null-assertion': 0, 'simple-import-sort/imports': 'error', 'simple-import-sort/exports': 'error', + 'prefer-const': ['error', { destructuring: 'all'}], }, }; diff --git a/packages/tools/src/go.test.ts b/packages/tools/src/go.test.ts new file mode 100644 index 00000000..126c264b --- /dev/null +++ b/packages/tools/src/go.test.ts @@ -0,0 +1,53 @@ +import { go } from './go' + +describe('preforms go unit test', () => { + describe('async callback should return', () => { + test('null result if error thrown', async () => { + const [err, result] = await go(async () => { + throw new Error('test error') + }) + + expect(err).toBeInstanceOf(Error) + expect(result).toBeNull() + }) + + test('result if no error thrown', async () => { + const [err, result] = await go(async () => { + return 'test result' + }) + expect(err).toBeNull() + expect(result).toBe('test result') + }) + }) + + describe('sync callback should return', () => { + test('null result if error thrown', async () => { + const [err, result] = await go(() => { + throw new Error('test error') + }) + expect(err).toBeInstanceOf(Error) + expect(result).toBeNull() + }) + + test('result if no error thrown', async () => { + const [err, result] = await go(() => { + return 'test result' + }) + expect(err).toBeNull() + expect(result).toBe('test result') + }) + }) + + test('should redeclare error during few executions', async () => { + let [err, result] = await go(async () => { + throw new Error('test error') + }) + + expect(err).toBeInstanceOf(Error) + expect(result).toBeNull() + ;[err] = await go(async () => { + return 'success' + }) + expect(err).toBeNull() + }) +}) diff --git a/packages/tools/src/index.ts b/packages/tools/src/index.ts index 5470f2dc..29ac8ad4 100644 --- a/packages/tools/src/index.ts +++ b/packages/tools/src/index.ts @@ -1,6 +1,7 @@ export * from '@/enums' export * from '@/errors' export * from '@/events' +export * from '@/go' export * from '@/helpers' export * from '@/math' export * from '@/time'