Skip to content

Commit

Permalink
refactor: ♻️ refactor plugin-simple-throttle
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerbenw committed Nov 12, 2024
1 parent edd8ffb commit 49b81dc
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 33 deletions.
14 changes: 13 additions & 1 deletion packages/plugin-simple-throttle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
"name": "@bugsnag/plugin-simple-throttle",
"version": "8.1.1",
"main": "throttle.js",
"types": "dist/types/throttle.d.ts",
"exports": {
".": {
"types": "./dist/types/throttle.d.ts",
"default": "./dist/throttle.js",
"import": "./dist/throttle.mjs"
}
},
"description": "@bugsnag/js plugin to prevent too many events from being sent",
"homepage": "https://www.bugsnag.com/",
"repository": {
Expand All @@ -14,7 +22,11 @@
"files": [
"*.js"
],
"scripts": {},
"scripts": {
"build": "npm run build:npm",
"build:npm": "rollup --config rollup.config.npm.mjs",
"clean": "rm -rf dist/*"
},
"author": "Bugsnag",
"license": "MIT",
"devDependencies": {
Expand Down
6 changes: 6 additions & 0 deletions packages/plugin-simple-throttle/rollup.config.npm.mjs
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']
})
49 changes: 49 additions & 0 deletions packages/plugin-simple-throttle/src/throttle.ts
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
2 changes: 1 addition & 1 deletion packages/plugin-simple-throttle/test/throttle.test.ts
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'

Expand Down
31 changes: 0 additions & 31 deletions packages/plugin-simple-throttle/throttle.js

This file was deleted.

5 changes: 5 additions & 0 deletions packages/plugin-simple-throttle/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*.ts"]
}

0 comments on commit 49b81dc

Please sign in to comment.