Skip to content

Commit c35d276

Browse files
committed
Revert "feat: Remove plugins related code, which is not used (#123)"
This reverts commit 42ffd92.
1 parent 9080bdd commit c35d276

File tree

4 files changed

+366
-12
lines changed

4 files changed

+366
-12
lines changed

packages/rrweb/src/record/index.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ function record<T = eventWithTime>(
109109
userTriggeredOnInput = false,
110110
collectFonts = false,
111111
inlineImages = false,
112+
plugins,
112113
keepIframeSrcFn = () => false,
113114
ignoreCSSAttributes = new Set([]),
114115
errorHandler,
@@ -197,7 +198,11 @@ function record<T = eventWithTime>(
197198
let incrementalSnapshotCount = 0;
198199

199200
const eventProcessor = (e: eventWithTime): T => {
200-
// We ignore plugins here, as we do not have any
201+
for (const plugin of plugins || []) {
202+
if (plugin.eventProcessor) {
203+
e = plugin.eventProcessor(e);
204+
}
205+
}
201206
if (
202207
packFn &&
203208
// Disable packing events which will be emitted to parent frames.
@@ -319,8 +324,16 @@ function record<T = eventWithTime>(
319324

320325
/**
321326
* Exposes mirror to the plugins
322-
* We ignore plugins here, as we don't use any
323327
*/
328+
for (const plugin of plugins || []) {
329+
if (plugin.getMirror)
330+
plugin.getMirror({
331+
nodeMirror: mirror,
332+
crossOriginIframeMirror: iframeManager.crossOriginIframeMirror,
333+
crossOriginIframeStyleMirror:
334+
iframeManager.crossOriginIframeStyleMirror,
335+
});
336+
}
324337

325338
const processedNodeManager = new ProcessedNodeManager();
326339

@@ -598,7 +611,23 @@ function record<T = eventWithTime>(
598611
processedNodeManager,
599612
canvasManager,
600613
ignoreCSSAttributes,
601-
plugins: [],
614+
plugins:
615+
plugins
616+
?.filter((p) => p.observer)
617+
?.map((p) => ({
618+
observer: p.observer!,
619+
options: p.options,
620+
callback: (payload: object) =>
621+
wrappedEmit(
622+
wrapEvent({
623+
type: EventType.Plugin,
624+
data: {
625+
plugin: p.name,
626+
payload,
627+
},
628+
}),
629+
),
630+
})) || [],
602631
},
603632
{},
604633
);

packages/rrweb/src/record/observer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,12 @@ export function initObservers(
13151315
const customElementObserver = initCustomElementObserver(o);
13161316

13171317
// plugins
1318-
// we ignore plugins here, as we don't have any
1318+
const pluginHandlers: listenerHandler[] = [];
1319+
for (const plugin of o.plugins) {
1320+
pluginHandlers.push(
1321+
plugin.observer(plugin.callback, currentWindow, plugin.options),
1322+
);
1323+
}
13191324

13201325
return callbackWrapper(() => {
13211326
mutationBuffers.forEach((b) => b.reset());
@@ -1332,6 +1337,7 @@ export function initObservers(
13321337
fontObserver();
13331338
selectionObserver();
13341339
customElementObserver();
1340+
pluginHandlers.forEach((h) => h());
13351341
});
13361342
}
13371343

packages/rrweb/src/replay/index.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,10 @@ export class Replayer {
202202

203203
/**
204204
* Exposes mirror to the plugins
205-
* We ignore plugins here, as we don't have any
206205
*/
206+
for (const plugin of this.config.plugins || []) {
207+
if (plugin.getMirror) plugin.getMirror({ nodeMirror: this.mirror });
208+
}
207209

208210
this.emitter.on(ReplayerEvents.Flush, () => {
209211
if (this.usingVirtualDom) {
@@ -234,7 +236,11 @@ export class Replayer {
234236
else if (data.source === IncrementalSource.StyleDeclaration)
235237
this.applyStyleDeclaration(data, styleSheet);
236238
},
237-
// we ignore plugins here, as we don't have any
239+
afterAppend: (node: Node, id: number) => {
240+
for (const plugin of this.config.plugins || []) {
241+
if (plugin.onBuild) plugin.onBuild(node, { id, replayer: this });
242+
}
243+
},
238244
};
239245
if (this.iframe.contentDocument)
240246
try {
@@ -717,7 +723,9 @@ export class Replayer {
717723
castFn();
718724
}
719725

720-
// we ignore plugins here, as we don't have any
726+
for (const plugin of this.config.plugins || []) {
727+
if (plugin.handler) plugin.handler(event, isSync, { replayer: this });
728+
}
721729

722730
this.service.send({ type: 'CAST_EVENT', payload: { event } });
723731

@@ -770,7 +778,13 @@ export class Replayer {
770778
const collected: AppendedIframe[] = [];
771779
const afterAppend = (builtNode: Node, id: number) => {
772780
this.collectIframeAndAttachDocument(collected, builtNode);
773-
// we ignore plugins here, as we don't have any
781+
for (const plugin of this.config.plugins || []) {
782+
if (plugin.onBuild)
783+
plugin.onBuild(builtNode, {
784+
id,
785+
replayer: this,
786+
});
787+
}
774788
};
775789

776790
/**
@@ -863,7 +877,7 @@ export class Replayer {
863877
type TMirror = typeof mirror extends Mirror ? Mirror : RRDOMMirror;
864878

865879
const collected: AppendedIframe[] = [];
866-
const afterAppend = (builtNode: Node, _id: number) => {
880+
const afterAppend = (builtNode: Node, id: number) => {
867881
this.collectIframeAndAttachDocument(collected, builtNode);
868882
const sn = (mirror as TMirror).getMeta(builtNode as unknown as TNode);
869883
if (
@@ -878,7 +892,14 @@ export class Replayer {
878892
}
879893

880894
// Skip the plugin onBuild callback in the virtual dom mode
881-
// we ignore plugins here, as we don't have any
895+
if (this.usingVirtualDom) return;
896+
for (const plugin of this.config.plugins || []) {
897+
if (plugin.onBuild)
898+
plugin.onBuild(builtNode, {
899+
id,
900+
replayer: this,
901+
});
902+
}
882903
};
883904

884905
buildNodeWithSN(mutation.node, {
@@ -1498,7 +1519,13 @@ export class Replayer {
14981519
);
14991520
return;
15001521
}
1501-
// we ignore plugins here, as we don't have any
1522+
const afterAppend = (node: Node | RRNode, id: number) => {
1523+
// Skip the plugin onBuild callback for virtual dom
1524+
if (this.usingVirtualDom) return;
1525+
for (const plugin of this.config.plugins || []) {
1526+
if (plugin.onBuild) plugin.onBuild(node, { id, replayer: this });
1527+
}
1528+
};
15021529

15031530
const target = buildNodeWithSN(mutation.node, {
15041531
doc: targetDoc as Document, // can be Document or RRDocument
@@ -1510,6 +1537,7 @@ export class Replayer {
15101537
* caveat: `afterAppend` only gets called on child nodes of target
15111538
* we have to call it again below when this target was added to the DOM
15121539
*/
1540+
afterAppend,
15131541
}) as Node | RRNode;
15141542

15151543
// legacy data, we should not have -1 siblings any more
@@ -1584,7 +1612,10 @@ export class Replayer {
15841612
} else {
15851613
(parent as TNode).appendChild(target as TNode);
15861614
}
1587-
// we ignore plugins here, as we don't have any
1615+
/**
1616+
* target was added, execute plugin hooks
1617+
*/
1618+
afterAppend(target, mutation.node.id);
15881619

15891620
/**
15901621
* https://github.com/rrweb-io/rrweb/pull/887

0 commit comments

Comments
 (0)