diff --git a/packages/browser/src/core/callback/index.ts b/packages/browser/src/core/callback/index.ts index 3a95d3d7b..9c52aa189 100644 --- a/packages/browser/src/core/callback/index.ts +++ b/packages/browser/src/core/callback/index.ts @@ -1,5 +1,4 @@ import { Context } from '../context' -import { asPromise } from '../../lib/as-promise' import { Callback } from '../events/interfaces' export function pTimeout( @@ -34,7 +33,7 @@ export function invokeCallback( ): Promise { const cb = () => { try { - return asPromise(callback(ctx)) + return Promise.resolve(callback(ctx)) } catch (err) { return Promise.reject(err) } diff --git a/packages/browser/src/lib/__tests__/as-promise.test.ts b/packages/browser/src/lib/__tests__/as-promise.test.ts deleted file mode 100644 index 021ab5844..000000000 --- a/packages/browser/src/lib/__tests__/as-promise.test.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { asPromise } from '../as-promise' - -describe('asPromise', () => { - it('wraps a value so it can be awaited', async () => { - expect(await asPromise(1)).toBe(1) - }) - - it('keeps promises as promises', async () => { - expect(await asPromise(Promise.resolve(1))).toBe(1) - }) - - it('works with functions', async () => { - expect((await asPromise(() => 1))()).toBe(1) - }) -}) diff --git a/packages/browser/src/lib/as-promise.ts b/packages/browser/src/lib/as-promise.ts deleted file mode 100644 index b0273432e..000000000 --- a/packages/browser/src/lib/as-promise.ts +++ /dev/null @@ -1,6 +0,0 @@ -/** - * Wraps an arbitrary value in a Promise so that it can be awaited on - */ -export function asPromise(value: T | PromiseLike): Promise { - return Promise.resolve(value) -} diff --git a/packages/browser/src/plugins/ajs-destination/index.ts b/packages/browser/src/plugins/ajs-destination/index.ts index 6bf2e8d99..c1e84b490 100644 --- a/packages/browser/src/plugins/ajs-destination/index.ts +++ b/packages/browser/src/plugins/ajs-destination/index.ts @@ -7,7 +7,6 @@ import { Context, ContextCancelation } from '../../core/context' import { isServer } from '../../core/environment' import { Plugin } from '../../core/plugin' import { attempt } from '../../core/queue/delivery' -import { asPromise } from '../../lib/as-promise' import { isPlanEventEnabled } from '../../lib/is-plan-event-enabled' import { mergedOptions } from '../../lib/merged-options' import { pWhile } from '../../lib/p-while' @@ -241,9 +240,7 @@ export class LegacyDestination implements Plugin { try { if (this.integration) { - await asPromise( - this.integration.invoke.call(this.integration, eventType, event) - ) + await this.integration.invoke.call(this.integration, eventType, event) } } catch (err) { ctx.stats.increment('analytics_js.integration.invoke.error', 1, [ diff --git a/packages/browser/src/plugins/middleware/__tests__/index.test.ts b/packages/browser/src/plugins/middleware/__tests__/index.test.ts index 0ceaacddb..9f01a7232 100644 --- a/packages/browser/src/plugins/middleware/__tests__/index.test.ts +++ b/packages/browser/src/plugins/middleware/__tests__/index.test.ts @@ -6,7 +6,6 @@ import { import { Analytics } from '../../../core/analytics' import { Context } from '../../../core/context' import { Plugin } from '../../../core/plugin' -import { asPromise } from '../../../lib/as-promise' import { LegacyDestination } from '../../ajs-destination' describe(sourceMiddlewarePlugin, () => { @@ -91,7 +90,10 @@ describe(sourceMiddlewarePlugin, () => { expect(returnedCtx).toBe(toReturn) const toCancel = new Context({ type: 'track' }) - await asPromise(hangsXT.track!(toCancel)).catch((err) => { + try { + await hangsXT.track!(toCancel) + throw new Error('should not reach here.') + } catch (err) { expect(err).toMatchInlineSnapshot(` ContextCancelation { "reason": "Middleware \`next\` function skipped", @@ -99,7 +101,7 @@ describe(sourceMiddlewarePlugin, () => { "type": "middleware_cancellation", } `) - }) + } }) }) diff --git a/packages/browser/src/plugins/middleware/index.ts b/packages/browser/src/plugins/middleware/index.ts index 2771492c7..9aa9c65ad 100644 --- a/packages/browser/src/plugins/middleware/index.ts +++ b/packages/browser/src/plugins/middleware/index.ts @@ -1,7 +1,6 @@ import { Context, ContextCancelation } from '../../core/context' import { SegmentEvent } from '../../core/events' import { Plugin } from '../../core/plugin' -import { asPromise } from '../../lib/as-promise' import { SegmentFacade, toFacade } from '../../lib/to-facade' export interface MiddlewareParams { @@ -17,10 +16,13 @@ export interface DestinationMiddlewareParams { next: (payload: MiddlewareParams['payload'] | null) => void } -export type MiddlewareFunction = (middleware: MiddlewareParams) => void +export type MiddlewareFunction = ( + middleware: MiddlewareParams +) => void | Promise + export type DestinationMiddlewareFunction = ( middleware: DestinationMiddlewareParams -) => void +) => void | Promise export async function applyDestinationMiddleware( destination: string, @@ -39,26 +41,24 @@ export async function applyDestinationMiddleware( let nextCalled = false let returnedEvent: SegmentEvent | null = null - await asPromise( - fn({ - payload: toFacade(event, { - clone: true, - traverse: false, - }), - integration: destination, - next(evt) { - nextCalled = true - - if (evt === null) { - returnedEvent = null - } - - if (evt) { - returnedEvent = evt.obj - } - }, - }) - ) + await fn({ + payload: toFacade(event, { + clone: true, + traverse: false, + }), + integration: destination, + next(evt) { + nextCalled = true + + if (evt === null) { + returnedEvent = null + } + + if (evt) { + returnedEvent = evt.obj + } + }, + }) if (!nextCalled && returnedEvent !== null) { returnedEvent = returnedEvent as SegmentEvent @@ -89,21 +89,19 @@ export function sourceMiddlewarePlugin( async function apply(ctx: Context): Promise { let nextCalled = false - await asPromise( - fn({ - payload: toFacade(ctx.event, { - clone: true, - traverse: false, - }), - integrations: integrations ?? {}, - next(evt) { - nextCalled = true - if (evt) { - ctx.event = evt.obj - } - }, - }) - ) + await fn({ + payload: toFacade(ctx.event, { + clone: true, + traverse: false, + }), + integrations: integrations ?? {}, + next(evt) { + nextCalled = true + if (evt) { + ctx.event = evt.obj + } + }, + }) if (!nextCalled) { throw new ContextCancelation({ diff --git a/packages/browser/src/plugins/remote-loader/index.ts b/packages/browser/src/plugins/remote-loader/index.ts index 3bdf807ee..d66811e55 100644 --- a/packages/browser/src/plugins/remote-loader/index.ts +++ b/packages/browser/src/plugins/remote-loader/index.ts @@ -2,7 +2,6 @@ import type { Integrations } from '../../core/events/interfaces' import { LegacySettings } from '../../browser' import { JSONObject, JSONValue } from '../../core/events' import { Plugin } from '../../core/plugin' -import { asPromise } from '../../lib/as-promise' import { loadScript } from '../../lib/load-script' import { getCDN } from '../../lib/parse-cdn' import { @@ -187,12 +186,10 @@ export async function remoteLoader( if (typeof window[libraryName] === 'function') { // @ts-expect-error const pluginFactory = window[libraryName] as PluginFactory - const plugin = await asPromise( - pluginFactory({ - ...remotePlugin.settings, - ...mergedIntegrations[remotePlugin.name], - }) - ) + const plugin = await pluginFactory({ + ...remotePlugin.settings, + ...mergedIntegrations[remotePlugin.name], + }) const plugins = Array.isArray(plugin) ? plugin : [plugin] validate(plugins) diff --git a/packages/browser/src/plugins/routing-middleware/__tests__/index.test.ts b/packages/browser/src/plugins/routing-middleware/__tests__/index.test.ts index 9170f7bc1..f9949156a 100644 --- a/packages/browser/src/plugins/routing-middleware/__tests__/index.test.ts +++ b/packages/browser/src/plugins/routing-middleware/__tests__/index.test.ts @@ -19,13 +19,13 @@ describe('tsub middleware', () => { const middleware = tsubMiddleware(rules) - it('can apply tsub operations', () => { + it('can apply tsub operations', async () => { let payload const next = jest.fn().mockImplementation((transformed) => { payload = transformed?.obj }) - middleware({ + await middleware({ integration: 'Google Tag Manager', next, payload: toFacade({ @@ -38,13 +38,13 @@ describe('tsub middleware', () => { expect(payload).toMatchInlineSnapshot(`null`) }) - it("won't match different destinations", () => { + it("won't match different destinations", async () => { let payload const next = jest.fn().mockImplementation((transformed) => { payload = transformed?.obj }) - middleware({ + await middleware({ integration: 'Google Analytics', next, payload: toFacade({