Skip to content

Commit

Permalink
ref: Avoid unnecessary cloning of objects or arrays (#1340)
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea authored Nov 3, 2023
1 parent f362e7a commit 9c6edfe
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-apples-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'rrweb': patch
---

ref: Avoid unnecessary cloning of objects or arrays
28 changes: 7 additions & 21 deletions packages/rrweb/src/record/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,6 @@ function initViewportResizeObserver(
return on('resize', updateDimension, win);
}

function wrapEventWithUserTriggeredFlag(
v: inputValue,
enable: boolean,
): inputValue {
const value = { ...v };
if (!enable) delete value.userTriggered;
return value;
}

export const INPUT_TAGS = ['INPUT', 'TEXTAREA', 'SELECT'];
const lastInputValueMap: WeakMap<EventTarget, inputValue> = new WeakMap();
function initInputObserver({
Expand Down Expand Up @@ -477,10 +468,9 @@ function initInputObserver({
}
cbWithDedup(
target,
callbackWrapper(wrapEventWithUserTriggeredFlag)(
{ text, isChecked, userTriggered },
userTriggeredOnInput,
),
userTriggeredOnInput
? { text, isChecked, userTriggered }
: { text, isChecked },
);
// if a radio was checked
// the other radios with the same name attribute will be unchecked.
Expand All @@ -490,16 +480,12 @@ function initInputObserver({
.querySelectorAll(`input[type="radio"][name="${name}"]`)
.forEach((el) => {
if (el !== target) {
const text = (el as HTMLInputElement).value;
cbWithDedup(
el,
callbackWrapper(wrapEventWithUserTriggeredFlag)(
{
text: (el as HTMLInputElement).value,
isChecked: !isChecked,
userTriggered: false,
},
userTriggeredOnInput,
),
userTriggeredOnInput
? { text, isChecked: !isChecked, userTriggered: false }
: { text, isChecked: !isChecked },
);
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/rrweb/src/record/observers/canvas/2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function initCanvas2DMutationObserver(
// Using setTimeout as toDataURL can be heavy
// and we'd rather not block the main thread
setTimeout(() => {
const recordArgs = serializeArgs([...args], win, this);
const recordArgs = serializeArgs(args, win, this);
cb(this.canvas, {
type: CanvasContext['2D'],
property: prop,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export const serializeArgs = (
win: IWindow,
ctx: RenderingContext,
) => {
return [...args].map((arg) => serializeArg(arg, win, ctx));
return args.map((arg) => serializeArg(arg, win, ctx));
};

export const isInstanceOfWebGLObject = (
Expand Down
2 changes: 1 addition & 1 deletion packages/rrweb/src/record/observers/canvas/webgl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function patchGLPrototype(
'tagName' in this.canvas &&
!isBlocked(this.canvas, blockClass, blockSelector, true)
) {
const recordArgs = serializeArgs([...args], win, this);
const recordArgs = serializeArgs(args, win, this);
const mutation: canvasMutationWithType = {
type,
property: prop,
Expand Down

0 comments on commit 9c6edfe

Please sign in to comment.