reuse serialized nodes ids (#12)
* reuse serialized nodes ids With this patch, each DOM node keeps its id during its existence. This allows to apply RRWeb events to previous snapshots if needed. `resetId` has been removed because it loses its meaning: calling it would not reset the existing nodes ids anymore, only the new ones. Since we don't reset the id anymore, we may exhaust the available ids quicker, but Number.MAX_SAFE_INTEGER (2 ** 53 - 1) is pretty large, so I doubt this'll cause any problem. * improv TS typing The `nAsINode` variable was not very elegant. Simplify this by removing the cast, and make the INode interface compatible with Node. * update typings
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import snapshot, { serializeNodeWithId, resetId } from './snapshot';
|
import snapshot, { serializeNodeWithId } from './snapshot';
|
||||||
import rebuild, { buildNodeWithSN } from './rebuild';
|
import rebuild, { buildNodeWithSN } from './rebuild';
|
||||||
export * from './types';
|
export * from './types';
|
||||||
|
|
||||||
export { snapshot, serializeNodeWithId, resetId, rebuild, buildNodeWithSN };
|
export { snapshot, serializeNodeWithId, rebuild, buildNodeWithSN };
|
||||||
|
|||||||
@@ -13,10 +13,6 @@ function genId(): number {
|
|||||||
return _id++;
|
return _id++;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function resetId() {
|
|
||||||
_id = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getCssRulesString(s: CSSStyleSheet): string | null {
|
function getCssRulesString(s: CSSStyleSheet): string | null {
|
||||||
try {
|
try {
|
||||||
const rules = s.rules || s.cssRules;
|
const rules = s.rules || s.cssRules;
|
||||||
@@ -239,7 +235,7 @@ function serializeNode(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function serializeNodeWithId(
|
export function serializeNodeWithId(
|
||||||
n: Node,
|
n: Node | INode,
|
||||||
doc: Document,
|
doc: Document,
|
||||||
map: idNodeMap,
|
map: idNodeMap,
|
||||||
blockClass: string | RegExp,
|
blockClass: string | RegExp,
|
||||||
@@ -252,11 +248,16 @@ export function serializeNodeWithId(
|
|||||||
console.warn(n, 'not serialized');
|
console.warn(n, 'not serialized');
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const serializedNode = Object.assign(_serializedNode, {
|
let id
|
||||||
id: genId(),
|
// Try to reuse the previous id
|
||||||
});
|
if ('__sn' in n) {
|
||||||
|
id = n.__sn.id
|
||||||
|
} else {
|
||||||
|
id = genId()
|
||||||
|
}
|
||||||
|
const serializedNode = Object.assign(_serializedNode, { id });
|
||||||
(n as INode).__sn = serializedNode;
|
(n as INode).__sn = serializedNode;
|
||||||
map[serializedNode.id] = n as INode;
|
map[id] = (n as INode);
|
||||||
let recordChild = !skipChild;
|
let recordChild = !skipChild;
|
||||||
if (serializedNode.type === NodeType.Element) {
|
if (serializedNode.type === NodeType.Element) {
|
||||||
recordChild = recordChild && !serializedNode.needBlock;
|
recordChild = recordChild && !serializedNode.needBlock;
|
||||||
@@ -290,7 +291,6 @@ function snapshot(
|
|||||||
blockClass: string | RegExp = 'rr-block',
|
blockClass: string | RegExp = 'rr-block',
|
||||||
inlineStylesheet = true,
|
inlineStylesheet = true,
|
||||||
): [serializedNodeWithId | null, idNodeMap] {
|
): [serializedNodeWithId | null, idNodeMap] {
|
||||||
resetId();
|
|
||||||
const idNodeMap: idNodeMap = {};
|
const idNodeMap: idNodeMap = {};
|
||||||
return [
|
return [
|
||||||
serializeNodeWithId(n, n, idNodeMap, blockClass, false, inlineStylesheet),
|
serializeNodeWithId(n, n, idNodeMap, blockClass, false, inlineStylesheet),
|
||||||
|
|||||||
4
typings/index.d.ts
vendored
4
typings/index.d.ts
vendored
@@ -1,4 +1,4 @@
|
|||||||
import snapshot, { serializeNodeWithId, resetId } from './snapshot';
|
import snapshot, { serializeNodeWithId } from './snapshot';
|
||||||
import rebuild, { buildNodeWithSN } from './rebuild';
|
import rebuild, { buildNodeWithSN } from './rebuild';
|
||||||
export * from './types';
|
export * from './types';
|
||||||
export { snapshot, serializeNodeWithId, resetId, rebuild, buildNodeWithSN };
|
export { snapshot, serializeNodeWithId, rebuild, buildNodeWithSN };
|
||||||
|
|||||||
5
typings/snapshot.d.ts
vendored
5
typings/snapshot.d.ts
vendored
@@ -1,6 +1,5 @@
|
|||||||
import { serializedNodeWithId, idNodeMap } from './types';
|
import { serializedNodeWithId, INode, idNodeMap } from './types';
|
||||||
export declare function resetId(): void;
|
|
||||||
export declare function absoluteToStylesheet(cssText: string, href: string): string;
|
export declare function absoluteToStylesheet(cssText: string, href: string): string;
|
||||||
export declare function serializeNodeWithId(n: Node, doc: Document, map: idNodeMap, blockClass: string | RegExp, skipChild?: boolean, inlineStylesheet?: boolean): serializedNodeWithId | null;
|
export declare function serializeNodeWithId(n: Node | INode, doc: Document, map: idNodeMap, blockClass: string | RegExp, skipChild?: boolean, inlineStylesheet?: boolean): serializedNodeWithId | null;
|
||||||
declare function snapshot(n: Document, blockClass?: string | RegExp, inlineStylesheet?: boolean): [serializedNodeWithId | null, idNodeMap];
|
declare function snapshot(n: Document, blockClass?: string | RegExp, inlineStylesheet?: boolean): [serializedNodeWithId | null, idNodeMap];
|
||||||
export default snapshot;
|
export default snapshot;
|
||||||
|
|||||||
Reference in New Issue
Block a user