Skip to content

Commit aa79918

Browse files
committed
fix(TRI-5140): maintain proper context in metadata.root and parent getters
1 parent 61232ab commit aa79918

File tree

2 files changed

+72
-48
lines changed

2 files changed

+72
-48
lines changed

Diff for: packages/core/src/v3/runMetadata/manager.ts

+44-32
Original file line numberDiff line numberDiff line change
@@ -31,71 +31,83 @@ export class StandardMetadataManager implements RunMetadataManager {
3131
) {}
3232

3333
get parent(): RunMetadataUpdater {
34-
return {
34+
// Store a reference to 'this' to ensure proper context
35+
const self = this;
36+
37+
// Create the updater object and store it in a local variable
38+
const parentUpdater: RunMetadataUpdater = {
3539
set: (key, value) => {
36-
this.queuedParentOperations.add({ type: "set", key, value });
37-
return this.parent;
40+
self.queuedParentOperations.add({ type: "set", key, value });
41+
return parentUpdater;
3842
},
3943
del: (key) => {
40-
this.queuedParentOperations.add({ type: "delete", key });
41-
return this.parent;
44+
self.queuedParentOperations.add({ type: "delete", key });
45+
return parentUpdater;
4246
},
4347
append: (key, value) => {
44-
this.queuedParentOperations.add({ type: "append", key, value });
45-
return this.parent;
48+
self.queuedParentOperations.add({ type: "append", key, value });
49+
return parentUpdater;
4650
},
4751
remove: (key, value) => {
48-
this.queuedParentOperations.add({ type: "remove", key, value });
49-
return this.parent;
52+
self.queuedParentOperations.add({ type: "remove", key, value });
53+
return parentUpdater;
5054
},
5155
increment: (key, value) => {
52-
this.queuedParentOperations.add({ type: "increment", key, value });
53-
return this.parent;
56+
self.queuedParentOperations.add({ type: "increment", key, value });
57+
return parentUpdater;
5458
},
5559
decrement: (key, value) => {
56-
this.queuedParentOperations.add({ type: "increment", key, value: -Math.abs(value) });
57-
return this.parent;
60+
self.queuedParentOperations.add({ type: "increment", key, value: -Math.abs(value) });
61+
return parentUpdater;
5862
},
5963
update: (value) => {
60-
this.queuedParentOperations.add({ type: "update", value });
61-
return this.parent;
64+
self.queuedParentOperations.add({ type: "update", value });
65+
return parentUpdater;
6266
},
63-
stream: (key, value, signal) => this.doStream(key, value, "parent", this.parent, signal),
67+
stream: (key, value, signal) => self.doStream(key, value, "parent", parentUpdater, signal),
6468
};
69+
70+
return parentUpdater;
6571
}
6672

6773
get root(): RunMetadataUpdater {
68-
return {
74+
// Store a reference to 'this' to ensure proper context
75+
const self = this;
76+
77+
// Create the updater object and store it in a local variable
78+
const rootUpdater: RunMetadataUpdater = {
6979
set: (key, value) => {
70-
this.queuedRootOperations.add({ type: "set", key, value });
71-
return this.root;
80+
self.queuedRootOperations.add({ type: "set", key, value });
81+
return rootUpdater;
7282
},
7383
del: (key) => {
74-
this.queuedRootOperations.add({ type: "delete", key });
75-
return this.root;
84+
self.queuedRootOperations.add({ type: "delete", key });
85+
return rootUpdater;
7686
},
7787
append: (key, value) => {
78-
this.queuedRootOperations.add({ type: "append", key, value });
79-
return this.root;
88+
self.queuedRootOperations.add({ type: "append", key, value });
89+
return rootUpdater;
8090
},
8191
remove: (key, value) => {
82-
this.queuedRootOperations.add({ type: "remove", key, value });
83-
return this.root;
92+
self.queuedRootOperations.add({ type: "remove", key, value });
93+
return rootUpdater;
8494
},
8595
increment: (key, value) => {
86-
this.queuedRootOperations.add({ type: "increment", key, value });
87-
return this.root;
96+
self.queuedRootOperations.add({ type: "increment", key, value });
97+
return rootUpdater;
8898
},
8999
decrement: (key, value) => {
90-
this.queuedRootOperations.add({ type: "increment", key, value: -Math.abs(value) });
91-
return this.root;
100+
self.queuedRootOperations.add({ type: "increment", key, value: -Math.abs(value) });
101+
return rootUpdater;
92102
},
93103
update: (value) => {
94-
this.queuedRootOperations.add({ type: "update", value });
95-
return this.root;
104+
self.queuedRootOperations.add({ type: "update", value });
105+
return rootUpdater;
96106
},
97-
stream: (key, value, signal) => this.doStream(key, value, "root", this.root, signal),
107+
stream: (key, value, signal) => self.doStream(key, value, "root", rootUpdater, signal),
98108
};
109+
110+
return rootUpdater;
99111
}
100112

101113
public enterWithMetadata(metadata: Record<string, DeserializedJson>): void {

Diff for: packages/core/src/v3/runMetadata/noopManager.ts

+28-16
Original file line numberDiff line numberDiff line change
@@ -46,38 +46,50 @@ export class NoopRunMetadataManager implements RunMetadataManager {
4646
}
4747

4848
get parent(): RunMetadataUpdater {
49-
return {
50-
append: () => this.parent,
51-
set: () => this.parent,
52-
del: () => this.parent,
53-
increment: () => this.parent,
54-
decrement: () => this.parent,
55-
remove: () => this.parent,
49+
// Store a reference to this object
50+
const self = this;
51+
52+
// Create a local reference to ensure proper context
53+
const parentUpdater: RunMetadataUpdater = {
54+
append: () => parentUpdater,
55+
set: () => parentUpdater,
56+
del: () => parentUpdater,
57+
increment: () => parentUpdater,
58+
decrement: () => parentUpdater,
59+
remove: () => parentUpdater,
5660
stream: () =>
5761
Promise.resolve({
5862
[Symbol.asyncIterator]: () => ({
5963
next: () => Promise.resolve({ done: true, value: undefined }),
6064
}),
6165
}),
62-
update: () => this.parent,
66+
update: () => parentUpdater,
6367
};
68+
69+
return parentUpdater;
6470
}
6571

6672
get root(): RunMetadataUpdater {
67-
return {
68-
append: () => this.root,
69-
set: () => this.root,
70-
del: () => this.root,
71-
increment: () => this.root,
72-
decrement: () => this.root,
73-
remove: () => this.root,
73+
// Store a reference to this object
74+
const self = this;
75+
76+
// Create a local reference to ensure proper context
77+
const rootUpdater: RunMetadataUpdater = {
78+
append: () => rootUpdater,
79+
set: () => rootUpdater,
80+
del: () => rootUpdater,
81+
increment: () => rootUpdater,
82+
decrement: () => rootUpdater,
83+
remove: () => rootUpdater,
7484
stream: () =>
7585
Promise.resolve({
7686
[Symbol.asyncIterator]: () => ({
7787
next: () => Promise.resolve({ done: true, value: undefined }),
7888
}),
7989
}),
80-
update: () => this.root,
90+
update: () => rootUpdater,
8191
};
92+
93+
return rootUpdater;
8294
}
8395
}

0 commit comments

Comments
 (0)