From 0aacdb3231e3b725276f71613d74d9cb6daefc18 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Sun, 13 Mar 2022 20:55:48 +0100 Subject: [PATCH] First get stack when origin is accessed - fixes #273 --- src/query.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/query.js b/src/query.js index 36b0748c..513c044a 100644 --- a/src/query.js +++ b/src/query.js @@ -1,4 +1,6 @@ const originCache = new Map() + , originStackCache = new Map() + , originError = Symbol('OriginError') export const CLOSE = {} export class Query extends Promise { @@ -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]() { @@ -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) }