Skip to content

Commit bc183e6

Browse files
committed
Handle dup evals of same flag
1 parent 5453c1f commit bc183e6

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

packages/browser/src/utils/featureFlags.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ export function copyFlagsFromScopeToEvent(event: Event): Event {
5353
*
5454
* @param name Name of the feature flag to insert.
5555
* @param value Value of the feature flag.
56-
* @param maxSize Max number of flags the buffer should store. It's recommended
57-
* to keep this consistent across insertions. Default is FLAG_BUFFER_SIZE
56+
* @param maxSize Max number of flags the buffer should store. Default value should always be used in production.
5857
*/
5958
export function insertFlagToScope(name: string, value: unknown, maxSize: number = FLAG_BUFFER_SIZE): void {
6059
const scopeContexts = getCurrentScope().getScopeData().contexts;
@@ -68,7 +67,7 @@ export function insertFlagToScope(name: string, value: unknown, maxSize: number
6867
/**
6968
* Exported for tests. Currently only accepts boolean values (otherwise no-op).
7069
*/
71-
export function insertToFlagBuffer(flags: FeatureFlag[], name: string, value: unknown, maxSize: number): void {
70+
export function insertToFlagBuffer(flags: FeatureFlag[], name: string, value: unknown, maxSize: number, allowEviction: boolean = true): void {
7271
if (typeof value !== 'boolean') {
7372
return;
7473
}
@@ -87,8 +86,12 @@ export function insertToFlagBuffer(flags: FeatureFlag[], name: string, value: un
8786
}
8887

8988
if (flags.length === maxSize) {
90-
// If at capacity, pop the earliest flag - O(n)
91-
flags.shift();
89+
if (allowEviction) {
90+
// If at capacity, pop the earliest flag - O(n)
91+
flags.shift();
92+
} else {
93+
return;
94+
}
9295
}
9396

9497
// Push the flag to the end - O(1)
@@ -119,9 +122,7 @@ export function bufferSpanFeatureFlag(
119122
const span = getActiveSpan();
120123
if (span) {
121124
const flags = spanFlagMap.get(span) || [];
122-
if (!flags.find(flag => flag.flag === name) && flags.length < maxFlagsPerSpan) {
123-
flags.push({ flag: name, result: value });
124-
}
125+
insertToFlagBuffer(flags, name, value, maxFlagsPerSpan, false);
125126
spanFlagMap.set(span, flags);
126127
}
127128
}

packages/core/src/utils-hoist/worldwide.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export type InternalGlobal = {
6161
/**
6262
* A map of spans to feature flag buffers. Populated by feature flag integrations.
6363
*/
64-
_spanToFlagBufferMap?: WeakMap<Span, FeatureFlag[]>;
64+
_spanToFlagBufferMap?: WeakMap<Span, Set<FeatureFlag>>;
6565
} & Carrier;
6666

6767
/** Get's the global object for the current JavaScript runtime */

0 commit comments

Comments
 (0)