-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
John/hig-1984-pull-in-rrweb-sequential-id-pr (#65)
* Add sequential IDs * rrweb-io/rrweb#840 * bump version
- Loading branch information
John Pham
authored
Feb 22, 2022
1 parent
d6143d2
commit 9f3fe95
Showing
6 changed files
with
106 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { RecordPlugin } from '../../../types'; | ||
|
||
export type SequentialIdOptions = { | ||
key: string; | ||
}; | ||
|
||
const defaultOptions: SequentialIdOptions = { | ||
key: '_sid', | ||
}; | ||
|
||
export const PLUGIN_NAME = 'rrweb/sequential-id@1'; | ||
|
||
export const getRecordSequentialIdPlugin: ( | ||
options?: Partial<SequentialIdOptions>, | ||
) => RecordPlugin = (options) => { | ||
const _options = options | ||
? Object.assign({}, defaultOptions, options) | ||
: defaultOptions; | ||
let id = 0; | ||
|
||
return { | ||
name: PLUGIN_NAME, | ||
eventProcessor(event) { | ||
Object.assign(event, { | ||
[_options.key]: ++id, | ||
}); | ||
return event; | ||
}, | ||
options: _options, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { SequentialIdOptions } from '../record'; | ||
import { ReplayPlugin, eventWithTime } from '../../../types'; | ||
|
||
type Options = SequentialIdOptions & { | ||
warnOnMissingId: boolean; | ||
}; | ||
|
||
const defaultOptions: Options = { | ||
key: '_sid', | ||
warnOnMissingId: true, | ||
}; | ||
|
||
export const getReplaySequentialIdPlugin: ( | ||
options?: Partial<Options>, | ||
) => ReplayPlugin = (options) => { | ||
const { key, warnOnMissingId } = options | ||
? Object.assign({}, defaultOptions, options) | ||
: defaultOptions; | ||
let currentId = 1; | ||
|
||
return { | ||
handler(event: eventWithTime) { | ||
if (key in event) { | ||
const id = ((event as unknown) as Record<string, number>)[key]; | ||
if (id !== currentId) { | ||
console.error( | ||
`[sequential-id-plugin]: expect to get an id with value "${currentId}", but got "${id}"`, | ||
); | ||
} else { | ||
currentId++; | ||
} | ||
} else if (warnOnMissingId) { | ||
console.warn( | ||
`[sequential-id-plugin]: failed to get id in key: "${key}"`, | ||
); | ||
} | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters