refactor(jsdoc-util): remove unused EventBus class

This commit is contained in:
Jeff Williams 2025-07-06 17:31:09 -07:00
parent 8a0b40502e
commit 11d92f85d1
No known key found for this signature in database
4 changed files with 6 additions and 155 deletions

View File

@ -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 };

View File

@ -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;
}
}
}

View File

@ -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);
});
});
});

View File

@ -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();
});
});