add hooks API (#132)
This commit is contained in:
@@ -34,6 +34,7 @@ function record(options: recordOptions = {}): listenerHandler | undefined {
|
|||||||
ignoreClass = 'rr-ignore',
|
ignoreClass = 'rr-ignore',
|
||||||
inlineStylesheet = true,
|
inlineStylesheet = true,
|
||||||
maskAllInputs = false,
|
maskAllInputs = false,
|
||||||
|
hooks,
|
||||||
} = options;
|
} = options;
|
||||||
// runtime checks for user options
|
// runtime checks for user options
|
||||||
if (!emit) {
|
if (!emit) {
|
||||||
@@ -114,7 +115,8 @@ function record(options: recordOptions = {}): listenerHandler | undefined {
|
|||||||
takeFullSnapshot();
|
takeFullSnapshot();
|
||||||
|
|
||||||
handlers.push(
|
handlers.push(
|
||||||
initObservers({
|
initObservers(
|
||||||
|
{
|
||||||
mutationCb: m =>
|
mutationCb: m =>
|
||||||
wrappedEmit(
|
wrappedEmit(
|
||||||
wrapEvent({
|
wrapEvent({
|
||||||
@@ -179,7 +181,9 @@ function record(options: recordOptions = {}): listenerHandler | undefined {
|
|||||||
ignoreClass,
|
ignoreClass,
|
||||||
maskAllInputs,
|
maskAllInputs,
|
||||||
inlineStylesheet,
|
inlineStylesheet,
|
||||||
}),
|
},
|
||||||
|
hooks,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
if (
|
if (
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ import {
|
|||||||
attributeCursor,
|
attributeCursor,
|
||||||
blockClass,
|
blockClass,
|
||||||
IncrementalSource,
|
IncrementalSource,
|
||||||
|
hooksParam,
|
||||||
|
Arguments,
|
||||||
} from '../types';
|
} from '../types';
|
||||||
import { deepDelete, isParentRemoved, isAncestorInSet } from './collection';
|
import { deepDelete, isParentRemoved, isAncestorInSet } from './collection';
|
||||||
|
|
||||||
@@ -506,7 +508,58 @@ function initInputObserver(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function initObservers(o: observerParam): listenerHandler {
|
function mergeHooks(o: observerParam, hooks: hooksParam) {
|
||||||
|
const {
|
||||||
|
mutationCb,
|
||||||
|
mousemoveCb,
|
||||||
|
mouseInteractionCb,
|
||||||
|
scrollCb,
|
||||||
|
viewportResizeCb,
|
||||||
|
inputCb,
|
||||||
|
} = o;
|
||||||
|
o.mutationCb = (...p: Arguments<mutationCallBack>) => {
|
||||||
|
if (hooks.mutation) {
|
||||||
|
hooks.mutation(...p);
|
||||||
|
}
|
||||||
|
mutationCb(...p);
|
||||||
|
};
|
||||||
|
o.mousemoveCb = (...p: Arguments<mousemoveCallBack>) => {
|
||||||
|
if (hooks.mousemove) {
|
||||||
|
hooks.mousemove(...p);
|
||||||
|
}
|
||||||
|
mousemoveCb(...p);
|
||||||
|
};
|
||||||
|
o.mouseInteractionCb = (...p: Arguments<mouseInteractionCallBack>) => {
|
||||||
|
if (hooks.mouseInteraction) {
|
||||||
|
hooks.mouseInteraction(...p);
|
||||||
|
}
|
||||||
|
mouseInteractionCb(...p);
|
||||||
|
};
|
||||||
|
o.scrollCb = (...p: Arguments<scrollCallback>) => {
|
||||||
|
if (hooks.scroll) {
|
||||||
|
hooks.scroll(...p);
|
||||||
|
}
|
||||||
|
scrollCb(...p);
|
||||||
|
};
|
||||||
|
o.viewportResizeCb = (...p: Arguments<viewportResizeCallback>) => {
|
||||||
|
if (hooks.viewportResize) {
|
||||||
|
hooks.viewportResize(...p);
|
||||||
|
}
|
||||||
|
viewportResizeCb(...p);
|
||||||
|
};
|
||||||
|
o.inputCb = (...p: Arguments<inputCallback>) => {
|
||||||
|
if (hooks.input) {
|
||||||
|
hooks.input(...p);
|
||||||
|
}
|
||||||
|
inputCb(...p);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function initObservers(
|
||||||
|
o: observerParam,
|
||||||
|
hooks: hooksParam = {},
|
||||||
|
): listenerHandler {
|
||||||
|
mergeHooks(o, hooks);
|
||||||
const mutationObserver = initMutationObserver(
|
const mutationObserver = initMutationObserver(
|
||||||
o.mutationCb,
|
o.mutationCb,
|
||||||
o.blockClass,
|
o.blockClass,
|
||||||
|
|||||||
14
src/types.ts
14
src/types.ts
@@ -119,6 +119,7 @@ export type recordOptions = {
|
|||||||
ignoreClass?: string;
|
ignoreClass?: string;
|
||||||
maskAllInputs?: boolean;
|
maskAllInputs?: boolean;
|
||||||
inlineStylesheet?: boolean;
|
inlineStylesheet?: boolean;
|
||||||
|
hooks?: hooksParam;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type observerParam = {
|
export type observerParam = {
|
||||||
@@ -134,6 +135,15 @@ export type observerParam = {
|
|||||||
inlineStylesheet: boolean;
|
inlineStylesheet: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type hooksParam = {
|
||||||
|
mutation?: mutationCallBack;
|
||||||
|
mousemove?: mousemoveCallBack;
|
||||||
|
mouseInteraction?: mouseInteractionCallBack;
|
||||||
|
scroll?: scrollCallback;
|
||||||
|
viewportResize?: viewportResizeCallback;
|
||||||
|
input?: inputCallback;
|
||||||
|
};
|
||||||
|
|
||||||
export type textCursor = {
|
export type textCursor = {
|
||||||
node: Node;
|
node: Node;
|
||||||
value: string | null;
|
value: string | null;
|
||||||
@@ -285,6 +295,10 @@ export type Emitter = {
|
|||||||
emit(type: string, event?: unknown): void;
|
emit(type: string, event?: unknown): void;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type Arguments<T> = T extends (...payload: infer U) => unknown
|
||||||
|
? U
|
||||||
|
: unknown;
|
||||||
|
|
||||||
export enum ReplayerEvents {
|
export enum ReplayerEvents {
|
||||||
Start = 'start',
|
Start = 'start',
|
||||||
Pause = 'pause',
|
Pause = 'pause',
|
||||||
|
|||||||
4
typings/record/observer.d.ts
vendored
4
typings/record/observer.d.ts
vendored
@@ -1,2 +1,2 @@
|
|||||||
import { observerParam, listenerHandler } from '../types';
|
import { observerParam, listenerHandler, hooksParam } from '../types';
|
||||||
export default function initObservers(o: observerParam): listenerHandler;
|
export default function initObservers(o: observerParam, hooks?: hooksParam): listenerHandler;
|
||||||
|
|||||||
10
typings/types.d.ts
vendored
10
typings/types.d.ts
vendored
@@ -88,6 +88,7 @@ export declare type recordOptions = {
|
|||||||
ignoreClass?: string;
|
ignoreClass?: string;
|
||||||
maskAllInputs?: boolean;
|
maskAllInputs?: boolean;
|
||||||
inlineStylesheet?: boolean;
|
inlineStylesheet?: boolean;
|
||||||
|
hooks?: hooksParam;
|
||||||
};
|
};
|
||||||
export declare type observerParam = {
|
export declare type observerParam = {
|
||||||
mutationCb: mutationCallBack;
|
mutationCb: mutationCallBack;
|
||||||
@@ -101,6 +102,14 @@ export declare type observerParam = {
|
|||||||
maskAllInputs: boolean;
|
maskAllInputs: boolean;
|
||||||
inlineStylesheet: boolean;
|
inlineStylesheet: boolean;
|
||||||
};
|
};
|
||||||
|
export declare type hooksParam = {
|
||||||
|
mutation?: mutationCallBack;
|
||||||
|
mousemove?: mousemoveCallBack;
|
||||||
|
mouseInteraction?: mouseInteractionCallBack;
|
||||||
|
scroll?: scrollCallback;
|
||||||
|
viewportResize?: viewportResizeCallback;
|
||||||
|
input?: inputCallback;
|
||||||
|
};
|
||||||
export declare type textCursor = {
|
export declare type textCursor = {
|
||||||
node: Node;
|
node: Node;
|
||||||
value: string | null;
|
value: string | null;
|
||||||
@@ -225,6 +234,7 @@ export declare type Emitter = {
|
|||||||
on(type: string, handler: Handler): void;
|
on(type: string, handler: Handler): void;
|
||||||
emit(type: string, event?: unknown): void;
|
emit(type: string, event?: unknown): void;
|
||||||
};
|
};
|
||||||
|
export declare type Arguments<T> = T extends (...payload: infer U) => unknown ? U : unknown;
|
||||||
export declare enum ReplayerEvents {
|
export declare enum ReplayerEvents {
|
||||||
Start = "start",
|
Start = "start",
|
||||||
Pause = "pause",
|
Pause = "pause",
|
||||||
|
|||||||
Reference in New Issue
Block a user