From 73aad234b8219c69f3ec6008fc3d73be2255648d Mon Sep 17 00:00:00 2001 From: Akram Date: Sun, 26 Nov 2023 15:45:14 +0100 Subject: [PATCH] feat: use appendNarratives instead of deprecated pushNarrative --- src/trace/traceableExecution.spec.ts | 4 +-- src/trace/traceableExecution.ts | 38 ++++++++-------------------- 2 files changed, 13 insertions(+), 29 deletions(-) diff --git a/src/trace/traceableExecution.spec.ts b/src/trace/traceableExecution.spec.ts index c3ed574..e7b866d 100644 --- a/src/trace/traceableExecution.spec.ts +++ b/src/trace/traceableExecution.spec.ts @@ -215,7 +215,7 @@ describe('TraceableExecution', () => { config: { traceExecution: { narratives: ['Narrative 1 for function 2', 'Narrative 2 for function 2'] } } }); - traceableExecution.pushNarrative( + traceableExecution.appendNarratives( 'sampleFunction_custom_id_3', 'Narrative -1 for function 3, anticipated narrative before node creation' ); @@ -233,7 +233,7 @@ describe('TraceableExecution', () => { expect(trace?.length).toEqual(7); // Use pushNarrative to add a single narrative to the specified node - traceableExecution.pushNarrative(nodeId, 'Narrative 1'); + traceableExecution.appendNarratives(nodeId, 'Narrative 1'); // Check if the narrative was added successfully const nodeWithNarrative = traceableExecution.getTraceNodes().find((node) => node.data.id === nodeId); diff --git a/src/trace/traceableExecution.ts b/src/trace/traceableExecution.ts index f3cf360..884ceb1 100644 --- a/src/trace/traceableExecution.ts +++ b/src/trace/traceableExecution.ts @@ -268,44 +268,28 @@ export class TraceableExecution { } /** - * Push a string narrative to a trace node + * Pushes or appends narratives to a trace node. * @param nodeId - The ID of the node. - * @param narrative - The narrative to be pushed. + * @param narratives - The narrative or array of narratives to be processed. * @returns The updated instance of TraceableExecution. */ - pushNarrative(nodeId: NodeTrace['id'], narrative: string) { + appendNarratives(nodeId: NodeTrace['id'], narratives: string | string[]) { const existingNodeIndex = this.nodes?.findIndex((n) => n.data.id === nodeId); - if (existingNodeIndex >= 0) { - // it means that we are too late and node has already been created with narratives: - this.nodes[existingNodeIndex].data = { - ...this.nodes[existingNodeIndex]?.data, - narratives: [...(this.nodes[existingNodeIndex]?.data?.narratives ?? []), narrative] - }; - } else { - this.narrativesForNonFoundNodes[nodeId] = this.narrativesForNonFoundNodes[nodeId] ?? []; - this.narrativesForNonFoundNodes[nodeId]?.push(narrative); - } - - return this; - } - /** - * Appends an array of narratives to a trace node. - * @param nodeId - The ID of the node. - * @param narratives - An array of narratives to be appended. - * @returns The updated instance of TraceableExecution. - */ - appendNarratives(nodeId: NodeTrace['id'], narratives: Array) { - const existingNodeIndex = this.nodes?.findIndex((n) => n.data.id === nodeId); if (existingNodeIndex >= 0) { - // it means that we are too late and node has already been created with narratives: + // Node already exists, update its narratives this.nodes[existingNodeIndex].data = { ...this.nodes[existingNodeIndex]?.data, - narratives: (this.nodes[existingNodeIndex]?.data?.narratives ?? [])?.concat(narratives) + narratives: [...(this.nodes[existingNodeIndex]?.data?.narratives ?? []), ...(Array.isArray(narratives) ? narratives : [narratives])] }; } else { - this.narrativesForNonFoundNodes[nodeId] = (this.narrativesForNonFoundNodes[nodeId] ?? [])?.concat(narratives); + // Node doesn't exist, store narratives for later use + this.narrativesForNonFoundNodes[nodeId] = [ + ...(this.narrativesForNonFoundNodes[nodeId] ?? []), + ...(Array.isArray(narratives) ? narratives : [narratives]) + ]; } + return this; }