Skip to content

Commit

Permalink
Revert "temp revert rrweb-io#81"
Browse files Browse the repository at this point in the history
This reverts commit 835161c.
  • Loading branch information
Yuyz0112 committed Aug 12, 2019
1 parent 956afc7 commit 3beb572
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import record from './record';
import record, { addCustomEvent } from './record';
import { Replayer } from './replay';
import { mirror } from './utils';

Expand All @@ -8,4 +8,4 @@ export {
MouseInteractions,
ReplayerEvents,
} from './types';
export { record, Replayer, mirror };
export { record, addCustomEvent, Replayer, mirror };
15 changes: 15 additions & 0 deletions src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ function wrapEvent(e: event): eventWithTime {

let wrappedEmit!: (e: eventWithTime, isCheckout?: boolean) => void;

export function addCustomEvent<T>(tag: string, payload: T) {
if (!wrappedEmit) {
throw new Error('please add custom event after start recording');
}
wrappedEmit(
wrapEvent({
type: EventType.Custom,
data: {
tag,
payload,
},
}),
);
}

function record(options: recordOptions = {}): listenerHandler | undefined {
const {
emit,
Expand Down
16 changes: 16 additions & 0 deletions test/record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ interface ISuite extends Suite {
interface IWindow extends Window {
rrweb: {
record: (options: recordOptions) => listenerHandler | undefined;
addCustomEvent<T>(tag: string, payload: T): void;
};
emit: (e: eventWithTime) => undefined;
}
Expand Down Expand Up @@ -180,4 +181,19 @@ describe('record', function(this: ISuite) {
await this.page.waitFor(50);
assertSnapshot(this.events, __filename, 'async-checkout');
});

it('can add custom event', async () => {
await this.page.evaluate(() => {
const { record, addCustomEvent } = (window as IWindow).rrweb;
record({
emit: (window as IWindow).emit,
});
addCustomEvent<number>('tag1', 1);
addCustomEvent<{ a: string }>('tag2', {
a: 'b',
});
});
await this.page.waitFor(50);
assertSnapshot(this.events, __filename, 'custom-event');
});
});
4 changes: 2 additions & 2 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import record from './record';
import record, { addCustomEvent } from './record';
import { Replayer } from './replay';
import { mirror } from './utils';
export { EventType, IncrementalSource, MouseInteractions, ReplayerEvents, } from './types';
export { record, Replayer, mirror };
export { record, addCustomEvent, Replayer, mirror };
1 change: 1 addition & 0 deletions typings/record/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { recordOptions, listenerHandler } from '../types';
export declare function addCustomEvent<T>(tag: string, payload: T): void;
declare function record(options?: recordOptions): listenerHandler | undefined;
export default record;

0 comments on commit 3beb572

Please sign in to comment.