Skip to content

Commit

Permalink
small refactor to make DRY and improve code clarity (#603)
Browse files Browse the repository at this point in the history
* small refactor to make DRY and improve code clarity

* remove extraneous code from EQ

* get rid of console spam for extension flushing tests

Co-authored-by: Seth Silesky <silesky@users.noreply.github.com>
  • Loading branch information
silesky and silesky authored Sep 29, 2022
1 parent 4ce4934 commit ce90543
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-dolphins-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-core': patch
---

Remove extraneous code from EQ
4 changes: 4 additions & 0 deletions packages/core/src/queue/__tests__/extension-flushing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const testPlugin: Plugin = {

const ajs = {} as CoreAnalytics

// get rid of console spam for thrown errors.
jest.spyOn(console, 'warn').mockImplementation(() => {})

describe('Registration', () => {
test('can register plugins', async () => {
const eq = new EventQueue()
Expand Down Expand Up @@ -130,6 +133,7 @@ describe('Plugin flushing', () => {
})

test('atempts `enrichment` plugins', async () => {
jest.spyOn(console, 'warn').mockImplementationOnce(() => null)
const eq = new EventQueue()

await eq.register(
Expand Down
17 changes: 4 additions & 13 deletions packages/core/src/queue/event-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@ export class EventQueue extends Emitter<EventQueueEmitterContract> {
ctx.attempts = 1

return this.deliver(ctx).catch((err) => {
if (err instanceof ContextCancelation && err.retry === false) {
ctx.setFailedDelivery({ reason: err })
return ctx
}

const accepted = this.enqueuRetry(err, ctx)
if (!accepted) {
ctx.setFailedDelivery({ reason: err })
Expand Down Expand Up @@ -194,16 +189,12 @@ export class EventQueue extends Emitter<EventQueueEmitterContract> {
}

private enqueuRetry(err: Error, ctx: CoreContext): boolean {
const notRetriable =
err instanceof ContextCancelation && err.retry === false
const retriable = !notRetriable

if (retriable) {
const accepted = this.queue.pushWithBackoff(ctx)
return accepted
const retriable = !(err instanceof ContextCancelation) || err.retry
if (!retriable) {
return false
}

return false
return this.queue.pushWithBackoff(ctx)
}

async flush(): Promise<CoreContext[]> {
Expand Down

0 comments on commit ce90543

Please sign in to comment.