@loglayer/context-manager-linked
Version:
Context manager for loglayer that keeps context between parent and all children.
1 lines • 4.55 kB
Source Map (JSON)
{"version":3,"sources":["/home/runner/work/loglayer/loglayer/packages/context-managers/linked/dist/index.cjs","../src/LinkedContextManager.ts"],"names":[],"mappings":"AAAA;ACKO,IAAM,qBAAA,YAAN,MAAM,sBAAgD;AAAA,iBACnD,iBAAA,EAAuE;AAAA,IAC7E,IAAA,EAAM,CAAC,CAAA;AAAA,IACP,UAAA,EAAY;AAAA,EACd,EAAA;AAAA;AAAA;AAAA;AAAA,EAKA,UAAA,CAAW,OAAA,EAAqC;AAC9C,IAAA,GAAA,CAAI,CAAC,OAAA,EAAS;AAEZ,MAAA,IAAA,CAAA,MAAW,IAAA,GAAO,IAAA,CAAK,gBAAA,CAAiB,IAAA,EAAM;AAC5C,QAAA,GAAA,CAAI,MAAA,CAAO,SAAA,CAAU,cAAA,CAAe,IAAA,CAAK,IAAA,CAAK,gBAAA,CAAiB,IAAA,EAAM,GAAG,CAAA,EAAG;AACzE,UAAA,OAAO,IAAA,CAAK,gBAAA,CAAiB,IAAA,CAAK,GAAG,CAAA;AAAA,QACvC;AAAA,MACF;AAEA,MAAA,IAAA,CAAK,gBAAA,CAAiB,WAAA,EAAa,KAAA;AACnC,MAAA,MAAA;AAAA,IACF;AAGA,IAAA,MAAA,CAAO,MAAA,CAAO,IAAA,CAAK,gBAAA,CAAiB,IAAA,EAAM,OAAO,CAAA;AACjD,IAAA,IAAA,CAAK,gBAAA,CAAiB,WAAA,EAAa,IAAA;AAAA,EACrC;AAAA;AAAA;AAAA;AAAA,EAKA,aAAA,CAAc,OAAA,EAA8B;AAC1C,IAAA,MAAA,CAAO,MAAA,CAAO,IAAA,CAAK,gBAAA,CAAiB,IAAA,EAAM,OAAO,CAAA;AACjD,IAAA,IAAA,CAAK,gBAAA,CAAiB,WAAA,EAAa,IAAA;AAAA,EACrC;AAAA;AAAA;AAAA;AAAA,EAKA,UAAA,CAAA,EAAkC;AAChC,IAAA,OAAO,IAAA,CAAK,gBAAA,CAAiB,IAAA;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA,EAKA,cAAA,CAAA,EAA0B;AACxB,IAAA,OAAO,IAAA,CAAK,gBAAA,CAAiB,UAAA;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA,EAKA,oBAAA,CAAqB,EAAE,oBAAA,EAAsB,oBAAoB,CAAA,EAA+B;AAE9F,IAAA,GAAA,CAAI,oBAAA,WAA+B,qBAAA,EAAsB;AACvD,MAAC,mBAAA,CAA6C,iBAAA,EAAmB,IAAA,CAAK,gBAAA;AAAA,IACxE,EAAA,KAAO;AACL,MAAA,mBAAA,CAAoB,UAAA,CAAW,IAAA,CAAK,UAAA,CAAW,CAAC,CAAA;AAAA,IAClD;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,KAAA,CAAA,EAAyB;AACvB,IAAA,MAAM,MAAA,EAAQ,IAAI,qBAAA,CAAqB,CAAA;AAEvC,IAAA,KAAA,CAAM,iBAAA,EAAmB,IAAA,CAAK,gBAAA;AAC9B,IAAA,OAAO,KAAA;AAAA,EACT;AACF,UAAA;ADfA;AACE;AACF,oDAAC","file":"/home/runner/work/loglayer/loglayer/packages/context-managers/linked/dist/index.cjs","sourcesContent":[null,"import type { IContextManager, OnChildLoggerCreatedParams } from \"@loglayer/context-manager\";\n\n/**\n * A context manager that keeps context data synchronized between parent and all children (bi-directional).\n */\nexport class LinkedContextManager implements IContextManager {\n private contextContainer: { data: Record<string, any>; hasContext: boolean } = {\n data: {},\n hasContext: false,\n };\n\n /**\n * Sets the context data to be included with every log entry. Set to `undefined` to clear the context data.\n */\n setContext(context?: Record<string, any>): void {\n if (!context) {\n // We can't just assign an empty object else that would break the link between parent and child.\n for (const key in this.contextContainer.data) {\n if (Object.prototype.hasOwnProperty.call(this.contextContainer.data, key)) {\n delete this.contextContainer.data[key];\n }\n }\n\n this.contextContainer.hasContext = false;\n return;\n }\n\n // We can't just directly replace the context object else that would break the link between parent and child.\n Object.assign(this.contextContainer.data, context);\n this.contextContainer.hasContext = true;\n }\n\n /**\n * Appends context data to the existing context data.\n */\n appendContext(context: Record<string, any>) {\n Object.assign(this.contextContainer.data, context);\n this.contextContainer.hasContext = true;\n }\n\n /**\n * Returns the context data to be included with every log entry.\n */\n getContext(): Record<string, any> {\n return this.contextContainer.data;\n }\n\n /**\n * Returns true if context data is present.\n */\n hasContextData(): boolean {\n return this.contextContainer.hasContext;\n }\n\n /**\n * Links the child context manager's context to be the same as the parent context manager's context.\n */\n onChildLoggerCreated({ parentContextManager, childContextManager }: OnChildLoggerCreatedParams) {\n // Share the same context container between parent and child for bi-directional linking\n if (childContextManager instanceof LinkedContextManager) {\n (childContextManager as LinkedContextManager).contextContainer = this.contextContainer;\n } else {\n childContextManager.setContext(this.getContext());\n }\n }\n\n /**\n * Creates a new instance of the context manager that shares the same context data.\n * The clone maintains a bi-directional link with the original context manager.\n */\n clone(): IContextManager {\n const clone = new LinkedContextManager();\n // Share the same context container to maintain bi-directional linking\n clone.contextContainer = this.contextContainer;\n return clone;\n }\n}\n"]}