Skip to content

Commit

Permalink
👌 [RUMF-1267] collocate remaining types from domain/record/types
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitZugmeyer committed May 31, 2022
1 parent 2b97076 commit bf2085f
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 132 deletions.
1 change: 0 additions & 1 deletion packages/rum/src/domain/record/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { record } from './record'
export { serializeNodeWithId, serializeDocument } from './serialize'
export * from './types'
2 changes: 1 addition & 1 deletion packages/rum/src/domain/record/mutationBatch.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { collectAsyncCalls } from '../../../test/utils'
import { createMutationBatch } from './mutationBatch'
import type { RumMutationRecord } from './types'
import type { RumMutationRecord } from './mutationObserver'

describe('createMutationBatch', () => {
let mutationBatch: ReturnType<typeof createMutationBatch>
Expand Down
2 changes: 1 addition & 1 deletion packages/rum/src/domain/record/mutationBatch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { noop, requestIdleCallback } from '@datadog/browser-core'
import type { RumMutationRecord } from './types'
import type { RumMutationRecord } from './mutationObserver'

/**
* Maximum duration to wait before processing mutations. If the browser is idle, mutations will be
Expand Down
2 changes: 1 addition & 1 deletion packages/rum/src/domain/record/mutationObserver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { AttributeMutation, Attributes } from '../../types'
import { NodeType } from '../../types'
import { serializeDocument } from './serialize'
import { sortAddedAndMovedNodes, startMutationObserver, MutationController } from './mutationObserver'
import type { MutationCallBack } from './types'
import type { MutationCallBack } from './observer'

describe('startMutationCollection', () => {
let sandbox: HTMLElement
Expand Down
34 changes: 27 additions & 7 deletions packages/rum/src/domain/record/mutationObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,38 @@ import {
nodeAndAncestorsHaveSerializedNode,
} from './serializationUtils'
import { serializeNodeWithId, serializeAttribute } from './serialize'
import type {
RumAttributesMutationRecord,
RumCharacterDataMutationRecord,
RumChildListMutationRecord,
MutationCallBack,
RumMutationRecord,
} from './types'
import { forEach } from './utils'
import { createMutationBatch } from './mutationBatch'
import type { MutationCallBack } from './observer'

type WithSerializedTarget<T> = T & { target: NodeWithSerializedNode }

// https://dom.spec.whatwg.org/#interface-mutationrecord
interface RumCharacterDataMutationRecord {
type: 'characterData'
target: Node
oldValue: string | null
}

interface RumAttributesMutationRecord {
type: 'attributes'
target: Element
oldValue: string | null
attributeName: string | null
}

interface RumChildListMutationRecord {
type: 'childList'
target: Node
addedNodes: NodeList
removedNodes: NodeList
}

export type RumMutationRecord =
| RumCharacterDataMutationRecord
| RumAttributesMutationRecord
| RumChildListMutationRecord

/**
* Buffers and aggregate mutations generated by a MutationObserver into MutationPayload
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/rum/src/domain/record/observer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DefaultPrivacyLevel, isIE } from '@datadog/browser-core'
import { createNewEvent } from '../../../../core/test/specHelper'
import { NodePrivacyLevel, PRIVACY_ATTR_NAME, PRIVACY_ATTR_VALUE_MASK_USER_INPUT } from '../../constants'
import type { InputCallback } from './observer';
import { initInputObserver } from './observer'
import { serializeDocument } from './serialize'
import type { InputCallback } from './types'

describe('initInputObserver', () => {
let stopInputObserver: () => void
Expand Down
67 changes: 52 additions & 15 deletions packages/rum/src/domain/record/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,21 @@ import {
noop,
} from '@datadog/browser-core'
import { NodePrivacyLevel } from '../../constants'
import type { InputState, MousePosition, MouseInteraction } from '../../types'
import type {
InputState,
MousePosition,
MouseInteraction,
MutationPayload,
ScrollPosition,
StyleSheetRule,
ViewportResizeDimension,
MediaInteraction,
FocusRecord,
VisualViewportRecord,
} from '../../types'
import { IncrementalSource, MediaInteractionType, MouseInteractionType } from '../../types'
import { getNodePrivacyLevel, shouldMaskNode } from './privacy'
import { getElementInputValue, getSerializedNodeId, hasSerializedNode } from './serializationUtils'
import type {
FocusCallback,
InputCallback,
ListenerHandler,
MediaInteractionCallback,
MouseInteractionCallBack,
MousemoveCallBack,
MutationCallBack,
ObserverParam,
ScrollCallback,
StyleSheetRuleCallback,
ViewportResizeCallback,
VisualViewportResizeCallback,
} from './types'
import { forEach, isTouchEvent } from './utils'
import type { MutationController } from './mutationObserver'
import { startMutationObserver } from './mutationObserver'
Expand All @@ -46,6 +43,46 @@ const MOUSE_MOVE_OBSERVER_THRESHOLD = 50
const SCROLL_OBSERVER_THRESHOLD = 100
const VISUAL_VIEWPORT_OBSERVER_THRESHOLD = 200

type ListenerHandler = () => void

type MousemoveCallBack = (
p: MousePosition[],
source: typeof IncrementalSource.MouseMove | typeof IncrementalSource.TouchMove
) => void

export type MutationCallBack = (m: MutationPayload) => void

type MouseInteractionCallBack = (d: MouseInteraction) => void

type ScrollCallback = (p: ScrollPosition) => void

type StyleSheetRuleCallback = (s: StyleSheetRule) => void

type ViewportResizeCallback = (d: ViewportResizeDimension) => void

export type InputCallback = (v: InputState & { id: number }) => void

type MediaInteractionCallback = (p: MediaInteraction) => void

type FocusCallback = (data: FocusRecord['data']) => void

type VisualViewportResizeCallback = (data: VisualViewportRecord['data']) => void

interface ObserverParam {
defaultPrivacyLevel: DefaultPrivacyLevel
mutationController: MutationController
mutationCb: MutationCallBack
mousemoveCb: MousemoveCallBack
mouseInteractionCb: MouseInteractionCallBack
scrollCb: ScrollCallback
viewportResizeCb: ViewportResizeCallback
visualViewportResizeCb: VisualViewportResizeCallback
inputCb: InputCallback
mediaInteractionCb: MediaInteractionCallback
styleSheetRuleCb: StyleSheetRuleCallback
focusCb: FocusCallback
}

export function initObservers(o: ObserverParam): ListenerHandler {
const mutationHandler = initMutationObserver(o.mutationController, o.mutationCb, o.defaultPrivacyLevel)
const mousemoveHandler = initMoveObserver(o.mousemoveCb)
Expand Down
2 changes: 1 addition & 1 deletion packages/rum/src/domain/record/record.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { createNewEvent } from '../../../../core/test/specHelper'
import { collectAsyncCalls, recordsPerFullSnapshot } from '../../../test/utils'
import type { IncrementalSnapshotRecord, FocusRecord, Record } from '../../types'
import { RecordType, IncrementalSource } from '../../types'
import type { RecordAPI } from './record';
import { record } from './record'
import type { RecordAPI } from './types'

describe('record', () => {
let sandbox: HTMLElement
Expand Down
14 changes: 13 additions & 1 deletion packages/rum/src/domain/record/record.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assign, timeStampNow } from '@datadog/browser-core'
import type { DefaultPrivacyLevel, TimeStamp } from '@datadog/browser-core'
import type {
IncrementalSnapshotRecord,
IncrementalData,
Expand All @@ -10,15 +11,26 @@ import type {
ScrollData,
StyleSheetRuleData,
ViewportResizeData,
Record,
} from '../../types'
import { RecordType, IncrementalSource } from '../../types'
import { serializeDocument } from './serialize'
import { initObservers } from './observer'
import type { RecordAPI, RecordOptions } from './types'

import { MutationController } from './mutationObserver'
import { getVisualViewport, getScrollX, getScrollY, getWindowHeight, getWindowWidth } from './viewports'

export interface RecordOptions {
emit?: (record: Record) => void
defaultPrivacyLevel: DefaultPrivacyLevel
}

export interface RecordAPI {
stop: () => void
takeFullSnapshot: (timestamp?: TimeStamp) => void
flushMutations: () => void
}

export function record(options: RecordOptions): RecordAPI {
const { emit } = options
// runtime checks for user options
Expand Down
103 changes: 0 additions & 103 deletions packages/rum/src/domain/record/types.ts
Original file line number Diff line number Diff line change
@@ -1,103 +0,0 @@
import type { DefaultPrivacyLevel, TimeStamp } from '@datadog/browser-core'
import type {
FocusRecord,
VisualViewportRecord,
Record,
MousePosition,
IncrementalSource,
MouseInteraction,
ScrollPosition,
StyleSheetRule,
ViewportResizeDimension,
MediaInteraction,
MutationPayload,
InputState,
} from '../../types'
import type { MutationController } from './mutationObserver'

export interface RecordOptions {
emit?: (record: Record) => void
defaultPrivacyLevel: DefaultPrivacyLevel
}

export interface RecordAPI {
stop: ListenerHandler
takeFullSnapshot: (timestamp?: TimeStamp) => void
flushMutations: () => void
}

export interface ObserverParam {
defaultPrivacyLevel: DefaultPrivacyLevel
mutationController: MutationController
mutationCb: MutationCallBack
mousemoveCb: MousemoveCallBack
mouseInteractionCb: MouseInteractionCallBack
scrollCb: ScrollCallback
viewportResizeCb: ViewportResizeCallback
visualViewportResizeCb: VisualViewportResizeCallback
inputCb: InputCallback
mediaInteractionCb: MediaInteractionCallback
styleSheetRuleCb: StyleSheetRuleCallback
focusCb: FocusCallback
}

// https://dom.spec.whatwg.org/#interface-mutationrecord
export interface RumCharacterDataMutationRecord {
type: 'characterData'
target: Node
oldValue: string | null
}

export interface RumAttributesMutationRecord {
type: 'attributes'
target: Element
oldValue: string | null
attributeName: string | null
}

export interface RumChildListMutationRecord {
type: 'childList'
target: Node
addedNodes: NodeList
removedNodes: NodeList
}

export type RumMutationRecord =
| RumCharacterDataMutationRecord
| RumAttributesMutationRecord
| RumChildListMutationRecord

export interface TextCursor {
node: Node
value: string | null
}
export interface AttributeCursor {
node: Node
attributes: {
[key: string]: string | null
}
}
export type MutationCallBack = (m: MutationPayload) => void

export type MousemoveCallBack = (
p: MousePosition[],
source: typeof IncrementalSource.MouseMove | typeof IncrementalSource.TouchMove
) => void

export type MouseInteractionCallBack = (d: MouseInteraction) => void

export type ScrollCallback = (p: ScrollPosition) => void

export type StyleSheetRuleCallback = (s: StyleSheetRule) => void

export type ViewportResizeCallback = (d: ViewportResizeDimension) => void

export type InputCallback = (v: InputState & { id: number }) => void

export type MediaInteractionCallback = (p: MediaInteraction) => void

export type FocusCallback = (data: FocusRecord['data']) => void

export type VisualViewportResizeCallback = (data: VisualViewportRecord['data']) => void

export type ListenerHandler = () => void

0 comments on commit bf2085f

Please sign in to comment.