Skip to content

Commit

Permalink
browser: refactor/remove asPromise (#686)
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky authored Dec 2, 2022
1 parent db38bb3 commit c5d2a99
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 77 deletions.
3 changes: 1 addition & 2 deletions packages/browser/src/core/callback/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Context } from '../context'
import { asPromise } from '../../lib/as-promise'
import { Callback } from '../events/interfaces'

export function pTimeout(
Expand Down Expand Up @@ -34,7 +33,7 @@ export function invokeCallback(
): Promise<Context> {
const cb = () => {
try {
return asPromise(callback(ctx))
return Promise.resolve(callback(ctx))
} catch (err) {
return Promise.reject(err)
}
Expand Down
15 changes: 0 additions & 15 deletions packages/browser/src/lib/__tests__/as-promise.test.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/browser/src/lib/as-promise.ts

This file was deleted.

5 changes: 1 addition & 4 deletions packages/browser/src/plugins/ajs-destination/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -261,9 +260,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, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {
Expand Down Expand Up @@ -91,15 +90,18 @@ 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",
"retry": false,
"type": "middleware_cancellation",
}
`)
})
}
})
})

Expand Down
74 changes: 36 additions & 38 deletions packages/browser/src/plugins/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<void>

export type DestinationMiddlewareFunction = (
middleware: DestinationMiddlewareParams
) => void
) => void | Promise<void>

export async function applyDestinationMiddleware(
destination: string,
Expand All @@ -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
Expand Down Expand Up @@ -89,21 +89,19 @@ export function sourceMiddlewarePlugin(
async function apply(ctx: Context): Promise<Context> {
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({
Expand Down
11 changes: 4 additions & 7 deletions packages/browser/src/plugins/remote-loader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('tsub middleware', () => {
payload = transformed?.obj
})

middleware({
void middleware({
integration: 'Google Tag Manager',
next,
payload: toFacade({
Expand All @@ -44,7 +44,7 @@ describe('tsub middleware', () => {
payload = transformed?.obj
})

middleware({
void middleware({
integration: 'Google Analytics',
next,
payload: toFacade({
Expand Down

0 comments on commit c5d2a99

Please sign in to comment.