-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Argument idNodeMap
direct mutation
#32
Comments
@igaloly Thanks for the feedback. Since |
Maybe something like: export function serializeNodeWithId(
n: Node | INode,
doc: Document,
map: idNodeMap,
blockClass: string | RegExp,
skipChild = false,
inlineStylesheet = true,
maskAllInputs = false,
): serializedNodeWithId | null {
const _serializedNode = serializeNode(
n,
doc,
blockClass,
inlineStylesheet,
maskAllInputs,
);
if (!_serializedNode) {
// TODO: dev only
console.warn(n, 'not serialized');
return null;
}
let id;
// 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;
map[id] = n as INode;
let recordChild = !skipChild;
if (serializedNode.type === NodeType.Element) {
recordChild = recordChild && !serializedNode.needBlock;
// this property was not needed in replay side
delete serializedNode.needBlock;
}
if (
(serializedNode.type === NodeType.Document ||
serializedNode.type === NodeType.Element) &&
recordChild
) {
for (const childN of Array.from(n.childNodes)) {
const serializedChildNode = serializeNodeWithId(
childN,
doc,
map,
blockClass,
skipChild,
inlineStylesheet,
maskAllInputs,
);
if (serializedChildNode) {
serializedNode.childNodes.push(serializedChildNode);
}
}
}
return { serializedNode, idNodeMap: map };
}
function snapshot(
n: Document,
blockClass: string | RegExp = 'rr-block',
inlineStylesheet = true,
maskAllInputs = false,
): [serializedNodeWithId | null, idNodeMap] {
const { serializedNode, idNodeMap } = serializeNodeWithId( n, n, {}, blockClass, false, inlineStylesheet, maskAllInputs )
return [
serializedNode,
idNodeMap,
];
}
Even though it seems like now, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In
/src/snapshot.ts
,snapshot
function declaring a new empty map,idNodeMap
, and it then returning it.At first glance, it seems like
idNodeMap
is useless, because there a declare and immediate return.Only what you go inside
serializeNodeWithId
, see thatidNodeMap
is passed to and is mutated inside of it, you can understand thatidNodeMap
is not empty when returning it.I think it's a bit confusing and generally, as I know, not the best practice.
I suggest creating an helper function that will handle creating
idNodeMap
and returning it.The text was updated successfully, but these errors were encountered: