Skip to content

Commit ce90543

Browse files
authored
small refactor to make DRY and improve code clarity (#603)
* 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 <[email protected]>
1 parent 4ce4934 commit ce90543

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

.changeset/loud-dolphins-clap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-core': patch
3+
---
4+
5+
Remove extraneous code from EQ

packages/core/src/queue/__tests__/extension-flushing.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ const testPlugin: Plugin = {
3030

3131
const ajs = {} as CoreAnalytics
3232

33+
// get rid of console spam for thrown errors.
34+
jest.spyOn(console, 'warn').mockImplementation(() => {})
35+
3336
describe('Registration', () => {
3437
test('can register plugins', async () => {
3538
const eq = new EventQueue()
@@ -130,6 +133,7 @@ describe('Plugin flushing', () => {
130133
})
131134

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

135139
await eq.register(

packages/core/src/queue/event-queue.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,6 @@ export class EventQueue extends Emitter<EventQueueEmitterContract> {
133133
ctx.attempts = 1
134134

135135
return this.deliver(ctx).catch((err) => {
136-
if (err instanceof ContextCancelation && err.retry === false) {
137-
ctx.setFailedDelivery({ reason: err })
138-
return ctx
139-
}
140-
141136
const accepted = this.enqueuRetry(err, ctx)
142137
if (!accepted) {
143138
ctx.setFailedDelivery({ reason: err })
@@ -194,16 +189,12 @@ export class EventQueue extends Emitter<EventQueueEmitterContract> {
194189
}
195190

196191
private enqueuRetry(err: Error, ctx: CoreContext): boolean {
197-
const notRetriable =
198-
err instanceof ContextCancelation && err.retry === false
199-
const retriable = !notRetriable
200-
201-
if (retriable) {
202-
const accepted = this.queue.pushWithBackoff(ctx)
203-
return accepted
192+
const retriable = !(err instanceof ContextCancelation) || err.retry
193+
if (!retriable) {
194+
return false
204195
}
205196

206-
return false
197+
return this.queue.pushWithBackoff(ctx)
207198
}
208199

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

0 commit comments

Comments
 (0)