Skip to content

Commit

Permalink
remove as-promise
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Nov 19, 2022
1 parent 64ae9ba commit 2a17b84
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 49 deletions.
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 @@ -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({
Expand All @@ -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({
Expand Down

0 comments on commit 2a17b84

Please sign in to comment.