From 140e12e6cae821e68196c8b926b060f89ebcc264 Mon Sep 17 00:00:00 2001 From: Bastien Caudan Date: Wed, 26 May 2021 18:14:34 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8C=20buffer=20closure=20directly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/tools/boundedBuffer.spec.ts | 15 ++++++------ packages/core/src/tools/boundedBuffer.ts | 8 +++---- packages/logs/src/boot/logs.entry.ts | 4 +--- packages/rum-core/src/boot/rumPublicApi.ts | 24 +++++++------------ 4 files changed, 21 insertions(+), 30 deletions(-) diff --git a/packages/core/src/tools/boundedBuffer.spec.ts b/packages/core/src/tools/boundedBuffer.spec.ts index 716fa5ee5e..cd132c0c0d 100644 --- a/packages/core/src/tools/boundedBuffer.spec.ts +++ b/packages/core/src/tools/boundedBuffer.spec.ts @@ -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(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(i, spy) + buffered.add(spy) } buffered.drain() - expect(spy.calls.allArgs()).toEqual([[5], [6], [7], [8], [9]]) + expect(spy.calls.count()).toEqual(5) }) }) diff --git a/packages/core/src/tools/boundedBuffer.ts b/packages/core/src/tools/boundedBuffer.ts index 42cc6a2f5c..bdcdabfba6 100644 --- a/packages/core/src/tools/boundedBuffer.ts +++ b/packages/core/src/tools/boundedBuffer.ts @@ -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(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 } } diff --git a/packages/logs/src/boot/logs.entry.ts b/packages/logs/src/boot/logs.entry.ts index f6c9c05243..26ee2f269a 100644 --- a/packages/logs/src/boot/logs.entry.ts +++ b/packages/logs/src/boot/logs.entry.ts @@ -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) diff --git a/packages/rum-core/src/boot/rumPublicApi.ts b/packages/rum-core/src/boot/rumPublicApi.ts index ac647d99fa..52dcb9cdb2 100644 --- a/packages/rum-core/src/boot/rumPublicApi.ts +++ b/packages/rum-core/src/boot/rumPublicApi.ts @@ -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' @@ -47,23 +44,20 @@ export function makeRumPublicApi(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 {