Skip to content

Commit

Permalink
👌 buffer closure directly
Browse files Browse the repository at this point in the history
  • Loading branch information
bcaudan committed May 26, 2021
1 parent 0e3c03e commit 140e12e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 30 deletions.
15 changes: 7 additions & 8 deletions packages/core/src/tools/boundedBuffer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
import { BoundedBuffer } from './boundedBuffer'

describe('BoundedBuffer', () => {
it('collect and drain the items', () => {
const spy = jasmine.createSpy<(i: number) => void>()
it('collect and drain the callbacks', () => {
const spy = jasmine.createSpy<() => void>()
const buffered = new BoundedBuffer()

buffered.add<number>(1, spy)
buffered.add(spy)
expect(spy.calls.count()).toBe(0)

buffered.drain()
expect(spy.calls.count()).toBe(1)
expect(spy.calls.first().args).toEqual([1])

buffered.drain()
expect(spy.calls.count()).toBe(1)
})

it('store only the N last items', () => {
const spy = jasmine.createSpy<(i: number) => void>()
it('store only the N last callbacks', () => {
const spy = jasmine.createSpy<() => void>()
const buffered = new BoundedBuffer(5)

for (let i = 0; i < 10; i += 1) {
buffered.add<number>(i, spy)
buffered.add(spy)
}

buffered.drain()
expect(spy.calls.allArgs()).toEqual([[5], [6], [7], [8], [9]])
expect(spy.calls.count()).toEqual(5)
})
})
8 changes: 4 additions & 4 deletions packages/core/src/tools/boundedBuffer.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const DEFAULT_LIMIT = 10_000

export class BoundedBuffer {
private buffer: Array<[any, (item: any) => void]> = []
private buffer: Array<() => void> = []

constructor(private limit: number = DEFAULT_LIMIT) {}

add<T>(item: T, drainFn: (item: T) => void) {
const length = this.buffer.push([item, drainFn])
add(callback: () => void) {
const length = this.buffer.push(callback)
if (length > this.limit) {
this.buffer.splice(0, 1)
}
}

drain() {
this.buffer.forEach(([item, drainFn]) => drainFn(item))
this.buffer.forEach((callback) => callback())
this.buffer.length = 0
}
}
4 changes: 1 addition & 3 deletions packages/logs/src/boot/logs.entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ export function makeLogsPublicApi(startLogsImpl: StartLogs) {

const beforeInitSendLog = new BoundedBuffer()
let sendLogStrategy = (message: LogsMessage, currentContext: Context) => {
beforeInitSendLog.add<[LogsMessage, Context]>([message, currentContext], ([message, context]) =>
sendLogStrategy(message, context)
)
beforeInitSendLog.add(() => sendLogStrategy(message, currentContext))
}
const logger = new Logger(sendLog)

Expand Down
24 changes: 9 additions & 15 deletions packages/rum-core/src/boot/rumPublicApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ import {
monitor,
UserConfiguration,
clocksNow,
TimeStamp,
timeStampNow,
ClocksState,
display,
} from '@datadog/browser-core'
import { CustomAction } from '../domain/rumEventsCollection/action/trackActions'
import { ProvidedError, ProvidedSource } from '../domain/rumEventsCollection/error/errorCollection'
import { ProvidedSource } from '../domain/rumEventsCollection/error/errorCollection'
import { CommonContext, User, ActionType } from '../rawRumEvent.types'
import { RumEvent } from '../rumEvent.types'
import { startRum } from './rum'
Expand Down Expand Up @@ -47,23 +44,20 @@ export function makeRumPublicApi<C extends RumUserConfiguration>(startRumImpl: S

const beforeInitApiCalls = new BoundedBuffer()
let addTimingStrategy: StartRumResult['addTiming'] = (name) => {
beforeInitApiCalls.add<[string, TimeStamp]>([name, timeStampNow()], ([name, time]) => addTimingStrategy(name, time))
const time = timeStampNow()
beforeInitApiCalls.add(() => addTimingStrategy(name, time))
}
let startViewStrategy: StartRumResult['startView'] = (name) => {
beforeInitApiCalls.add<[string | undefined, ClocksState]>([name, clocksNow()], ([name, startClocks]) =>
startViewStrategy(name, startClocks)
)
const startClocks = clocksNow()
beforeInitApiCalls.add(() => startViewStrategy(name, startClocks))
}
let addActionStrategy: StartRumResult['addAction'] = (action) => {
beforeInitApiCalls.add<[CustomAction, CommonContext]>([action, clonedCommonContext()], ([action, commonContext]) =>
addActionStrategy(action, commonContext)
)
const commonContext = clonedCommonContext()
beforeInitApiCalls.add(() => addActionStrategy(action, commonContext))
}
let addErrorStrategy: StartRumResult['addError'] = (providedError) => {
beforeInitApiCalls.add<[ProvidedError, CommonContext]>(
[providedError, clonedCommonContext()],
([error, commonContext]) => addErrorStrategy(error, commonContext)
)
const commonContext = clonedCommonContext()
beforeInitApiCalls.add(() => addErrorStrategy(providedError, commonContext))
}

function clonedCommonContext(): CommonContext {
Expand Down

0 comments on commit 140e12e

Please sign in to comment.