Skip to content

Commit

Permalink
loosen coupling by using constructor injection, add internal
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Aug 27, 2022
1 parent 06bc149 commit 21106d6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 53 deletions.
66 changes: 28 additions & 38 deletions packages/core/src/context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CoreSegmentEvent } from '../events/interfaces'

import { v4 as uuid } from '@lukeed/uuid'
import { dset } from 'dset'
import Logger, { LogLevel, LogMessage } from '../logger'
import { CoreLogger, LogLevel, LogMessage } from '../logger'
import Stats, { Metric } from '../stats'

export function isContext(v?: object): v is CoreContext {
Expand Down Expand Up @@ -39,17 +39,23 @@ export class ContextCancelation {
}

export class CoreContext<Event extends CoreSegmentEvent = CoreSegmentEvent> {
private _event: Event
private _attempts: number
public logger = new Logger()
private _id: string
event: Event
logger: CoreLogger
stats?: Stats
attempts = 0

private _failedDelivery?: ContextFailedDelivery
public stats?: Stats
private _id: string

constructor(event: Event, id?: string, stats?: Stats) {
this._attempts = 0
this._event = event
this._id = id ?? uuid()
constructor(
event: Event,
id = uuid(),
stats?: Stats,
logger: CoreLogger = new CoreLogger()
) {
this.event = event
this._id = id
this.logger = logger
this.stats = stats
}

Expand All @@ -58,7 +64,7 @@ export class CoreContext<Event extends CoreSegmentEvent = CoreSegmentEvent> {
}

isSame(other: CoreContext): boolean {
return other._id === this._id
return other.id === this.id
}

cancel = (error?: Error | ContextCancelation): never => {
Expand All @@ -73,61 +79,45 @@ export class CoreContext<Event extends CoreSegmentEvent = CoreSegmentEvent> {
this.logger.log(level, message, extras)
}

public get id(): string {
get id(): string {
return this._id
}

public get event(): Event {
return this._event
}

public set event(evt: Event) {
this._event = evt
}

public get attempts(): number {
return this._attempts
}

public set attempts(attempts: number) {
this._attempts = attempts
}

public updateEvent(path: string, val: unknown): Event {
updateEvent(path: string, val: unknown): Event {
// Don't allow integrations that are set to false to be overwritten with integration settings.
if (path.split('.')[0] === 'integrations') {
const integrationName = path.split('.')[1]

if (this._event.integrations?.[integrationName] === false) {
return this._event
if (this.event.integrations?.[integrationName] === false) {
return this.event
}
}

dset(this._event, path, val)
return this._event
dset(this.event, path, val)
return this.event
}

public failedDelivery(): ContextFailedDelivery | undefined {
failedDelivery(): ContextFailedDelivery | undefined {
return this._failedDelivery
}

public setFailedDelivery(options: ContextFailedDelivery) {
setFailedDelivery(options: ContextFailedDelivery) {
this._failedDelivery = options
}

public logs(): LogMessage[] {
logs(): LogMessage[] {
return this.logger.logs
}

public flush(): void {
flush(): void {
this.logger.flush()
this.stats?.flush()
}

toJSON(): SerializedContext {
return {
id: this._id,
event: this._event,
event: this.event,
logs: this.logger.logs,
metrics: this.stats?.metrics,
}
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/logger/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Logger from '..'
import { CoreLogger } from '..'

describe(Logger, () => {
let logger: Logger
describe(CoreLogger, () => {
let logger: CoreLogger

beforeEach(() => {
logger = new Logger()
logger = new CoreLogger()
})

it('logs events at different levels', () => {
Expand Down
14 changes: 6 additions & 8 deletions packages/core/src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ export type LogMessage = {
level: LogLevel
message: string
time?: Date
extras?: object & {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any
}
extras?: Record<string, any>
}

export interface BaseLogger {
log(...args: any[]): void
flush(...args: any[]): void
// interface is just for clarity
export interface Logger {
log(level: LogLevel, message: string, extras?: object): void
flush(): void
logs: LogMessage[]
}

export default class Logger {
export class CoreLogger implements Logger {
private _logs: LogMessage[] = []

log = (level: LogLevel, message: string, extras?: object): void => {
Expand Down
2 changes: 1 addition & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"tslib": "^2.4.0"
},
"devDependencies": {
"@internal/config": "workspace:^",
"@internal/config": "0.0.0",
"@types/node": "^12.12.14"
},
"packageManager": "[email protected]"
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ __metadata:
languageName: node
linkType: hard

"@internal/config@workspace:^, @internal/config@workspace:internal/config":
"@internal/config@0.0.0, @internal/config@workspace:^, @internal/config@workspace:internal/config":
version: 0.0.0-use.local
resolution: "@internal/config@workspace:internal/config"
languageName: unknown
Expand Down Expand Up @@ -1580,7 +1580,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@segment/analytics-node@workspace:packages/node"
dependencies:
"@internal/config": "workspace:^"
"@internal/config": 0.0.0
"@segment/analytics-core": 1.0.1
"@segment/analytics-plugin-validation": 0.0.0
"@types/node": ^12.12.14
Expand Down

0 comments on commit 21106d6

Please sign in to comment.