Skip to content

Commit 29c721c

Browse files
♻️ [PANA-5948] Make some small improvements to ItemId management (#4164)
1 parent 0ac2a6c commit 29c721c

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
lines changed

packages/rum/src/domain/record/itemIds.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,33 @@ describe('ItemIds', () => {
5151
})
5252
})
5353

54+
describe('delete', () => {
55+
it('allows an item to be re-inserted and receive a new id', () => {
56+
const item = createItem()
57+
expect(itemIds.getOrInsert(item)).toBe(firstId)
58+
expect(itemIds.getOrInsert(item)).toBe(firstId)
59+
60+
itemIds.delete(item)
61+
62+
expect(itemIds.getOrInsert(item)).toBe((firstId + 1) as ItemId)
63+
expect(itemIds.getOrInsert(item)).toBe((firstId + 1) as ItemId)
64+
})
65+
66+
it('does not change the next assigned id', () => {
67+
const item = createItem()
68+
expect(itemIds.getOrInsert(item)).toBe(firstId)
69+
expect(itemIds.getOrInsert(item)).toBe(firstId)
70+
expect(itemIds.nextId).toBe((firstId + 1) as ItemId)
71+
72+
itemIds.delete(item)
73+
expect(itemIds.nextId).toBe((firstId + 1) as ItemId)
74+
75+
const newItem = createItem()
76+
expect(itemIds.getOrInsert(newItem)).toBe((firstId + 1) as ItemId)
77+
expect(itemIds.getOrInsert(newItem)).toBe((firstId + 1) as ItemId)
78+
})
79+
})
80+
5481
describe('get', () => {
5582
it('returns undefined for items that have not been assigned an id', () => {
5683
expect(itemIds.get(createItem())).toBe(undefined)
@@ -82,6 +109,21 @@ describe('ItemIds', () => {
82109
})
83110
})
84111

112+
describe('nextId getter', () => {
113+
it('initially returns the first id', () => {
114+
expect(itemIds.nextId).toBe(firstId)
115+
})
116+
117+
it('increments after each insertion', () => {
118+
for (let id = firstId; id < firstId + 3; id++) {
119+
const item = createItem()
120+
expect(itemIds.getOrInsert(item)).toBe(id)
121+
expect(itemIds.getOrInsert(item)).toBe(id)
122+
expect(itemIds.nextId).toBe((id + 1) as ItemId)
123+
}
124+
})
125+
})
126+
85127
describe('size', () => {
86128
it('increments when an id is assigned', () => {
87129
expect(itemIds.size).toBe(0)

packages/rum/src/domain/record/itemIds.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ export function createStyleSheetIds(): StyleSheetIds {
3636

3737
export interface ItemIds<ItemType, ItemId extends number> {
3838
clear(this: void): void
39+
delete(this: void, item: ItemType): void
3940
get(this: void, item: ItemType): ItemId | undefined
4041
getOrInsert(this: void, item: ItemType): ItemId
42+
get nextId(): ItemId
4143
get size(): number
4244
}
4345

@@ -50,6 +52,7 @@ function createWeakIdMap<ItemType extends object, ItemId extends number>(firstId
5052
}
5153

5254
interface MapLike<Key, Value> {
55+
delete(key: Key): void
5356
get(key: Key): Value | undefined
5457
set(key: Key, value: Value): void
5558
}
@@ -65,9 +68,15 @@ function createItemIds<ItemType, ItemId extends number>(
6568

6669
return {
6770
clear(): void {
71+
if (nextId === firstId) {
72+
return
73+
}
6874
map = createMap()
6975
nextId = firstId
7076
},
77+
delete(object: ItemType): void {
78+
map.delete(object)
79+
},
7180
get,
7281
getOrInsert(object: ItemType): ItemId {
7382
// Try to reuse any existing id.
@@ -78,6 +87,9 @@ function createItemIds<ItemType, ItemId extends number>(
7887
}
7988
return id
8089
},
90+
get nextId(): ItemId {
91+
return nextId
92+
},
8193
get size(): number {
8294
return nextId - firstId
8395
},

packages/rum/src/domain/record/recordingScope.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ export function createRecordingScope(
3232
const nodeIds = createNodeIds()
3333
const stringIds = createStringIds()
3434
const styleSheetIds = createStyleSheetIds()
35-
return {
35+
36+
const scope: RecordingScope = {
3637
resetIds(): void {
37-
eventIds.clear()
38-
nodeIds.clear()
39-
stringIds.clear()
40-
styleSheetIds.clear()
38+
scope.eventIds.clear()
39+
scope.nodeIds.clear()
40+
scope.stringIds.clear()
41+
scope.styleSheetIds.clear()
4142
},
4243

4344
configuration,
@@ -48,4 +49,6 @@ export function createRecordingScope(
4849
stringIds,
4950
styleSheetIds,
5051
}
52+
53+
return scope
5154
}

packages/rum/src/domain/record/startFullSnapshots.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ export function takeFullSnapshot(
6969
})
7070

7171
if (isExperimentalFeatureEnabled(ExperimentalFeature.USE_CHANGE_RECORDS)) {
72-
if (kind === SerializationKind.SUBSEQUENT_FULL_SNAPSHOT) {
73-
scope.resetIds()
74-
}
72+
scope.resetIds()
7573
serializeChangesInTransaction(
7674
kind,
7775
emitRecord,

0 commit comments

Comments
 (0)