return stopper function as the result of record

This commit is contained in:
Yanzhen Yu
2026-04-01 12:00:00 +08:00
parent efa7a8fa1b
commit 09f30d2a81
3 changed files with 105 additions and 93 deletions

View File

@@ -7,6 +7,7 @@ import {
eventWithTime, eventWithTime,
recordOptions, recordOptions,
IncrementalSource, IncrementalSource,
listenerHandler,
} from '../types'; } from '../types';
function wrapEvent(e: event): eventWithTime { function wrapEvent(e: event): eventWithTime {
@@ -16,13 +17,15 @@ function wrapEvent(e: event): eventWithTime {
}; };
} }
function record(options: recordOptions = {}) { function record(options: recordOptions = {}): listenerHandler | undefined {
const { emit } = options; const { emit } = options;
// runtime checks for user options // runtime checks for user options
if (!emit) { if (!emit) {
throw new Error('emit function is required'); throw new Error('emit function is required');
} }
try { try {
const handlers: listenerHandler[] = [];
handlers.push(
on('DOMContentLoaded', () => { on('DOMContentLoaded', () => {
emit( emit(
wrapEvent({ wrapEvent({
@@ -30,7 +33,8 @@ function record(options: recordOptions = {}) {
data: {}, data: {},
}), }),
); );
}); }),
);
const init = () => { const init = () => {
emit( emit(
wrapEvent({ wrapEvent({
@@ -59,6 +63,7 @@ function record(options: recordOptions = {}) {
}, },
}), }),
); );
handlers.push(
initObservers({ initObservers({
mutationCb: m => mutationCb: m =>
emit( emit(
@@ -120,7 +125,8 @@ function record(options: recordOptions = {}) {
}, },
}), }),
), ),
}); }),
);
}; };
if ( if (
document.readyState === 'interactive' || document.readyState === 'interactive' ||
@@ -128,6 +134,7 @@ function record(options: recordOptions = {}) {
) { ) {
init(); init();
} else { } else {
handlers.push(
on( on(
'load', 'load',
() => { () => {
@@ -140,8 +147,12 @@ function record(options: recordOptions = {}) {
init(); init();
}, },
window, window,
),
); );
} }
return () => {
handlers.forEach(h => h());
};
} catch (error) { } catch (error) {
// TODO: handle internal error // TODO: handle internal error
console.warn(error); console.warn(error);

View File

@@ -365,7 +365,7 @@ function initInputObserver(cb: inputCallback): listenerHandler {
}; };
} }
export default function initObservers(o: observerParam) { export default function initObservers(o: observerParam): listenerHandler {
const mutationObserver = initMutationObserver(o.mutationCb); const mutationObserver = initMutationObserver(o.mutationCb);
const mousemoveHandler = initMousemoveObserver(o.mousemoveCb); const mousemoveHandler = initMousemoveObserver(o.mousemoveCb);
const mouseInteractionHandler = initMouseInteractionObserver( const mouseInteractionHandler = initMouseInteractionObserver(
@@ -374,12 +374,12 @@ export default function initObservers(o: observerParam) {
const scrollHandler = initScrollObserver(o.scrollCb); const scrollHandler = initScrollObserver(o.scrollCb);
const viewportResizeHandler = initViewportResizeObserver(o.viewportResizeCb); const viewportResizeHandler = initViewportResizeObserver(o.viewportResizeCb);
const inputHandler = initInputObserver(o.inputCb); const inputHandler = initInputObserver(o.inputCb);
return { return () => {
mutationObserver, mutationObserver.disconnect();
mousemoveHandler, mousemoveHandler();
mouseInteractionHandler, mouseInteractionHandler();
scrollHandler, scrollHandler();
viewportResizeHandler, viewportResizeHandler();
inputHandler, inputHandler();
}; };
} }

View File

@@ -11,8 +11,9 @@ export function on(
fn: EventListenerOrEventListenerObject, fn: EventListenerOrEventListenerObject,
target: Document | Window = document, target: Document | Window = document,
): listenerHandler { ): listenerHandler {
target.addEventListener(type, fn, { capture: true, passive: true }); const options = { capture: true, passive: true };
return () => target.removeEventListener(type, fn); target.addEventListener(type, fn, options);
return () => target.removeEventListener(type, fn, options);
} }
export const mirror: Mirror = { export const mirror: Mirror = {