Skip to content

Commit

Permalink
First get stack when origin is accessed - fixes #273
Browse files Browse the repository at this point in the history
  • Loading branch information
porsager committed Mar 13, 2022
1 parent 8a1e3ee commit 0aacdb3
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/query.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const originCache = new Map()
, originStackCache = new Map()
, originError = Symbol('OriginError')

export const CLOSE = {}
export class Query extends Promise {
Expand Down Expand Up @@ -29,7 +31,17 @@ export class Query extends Promise {
this.executed = false
this.signature = ''

this.origin = handler.debug ? new Error().stack : cachedError(this.strings)
this[originError] = handler.debug || !this.tagged
? new Error()
: cachedError(this.strings)
}

get origin() {
return this.handler.debug || !this.tagged
? this[originError].stack
: originStackCache.has(this.strings)
? originStackCache.get(this.strings)
: originStackCache.set(this.strings, this[originError].stack).get(this.strings)
}

static get [Symbol.species]() {
Expand Down Expand Up @@ -143,7 +155,7 @@ function cachedError(xs) {

const x = Error.stackTraceLimit
Error.stackTraceLimit = 4
originCache.set(xs, new Error().stack)
originCache.set(xs, new Error())
Error.stackTraceLimit = x
return originCache.get(xs)
}

0 comments on commit 0aacdb3

Please sign in to comment.