Skip to content

Commit c5d2a99

Browse files
authored
browser: refactor/remove asPromise (#686)
1 parent db38bb3 commit c5d2a99

File tree

8 files changed

+49
-77
lines changed

8 files changed

+49
-77
lines changed

packages/browser/src/core/callback/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Context } from '../context'
2-
import { asPromise } from '../../lib/as-promise'
32
import { Callback } from '../events/interfaces'
43

54
export function pTimeout(
@@ -34,7 +33,7 @@ export function invokeCallback(
3433
): Promise<Context> {
3534
const cb = () => {
3635
try {
37-
return asPromise(callback(ctx))
36+
return Promise.resolve(callback(ctx))
3837
} catch (err) {
3938
return Promise.reject(err)
4039
}

packages/browser/src/lib/__tests__/as-promise.test.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/browser/src/lib/as-promise.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/browser/src/plugins/ajs-destination/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { Context, ContextCancelation } from '../../core/context'
77
import { isServer } from '../../core/environment'
88
import { Plugin } from '../../core/plugin'
99
import { attempt } from '../../core/queue/delivery'
10-
import { asPromise } from '../../lib/as-promise'
1110
import { isPlanEventEnabled } from '../../lib/is-plan-event-enabled'
1211
import { mergedOptions } from '../../lib/merged-options'
1312
import { pWhile } from '../../lib/p-while'
@@ -261,9 +260,7 @@ export class LegacyDestination implements Plugin {
261260

262261
try {
263262
if (this.integration) {
264-
await asPromise(
265-
this.integration.invoke.call(this.integration, eventType, event)
266-
)
263+
await this.integration.invoke.call(this.integration, eventType, event)
267264
}
268265
} catch (err) {
269266
ctx.stats.increment('analytics_js.integration.invoke.error', 1, [

packages/browser/src/plugins/middleware/__tests__/index.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
import { Analytics } from '../../../core/analytics'
77
import { Context } from '../../../core/context'
88
import { Plugin } from '../../../core/plugin'
9-
import { asPromise } from '../../../lib/as-promise'
109
import { LegacyDestination } from '../../ajs-destination'
1110

1211
describe(sourceMiddlewarePlugin, () => {
@@ -91,15 +90,18 @@ describe(sourceMiddlewarePlugin, () => {
9190
expect(returnedCtx).toBe(toReturn)
9291

9392
const toCancel = new Context({ type: 'track' })
94-
await asPromise(hangsXT.track!(toCancel)).catch((err) => {
93+
try {
94+
await hangsXT.track!(toCancel)
95+
throw new Error('should not reach here.')
96+
} catch (err) {
9597
expect(err).toMatchInlineSnapshot(`
9698
ContextCancelation {
9799
"reason": "Middleware \`next\` function skipped",
98100
"retry": false,
99101
"type": "middleware_cancellation",
100102
}
101103
`)
102-
})
104+
}
103105
})
104106
})
105107

packages/browser/src/plugins/middleware/index.ts

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Context, ContextCancelation } from '../../core/context'
22
import { SegmentEvent } from '../../core/events'
33
import { Plugin } from '../../core/plugin'
4-
import { asPromise } from '../../lib/as-promise'
54
import { SegmentFacade, toFacade } from '../../lib/to-facade'
65

76
export interface MiddlewareParams {
@@ -17,10 +16,13 @@ export interface DestinationMiddlewareParams {
1716
next: (payload: MiddlewareParams['payload'] | null) => void
1817
}
1918

20-
export type MiddlewareFunction = (middleware: MiddlewareParams) => void
19+
export type MiddlewareFunction = (
20+
middleware: MiddlewareParams
21+
) => void | Promise<void>
22+
2123
export type DestinationMiddlewareFunction = (
2224
middleware: DestinationMiddlewareParams
23-
) => void
25+
) => void | Promise<void>
2426

2527
export async function applyDestinationMiddleware(
2628
destination: string,
@@ -39,26 +41,24 @@ export async function applyDestinationMiddleware(
3941
let nextCalled = false
4042
let returnedEvent: SegmentEvent | null = null
4143

42-
await asPromise(
43-
fn({
44-
payload: toFacade(event, {
45-
clone: true,
46-
traverse: false,
47-
}),
48-
integration: destination,
49-
next(evt) {
50-
nextCalled = true
51-
52-
if (evt === null) {
53-
returnedEvent = null
54-
}
55-
56-
if (evt) {
57-
returnedEvent = evt.obj
58-
}
59-
},
60-
})
61-
)
44+
await fn({
45+
payload: toFacade(event, {
46+
clone: true,
47+
traverse: false,
48+
}),
49+
integration: destination,
50+
next(evt) {
51+
nextCalled = true
52+
53+
if (evt === null) {
54+
returnedEvent = null
55+
}
56+
57+
if (evt) {
58+
returnedEvent = evt.obj
59+
}
60+
},
61+
})
6262

6363
if (!nextCalled && returnedEvent !== null) {
6464
returnedEvent = returnedEvent as SegmentEvent
@@ -89,21 +89,19 @@ export function sourceMiddlewarePlugin(
8989
async function apply(ctx: Context): Promise<Context> {
9090
let nextCalled = false
9191

92-
await asPromise(
93-
fn({
94-
payload: toFacade(ctx.event, {
95-
clone: true,
96-
traverse: false,
97-
}),
98-
integrations: integrations ?? {},
99-
next(evt) {
100-
nextCalled = true
101-
if (evt) {
102-
ctx.event = evt.obj
103-
}
104-
},
105-
})
106-
)
92+
await fn({
93+
payload: toFacade(ctx.event, {
94+
clone: true,
95+
traverse: false,
96+
}),
97+
integrations: integrations ?? {},
98+
next(evt) {
99+
nextCalled = true
100+
if (evt) {
101+
ctx.event = evt.obj
102+
}
103+
},
104+
})
107105

108106
if (!nextCalled) {
109107
throw new ContextCancelation({

packages/browser/src/plugins/remote-loader/index.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { Integrations } from '../../core/events/interfaces'
22
import { LegacySettings } from '../../browser'
33
import { JSONObject, JSONValue } from '../../core/events'
44
import { Plugin } from '../../core/plugin'
5-
import { asPromise } from '../../lib/as-promise'
65
import { loadScript } from '../../lib/load-script'
76
import { getCDN } from '../../lib/parse-cdn'
87
import {
@@ -187,12 +186,10 @@ export async function remoteLoader(
187186
if (typeof window[libraryName] === 'function') {
188187
// @ts-expect-error
189188
const pluginFactory = window[libraryName] as PluginFactory
190-
const plugin = await asPromise(
191-
pluginFactory({
192-
...remotePlugin.settings,
193-
...mergedIntegrations[remotePlugin.name],
194-
})
195-
)
189+
const plugin = await pluginFactory({
190+
...remotePlugin.settings,
191+
...mergedIntegrations[remotePlugin.name],
192+
})
196193
const plugins = Array.isArray(plugin) ? plugin : [plugin]
197194

198195
validate(plugins)

packages/browser/src/plugins/routing-middleware/__tests__/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('tsub middleware', () => {
2525
payload = transformed?.obj
2626
})
2727

28-
middleware({
28+
void middleware({
2929
integration: 'Google Tag Manager',
3030
next,
3131
payload: toFacade({
@@ -44,7 +44,7 @@ describe('tsub middleware', () => {
4444
payload = transformed?.obj
4545
})
4646

47-
middleware({
47+
void middleware({
4848
integration: 'Google Analytics',
4949
next,
5050
payload: toFacade({

0 commit comments

Comments
 (0)