From 11d92f85d1fd6b9abf2b5b0881f2046f35f2722f Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Sun, 6 Jul 2025 17:31:09 -0700 Subject: [PATCH] refactor(jsdoc-util): remove unused `EventBus` class --- packages/jsdoc-util/index.js | 5 +- packages/jsdoc-util/lib/bus.js | 65 ------------------ packages/jsdoc-util/test/specs/index.js | 8 +-- packages/jsdoc-util/test/specs/lib/bus.js | 83 ----------------------- 4 files changed, 6 insertions(+), 155 deletions(-) delete mode 100644 packages/jsdoc-util/lib/bus.js delete mode 100644 packages/jsdoc-util/test/specs/lib/bus.js diff --git a/packages/jsdoc-util/index.js b/packages/jsdoc-util/index.js index 2fcee2f9..76c3fd1e 100644 --- a/packages/jsdoc-util/index.js +++ b/packages/jsdoc-util/index.js @@ -19,9 +19,8 @@ * * @module @jsdoc/util */ -import EventBus from './lib/bus.js'; import cast from './lib/cast.js'; import getLogFunctions from './lib/log.js'; -export { cast, EventBus, getLogFunctions }; -export default { cast, EventBus, getLogFunctions }; +export { cast, getLogFunctions }; +export default { cast, getLogFunctions }; diff --git a/packages/jsdoc-util/lib/bus.js b/packages/jsdoc-util/lib/bus.js deleted file mode 100644 index 6684fad0..00000000 --- a/packages/jsdoc-util/lib/bus.js +++ /dev/null @@ -1,65 +0,0 @@ -/* - Copyright 2019 the JSDoc Authors. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - https://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ -import EventEmitter from 'node:events'; - -import _ from 'lodash'; -import ow from 'ow'; - -let cache = {}; - -/** - * An event bus that works the same way as a standard Node.js event emitter, with a few key - * differences: - * - * + When you create an event bus, you must specify its name. Consider using your package's name. - * + Event buses are cached and shared by default. If module A and module B both create an event bus - * called `foo`, both modules get the same event bus. This behavior makes it easier to share one - * event bus among all of the modules in your package. - * - * To prevent a new event bus from being cached and shared, set the `opts.cache` property to - * `false` when you create the event bus. Setting this property to `false` also forces a new - * event bus to be created, even if there's a cached event bus with the same name. - * - * @alias module:@jsdoc/util.EventBus - * @extends module:events.EventEmitter - */ -export default class EventBus extends EventEmitter { - /** - * Create a new event bus, or retrieve the cached event bus for the ID you specify. - * - * @param {(string|Symbol)} id - The ID for the event bus. - * @param {Object} opts - Options for the event bus. - * @param {boolean} [opts.cache=true] - Set to `false` to prevent the event bus from being - * cached, and to return a new event bus even if there is already an event bus with the same ID. - */ - constructor(id, opts = {}) { - super(); - - ow(id, ow.any(ow.string, ow.symbol)); - - const shouldCache = _.isBoolean(opts.cache) ? opts.cache : true; - - if (Object.hasOwn(cache, id) && shouldCache) { - return cache[id]; - } - - this._id = id; - - if (shouldCache) { - cache[id] = this; - } - } -} diff --git a/packages/jsdoc-util/test/specs/index.js b/packages/jsdoc-util/test/specs/index.js index 78b2a9e9..4c2b05c4 100644 --- a/packages/jsdoc-util/test/specs/index.js +++ b/packages/jsdoc-util/test/specs/index.js @@ -15,8 +15,8 @@ */ import util from '../../index.js'; -import bus from '../../lib/bus.js'; import cast from '../../lib/cast.js'; +import log from '../../lib/log.js'; describe('@jsdoc/util', () => { it('is an object', () => { @@ -29,9 +29,9 @@ describe('@jsdoc/util', () => { }); }); - describe('EventBus', () => { - it('is lib/bus', () => { - expect(util.EventBus).toEqual(bus); + describe('getLogFunctions', () => { + it('is lib/log', () => { + expect(util.getLogFunctions).toEqual(log); }); }); }); diff --git a/packages/jsdoc-util/test/specs/lib/bus.js b/packages/jsdoc-util/test/specs/lib/bus.js deleted file mode 100644 index 85dc31af..00000000 --- a/packages/jsdoc-util/test/specs/lib/bus.js +++ /dev/null @@ -1,83 +0,0 @@ -/* - Copyright 2019 the JSDoc Authors. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - https://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -import EventEmitter from 'node:events'; - -import EventBus from '../../../lib/bus.js'; - -describe('@jsdoc/util/lib/bus', () => { - const ignoreCache = { cache: false }; - - it('inherits from EventEmitter', () => { - expect(new EventBus('foo', ignoreCache) instanceof EventEmitter).toBeTrue(); - }); - - it('accepts a string for the ID', () => { - function makeBus() { - return new EventBus('foo', ignoreCache); - } - - expect(makeBus).not.toThrow(); - }); - - it('accepts a Symbol for the ID', () => { - function makeBus() { - return new EventBus(Symbol('foo'), ignoreCache); - } - - expect(makeBus).not.toThrow(); - }); - - it('throws on bad IDs', () => { - function crashBus() { - return new EventBus(true, ignoreCache); - } - - expect(crashBus).toThrowError(); - }); - - it('uses a cache by default', () => { - let fired = false; - const id = Symbol('cache-test'); - const bus1 = new EventBus(id); - const bus2 = new EventBus(id); - - bus1.once('foo', () => { - fired = true; - }); - - bus2.emit('foo'); - - expect(bus1).toBe(bus2); - expect(fired).toBeTrue(); - }); - - it('ignores the cache when asked', () => { - let fired = false; - const id = Symbol('cache-test'); - const bus1 = new EventBus(id, ignoreCache); - const bus2 = new EventBus(id, ignoreCache); - - bus1.once('foo', () => { - fired = true; - }); - - bus2.emit('foo'); - - expect(bus1).not.toBe(bus2); - expect(fired).toBeFalse(); - }); -});