Skip to content

Commit

Permalink
add non-traced overload to Effect.fn (#4077)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored and gcanti committed Dec 10, 2024
1 parent 52a4b66 commit 98324d0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-mice-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

add non-traced overload to Effect.fn
94 changes: 58 additions & 36 deletions packages/effect/src/Effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10075,49 +10075,71 @@ export namespace fn {
* @since 3.11.0
* @category function
*/
export const fn: (
name: string,
options?: Tracer.SpanOptions
) => fn.Gen & fn.NonGen = (name, options) => (body: Function, ...pipeables: Array<any>) => {
return function(this: any, ...args: Array<any>) {
const limit = Error.stackTraceLimit
Error.stackTraceLimit = 2
const error = new Error()
Error.stackTraceLimit = limit
let cache: false | string = false
const captureStackTrace = () => {
if (cache !== false) {
return cache
}
if (error.stack) {
const stack = error.stack.trim().split("\n")
cache = stack.slice(2).join("\n").trim()
return cache
export const fn:
& fn.Gen
& fn.NonGen
& ((
name: string,
options?: Tracer.SpanOptions
) => fn.Gen & fn.NonGen) = function(nameOrBody: Function | string, ...pipeables: Array<any>) {
if (typeof nameOrBody !== "string") {
return function(this: any) {
return fnApply(this, nameOrBody, arguments as any, pipeables)
} as any
}
const name = nameOrBody
const options = pipeables[0]
return (body: Function, ...pipeables: Array<any>) => {
return function(this: any, ...args: Array<any>) {
const limit = Error.stackTraceLimit
Error.stackTraceLimit = 2
const error = new Error()
Error.stackTraceLimit = limit
let cache: false | string = false
const captureStackTrace = () => {
if (cache !== false) {
return cache
}
if (error.stack) {
const stack = error.stack.trim().split("\n")
cache = stack.slice(2).join("\n").trim()
return cache
}
}
const effect = fnApply(this, body, args, pipeables)
const opts: any = (options && "captureStackTrace" in options) ? options : { captureStackTrace, ...options }
return withSpan(effect, name, opts)
}
}
let effect: Effect<any, any, any>
let fnError: any = undefined
}

function fnApply(self: any, body: Function, args: Array<any>, pipeables: Array<any>) {
let effect: Effect<any, any, any>
let fnError: any = undefined
if (isGeneratorFunction(body)) {
effect = gen(() => internalCall(() => body.apply(self, args)))
} else {
try {
effect = isGeneratorFunction(body)
? gen(() => internalCall(() => body.apply(this, args)))
: body.apply(this, args)
effect = body.apply(self, args)
} catch (error) {
fnError = error
effect = die(error)
}
try {
for (const x of pipeables) {
effect = x(effect)
}
} catch (error) {
effect = fnError
? failCause(internalCause.sequential(
internalCause.die(fnError),
internalCause.die(error)
))
: die(error)
}
if (pipeables.length === 0) {
return effect
}
try {
for (const x of pipeables) {
effect = x(effect)
}
const opts: any = (options && "captureStackTrace" in options) ? options : { captureStackTrace, ...options }
return withSpan(effect, name, opts)
} catch (error) {
effect = fnError
? failCause(internalCause.sequential(
internalCause.die(fnError),
internalCause.die(error)
))
: die(error)
}
return effect
}

0 comments on commit 98324d0

Please sign in to comment.