-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ♻️ refactor plugin-simple-throttle
- Loading branch information
1 parent
edd8ffb
commit 49b81dc
Showing
6 changed files
with
74 additions
and
33 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,6 @@ | ||
import createRollupConfig from '../../.rollup/index.mjs' | ||
|
||
export default createRollupConfig({ | ||
input: 'src/throttle.ts', | ||
external: ['@bugsnag/core/lib/validators/int-range'] | ||
}) |
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,49 @@ | ||
import { Client, Config, Logger, Plugin } from '@bugsnag/core' | ||
import intRange from '@bugsnag/core/lib/validators/int-range' | ||
|
||
interface ThrottlePlugin extends Plugin { | ||
configSchema: { | ||
[key: string]: { | ||
defaultValue: () => unknown | ||
message: string | ||
validate: (value: unknown) => boolean | ||
} | ||
} | ||
} | ||
|
||
interface InternalClient extends Client { | ||
_config: Config & { maxEvents: number } | ||
_logger: Logger | ||
} | ||
|
||
/* | ||
* Throttles and dedupes events | ||
*/ | ||
|
||
const plugin: ThrottlePlugin = { | ||
load: (client) => { | ||
// track sent events for each init of the plugin | ||
let n = 0 | ||
|
||
// add onError hook | ||
client.addOnError((event) => { | ||
// have max events been sent already? | ||
if (n >= (client as InternalClient)._config.maxEvents) { | ||
(client as InternalClient)._logger.warn(`Cancelling event send due to maxEvents per session limit of ${(client as InternalClient)._config.maxEvents} being reached`) | ||
return false | ||
} | ||
n++ | ||
}) | ||
|
||
client.resetEventCount = () => { n = 0 } | ||
}, | ||
configSchema: { | ||
maxEvents: { | ||
defaultValue: () => 10, | ||
message: 'should be a positive integer ≤100', | ||
validate: val => intRange(1, 100)(val) | ||
} | ||
} | ||
} | ||
|
||
export default plugin |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import plugin from '../' | ||
import plugin from '../src/throttle' | ||
|
||
import Client from '@bugsnag/core/client' | ||
|
||
|
This file was deleted.
Oops, something went wrong.
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,5 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["src/**/*.ts"] | ||
} | ||
|