Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Move ids to weakmap
  • Loading branch information
Juice10 committed Mar 31, 2022
commit 50785be503a20799c3030e08a8cbf59e91b5d830
33 changes: 23 additions & 10 deletions packages/rrweb-snapshot/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
attributes,
INode,
idNodeMap,
nodeIdMap,
MaskInputOptions,
SlimDOMOptions,
DataURLOptions,
Expand Down Expand Up @@ -377,6 +378,7 @@ function serializeNode(
n: Node,
options: {
doc: Document;
nodeIdMap: nodeIdMap;
blockClass: string | RegExp;
blockSelector: string | null;
maskTextClass: string | RegExp;
Expand All @@ -393,6 +395,7 @@ function serializeNode(
): serializedNode | false {
const {
doc,
nodeIdMap,
blockClass,
blockSelector,
maskTextClass,
Expand All @@ -408,8 +411,8 @@ function serializeNode(
} = options;
// Only record root id when document object is not the base document
let rootId: number | undefined;
if (((doc as unknown) as INode).__sn) {
const docId = ((doc as unknown) as INode).__sn.id;
const docId = nodeIdMap.get(n);
if (docId) {
rootId = docId === 1 ? undefined : docId;
}
switch (n.nodeType) {
Expand Down Expand Up @@ -784,7 +787,8 @@ export function serializeNodeWithId(
n: Node | INode,
options: {
doc: Document;
map: idNodeMap;
map: idNodeMap; // DEPRECATED
nodeIdMap: nodeIdMap;
blockClass: string | RegExp;
blockSelector: string | null;
maskTextClass: string | RegExp;
Expand All @@ -808,6 +812,7 @@ export function serializeNodeWithId(
const {
doc,
map,
nodeIdMap,
blockClass,
blockSelector,
maskTextClass,
Expand All @@ -829,6 +834,7 @@ export function serializeNodeWithId(
let { preserveWhiteSpace = true } = options;
const _serializedNode = serializeNode(n, {
doc,
nodeIdMap,
blockClass,
blockSelector,
maskTextClass,
Expand All @@ -848,10 +854,9 @@ export function serializeNodeWithId(
return null;
}

let id;
// Try to reuse the previous id
if ('__sn' in n) {
id = n.__sn.id;
let id: number | undefined = nodeIdMap.get(n);
if (id) {
// Reuse the previous id
} else if (
slimDOMExcluded(_serializedNode, slimDOMOptions) ||
(!preserveWhiteSpace &&
Expand All @@ -868,7 +873,11 @@ export function serializeNodeWithId(
if (id === IGNORED_NODE) {
return null; // slimDOM
}
nodeIdMap.set(n, id);

// TODO: map is deprecated, please clean me up
map[id] = n as INode;

if (onSerialize) {
onSerialize(n as INode);
}
Expand All @@ -895,6 +904,7 @@ export function serializeNodeWithId(
const bypassOptions = {
doc,
map,
nodeIdMap,
blockClass,
blockSelector,
maskTextClass,
Expand Down Expand Up @@ -948,6 +958,7 @@ export function serializeNodeWithId(
const serializedIframeNode = serializeNodeWithId(iframeDoc, {
doc: iframeDoc,
map,
nodeIdMap,
blockClass,
blockSelector,
maskTextClass,
Expand Down Expand Up @@ -1001,7 +1012,7 @@ function snapshot(
iframeLoadTimeout?: number;
keepIframeSrcFn?: KeepIframeSrcFn;
},
): [serializedNodeWithId | null, idNodeMap] {
): [serializedNodeWithId | null, nodeIdMap] {
const {
blockClass = 'rr-block',
blockSelector = null,
Expand All @@ -1022,6 +1033,7 @@ function snapshot(
keepIframeSrcFn = () => false,
} = options || {};
const idNodeMap: idNodeMap = {};
const nodeIdMap: nodeIdMap = new WeakMap();
const maskInputOptions: MaskInputOptions =
maskAllInputs === true
? {
Expand Down Expand Up @@ -1068,7 +1080,8 @@ function snapshot(
return [
serializeNodeWithId(n, {
doc: n,
map: idNodeMap,
map: idNodeMap, // DEPRECATED
nodeIdMap,
blockClass,
blockSelector,
maskTextClass,
Expand All @@ -1088,7 +1101,7 @@ function snapshot(
iframeLoadTimeout,
keepIframeSrcFn,
}),
idNodeMap,
nodeIdMap,
];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/rrweb-snapshot/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export type tagMap = {
};

export interface INode extends Node {
__sn: serializedNodeWithId;
__sn: serializedNodeWithId | serializedNode;
}

export interface ICanvas extends HTMLCanvasElement {
Expand All @@ -79,6 +79,8 @@ export type idNodeMap = {
[key: number]: INode;
};

export type nodeIdMap = WeakMap<Node, number>;

export type MaskInputOptions = Partial<{
color: boolean;
date: boolean;
Expand Down
5 changes: 3 additions & 2 deletions packages/rrweb-snapshot/typings/snapshot.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { serializedNodeWithId, INode, idNodeMap, MaskInputOptions, SlimDOMOptions, DataURLOptions, MaskTextFn, MaskInputFn, KeepIframeSrcFn } from './types';
import { serializedNodeWithId, INode, idNodeMap, nodeIdMap, MaskInputOptions, SlimDOMOptions, DataURLOptions, MaskTextFn, MaskInputFn, KeepIframeSrcFn } from './types';
export declare const IGNORED_NODE = -2;
export declare function absoluteToStylesheet(cssText: string | null, href: string): string;
export declare function absoluteToDoc(doc: Document, attributeValue: string): string;
Expand All @@ -8,6 +8,7 @@ export declare function needMaskingText(node: Node | null, maskTextClass: string
export declare function serializeNodeWithId(n: Node | INode, options: {
doc: Document;
map: idNodeMap;
nodeIdMap: nodeIdMap;
blockClass: string | RegExp;
blockSelector: string | null;
maskTextClass: string | RegExp;
Expand Down Expand Up @@ -45,7 +46,7 @@ declare function snapshot(n: Document, options?: {
onIframeLoad?: (iframeINode: INode, node: serializedNodeWithId) => unknown;
iframeLoadTimeout?: number;
keepIframeSrcFn?: KeepIframeSrcFn;
}): [serializedNodeWithId | null, idNodeMap];
}): [serializedNodeWithId | null, nodeIdMap];
export declare function visitSnapshot(node: serializedNodeWithId, onVisit: (node: serializedNodeWithId) => unknown): void;
export declare function cleanupSnapshot(): void;
export default snapshot;
3 changes: 2 additions & 1 deletion packages/rrweb-snapshot/typings/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ export declare type tagMap = {
[key: string]: string;
};
export interface INode extends Node {
__sn: serializedNodeWithId;
__sn: serializedNodeWithId | serializedNode;
}
export interface ICanvas extends HTMLCanvasElement {
__context: string;
}
export declare type idNodeMap = {
[key: number]: INode;
};
export declare type nodeIdMap = WeakMap<Node, number>;
export declare type MaskInputOptions = Partial<{
color: boolean;
date: boolean;
Expand Down