From 14bdd6745959cbd1d033c60299883c3526537560 Mon Sep 17 00:00:00 2001 From: Eoghan Murray Date: Sat, 24 Oct 2020 09:10:51 +0100 Subject: [PATCH] Add ability to reset the mapping id, to facilitate destructor in rrweb (rrwebRecord.stop) (#49) --- src/index.ts | 2 ++ src/snapshot.ts | 5 +++++ typings/index.d.ts | 4 ++-- typings/snapshot.d.ts | 1 + 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 77ca8377..20d5b02d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ import snapshot, { serializeNodeWithId, transformAttribute, visitSnapshot, + cleanupSnapshot, IGNORED_NODE, } from './snapshot'; import rebuild, { buildNodeWithSN, addHoverClass } from './rebuild'; @@ -15,5 +16,6 @@ export { addHoverClass, transformAttribute, visitSnapshot, + cleanupSnapshot, IGNORED_NODE, }; diff --git a/src/snapshot.ts b/src/snapshot.ts index 0e97033b..c3190794 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -570,4 +570,9 @@ export function visitSnapshot( walk(node); } +export function cleanupSnapshot() { + // allow a new recording to start numbering nodes from scratch + _id = 1; +} + export default snapshot; diff --git a/typings/index.d.ts b/typings/index.d.ts index d436c211..b2c2271d 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1,4 +1,4 @@ -import snapshot, { serializeNodeWithId, transformAttribute, visitSnapshot, IGNORED_NODE } from './snapshot'; +import snapshot, { serializeNodeWithId, transformAttribute, visitSnapshot, IGNORED_NODE, cleanupSnapshot } from './snapshot'; import rebuild, { buildNodeWithSN, addHoverClass } from './rebuild'; export * from './types'; -export { snapshot, serializeNodeWithId, rebuild, buildNodeWithSN, addHoverClass, transformAttribute, visitSnapshot, IGNORED_NODE, }; +export { snapshot, serializeNodeWithId, rebuild, buildNodeWithSN, addHoverClass, transformAttribute, visitSnapshot, IGNORED_NODE, cleanupSnapshot, }; diff --git a/typings/snapshot.d.ts b/typings/snapshot.d.ts index 163ee0c7..988f7982 100644 --- a/typings/snapshot.d.ts +++ b/typings/snapshot.d.ts @@ -6,4 +6,5 @@ export declare function transformAttribute(doc: Document, name: string, value: s export declare function serializeNodeWithId(n: Node | INode, doc: Document, map: idNodeMap, blockClass: string | RegExp, skipChild?: boolean, inlineStylesheet?: boolean, maskInputOptions?: MaskInputOptions, slimDOMOptions?: SlimDOMOptions, recordCanvas?: boolean, preserveWhiteSpace?: boolean): serializedNodeWithId | null; declare function snapshot(n: Document, blockClass: string | RegExp | undefined, inlineStylesheet: boolean | undefined, maskAllInputsOrOptions: boolean | MaskInputOptions, slimDOMSensibleOrOptions: boolean | SlimDOMOptions, recordCanvas?: boolean): [serializedNodeWithId | null, idNodeMap]; export declare function visitSnapshot(node: serializedNodeWithId, onVisit: (node: serializedNodeWithId) => unknown): void; +export declare function cleanupSnapshot(): void; export default snapshot;