diff --git a/index.js b/index.js index ee5fa6f..1e7f87f 100644 --- a/index.js +++ b/index.js @@ -43,31 +43,7 @@ const { format } = require('node:util') function processWarning () { const codes = {} const emitted = new Map() - - /** - * @typedef {number} STATE_CONSTANT - */ - - /** - * @private - * @typdef {object} EMISSION_STATES - * @property {STATE_CONSTANT} UNLIMITED_INITIAL Indicates that the warning - * is to be issued an unlimited number of times but has not yet been - * emitted. - * @property {STATE_CONSTANT} UNLIMITED_ONGOING Indicates that the warning - * is to be issued an unlimited number of times and has been emitted at least - * once. - * @property {STATE_CONSTANT} LIMITED_INITIAL Indicates that the warning - * is to be issued only once but has not yet been emitted. - * @property {STATE_CONSTANT} LIMITED_FINAL Indicates that the warning - * is to be issued only once and has already been emitted. - */ - const STATES = { - UNLIMITED_INITIAL: 0, - UNLIMITED_ONGOING: -1, - LIMITED_INITIAL: 1, - LIMITED_FINAL: 2 - } + const opts = Object.create(null) /** * Builds a new {@link ProcessWarning} and adds it to the @@ -115,7 +91,8 @@ function processWarning () { } } - emitted.set(code, unlimited ? STATES.UNLIMITED_INITIAL : STATES.LIMITED_INITIAL) + Object.assign(opts, { unlimited }) + emitted.set(code, unlimited) codes[code] = buildWarnOpts return codes[code] @@ -160,13 +137,9 @@ function processWarning () { * @param {*} [c] Possible message interpolation value. */ function emit (code, a, b, c) { - const state = emitted.get(code) - if (state === STATES.LIMITED_FINAL) return + if (emitted.get(code) === true && opts.unlimited === false) return if (codes[code] === undefined) throw new Error(`The code '${code}' does not exist`) - emitted.set( - code, - state <= STATES.UNLIMITED_INITIAL ? STATES.UNLIMITED_ONGOING : STATES.LIMITED_FINAL - ) + emitted.set(code, true) const warning = codes[code](a, b, c) process.emitWarning(warning.message, warning.name, warning.code) @@ -176,12 +149,7 @@ function processWarning () { create, createDeprecation, emit, - emitted: { - get (code) { - const state = emitted.get(code) - return state === STATES.UNLIMITED_ONGOING || state === STATES.LIMITED_FINAL - } - } + emitted } } diff --git a/package.json b/package.json index 84d8b8f..2410cb3 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,6 @@ "scripts": { "lint": "standard", "lint:fix": "standard --fix", - "benchmark": "node benchmarks/warn.js", "test": "npm run test:unit && npm run test:jest && npm run test:typescript", "test:jest": "jest jest.test.js", "test:unit": "tap", diff --git a/test/emit-once-only.test.js b/test/emit-once-only.test.js index 75d9036..e1d8a30 100644 --- a/test/emit-once-only.test.js +++ b/test/emit-once-only.test.js @@ -1,6 +1,6 @@ 'use strict' -const { test } = require('tap') +const test = require('tap').test const build = require('..') test('emit should emit a given code only once', t => { diff --git a/test/issue-88.test.js b/test/issue-88.test.js deleted file mode 100644 index 79696c0..0000000 --- a/test/issue-88.test.js +++ /dev/null @@ -1,26 +0,0 @@ -'use strict' - -const { test } = require('tap') -const build = require('..') - -test('Must not overwrite the global config', t => { - t.plan(1) - - const { create, emit } = build() - - function onWarning (warning) { - t.equal(warning.code, 'CODE_1') - } - - create('FastifyWarning', 'CODE_1', 'Msg', { unlimited: false }) - create('FastifyWarning', 'CODE_2', 'Msg', { unlimited: true }) - - process.on('warning', onWarning) - emit('CODE_1') - emit('CODE_1') - - setImmediate(() => { - process.removeListener('warning', onWarning) - t.end() - }) -})