Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

browser: refactor/remove asPromise #686

Merged
merged 1 commit into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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, [
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({
Copy link
Contributor Author

@silesky silesky Nov 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that the original intention to use await asPromise here was because MiddlewareFunction and SourceMiddleware could return a Promise -- and maybe whoever wrote this originally didn't realize that void | Promise<void> is a valid return type -- or that there's no requirement to "normalize" all values to a promise in order to await -- that you can just as easily await a regular value as a promise (await 123=== await Promise.resolve(123)).

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