Skip to content

Commit

Permalink
Fix: processed-node-manager is created even in the environment that d…
Browse files Browse the repository at this point in the history
…oesn't need a recorder (#1186)

* Fix: processed-node-manager is created even in the environment that doesn't need a recorder

* apply Justin's suggestion

End the RAF loop when the recorder stops
  • Loading branch information
YunFeng0817 authored Mar 24, 2023
1 parent a225d8e commit 267e990
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-spoons-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'rrweb': patch
---

Fix: processed-node-manager is created even in the environment that doesn't need a recorder
1 change: 1 addition & 0 deletions guide.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ setInterval(save, 10 * 1000);
| collectFonts | false | 是否记录页面中的字体文件 |
| userTriggeredOnInput | false | [什么是 `userTriggered`](https://github.com/rrweb-io/rrweb/pull/495) |
| plugins | [] | 加载插件以获得额外的录制功能. [什么是插件?](./docs/recipes/plugin.zh_CN.md) |
| errorHandler | - | 一个可以定制化处理错误的毁掉函数,它的参数是错误对象。如果 rrweb recorder 内部的某些内容抛出错误,则会调用该回调。 |

#### 隐私

Expand Down
10 changes: 5 additions & 5 deletions packages/rrweb/src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import {
SlimDOMOptions,
createMirror,
} from 'rrweb-snapshot';
import {
initObservers,
mutationBuffers,
processedNodeManager,
} from './observer';
import { initObservers, mutationBuffers } from './observer';
import {
on,
getWindowWidth,
Expand Down Expand Up @@ -36,6 +32,7 @@ import { IframeManager } from './iframe-manager';
import { ShadowDomManager } from './shadow-dom-manager';
import { CanvasManager } from './observers/canvas/canvas-manager';
import { StylesheetManager } from './stylesheet-manager';
import ProcessedNodeManager from './processed-node-manager';
import {
callbackWrapper,
registerErrorHandler,
Expand Down Expand Up @@ -306,6 +303,8 @@ function record<T = eventWithTime>(
});
}

const processedNodeManager = new ProcessedNodeManager();

canvasManager = new CanvasManager({
recordCanvas,
mutationCb: wrappedCanvasMutationEmit,
Expand Down Expand Up @@ -616,6 +615,7 @@ function record<T = eventWithTime>(
}
return () => {
handlers.forEach((h) => h());
processedNodeManager.destroy();
recording = false;
unregisterErrorHandler();
};
Expand Down
2 changes: 0 additions & 2 deletions packages/rrweb/src/record/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import {
selectionCallback,
} from '@rrweb/types';
import MutationBuffer from './mutation';
import ProcessedNodeManager from './processed-node-manager';
import { callbackWrapper } from './error-handler';

type WindowWithStoredMutationObserver = IWindow & {
Expand All @@ -54,7 +53,6 @@ type WindowWithAngularZone = IWindow & {
};

export const mutationBuffers: MutationBuffer[] = [];
export const processedNodeManager = new ProcessedNodeManager();

// Event.path is non-standard and used in some older browsers
type NonStandardEvent = Omit<Event, 'composedPath'> & {
Expand Down
9 changes: 8 additions & 1 deletion packages/rrweb/src/record/processed-node-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type MutationBuffer from './mutation';
*/
export default class ProcessedNodeManager {
private nodeMap: WeakMap<Node, Set<MutationBuffer>> = new WeakMap();
// Whether to continue RAF loop.
private loop = true;

constructor() {
this.periodicallyClear();
Expand All @@ -13,7 +15,7 @@ export default class ProcessedNodeManager {
private periodicallyClear() {
requestAnimationFrame(() => {
this.clear();
this.periodicallyClear();
if (this.loop) this.periodicallyClear();
});
}

Expand All @@ -31,4 +33,9 @@ export default class ProcessedNodeManager {
private clear() {
this.nodeMap = new WeakMap();
}

public destroy() {
// Stop the RAF loop.
this.loop = false;
}
}
3 changes: 2 additions & 1 deletion packages/rrweb/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ export function getInputType(element: HTMLElement): Lowercase<string> | null {
return element.hasAttribute('data-rr-is-password')
? 'password'
: element.hasAttribute('type')
? (element.getAttribute('type')!.toLowerCase() as Lowercase<string>)
? // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion, @typescript-eslint/no-non-null-assertion
(element.getAttribute('type')!.toLowerCase() as Lowercase<string>)
: null;
}

0 comments on commit 267e990

Please sign in to comment.