all files / src/ Registry.js

100% Statements 29/29
100% Branches 12/12
100% Functions 4/4
100% Lines 25/25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                          18×     18×                                                               30× 26× 26×                         26× 22× 22× 18× 18×                         82× 46×      
/* global hasOwn */
 
/**
 * A map interface for associating arbitrary data with a symbolic name. Used in
 * place of a plain object to provide additional {@link OO.Registry#register registration}
 * or {@link OO.Registry#lookup lookup} functionality.
 *
 * See <https://www.mediawiki.org/wiki/OOjs/Registries_and_factories>.
 *
 * @class
 * @mixes OO.EventEmitter
 */
OO.Registry = function OoRegistry() {
	// Mixin constructors
	OO.EventEmitter.call( this );
 
	// Properties
	this.registry = {};
};
 
/* Inheritance */
 
OO.mixinClass( OO.Registry, OO.EventEmitter );
 
/* Events */
 
/**
 * @event OO.Registry#register
 * @param {string} name
 * @param {any} data
 */
 
/**
 * @event OO.Registry#unregister
 * @param {string} name
 * @param {any} data Data removed from registry
 */
 
/* Methods */
 
/**
 * Associate one or more symbolic names with some data.
 *
 * Any existing entry with the same name will be overridden.
 *
 * @param {string|string[]} name Symbolic name or list of symbolic names
 * @param {any} data Data to associate with symbolic name
 * @fires OO.Registry#register
 * @throws {Error} Name argument must be a string or array
 */
OO.Registry.prototype.register = function ( name, data ) {
	if ( typeof name === 'string' ) {
		this.registry[ name ] = data;
		this.emit( 'register', name, data );
	} else if ( Array.isArray( name ) ) {
		for ( let i = 0, len = name.length; i < len; i++ ) {
			this.register( name[ i ], data );
		}
	} else {
		throw new Error( 'Name must be a string or array, cannot be a ' + typeof name );
	}
};
 
/**
 * Remove one or more symbolic names from the registry.
 *
 * @param {string|string[]} name Symbolic name or list of symbolic names
 * @fires OO.Registry#unregister
 * @throws {Error} Name argument must be a string or array
 */
OO.Registry.prototype.unregister = function ( name ) {
	if ( typeof name === 'string' ) {
		const data = this.lookup( name );
		if ( data !== undefined ) {
			delete this.registry[ name ];
			this.emit( 'unregister', name, data );
		}
	} else if ( Array.isArray( name ) ) {
		for ( let i = 0, len = name.length; i < len; i++ ) {
			this.unregister( name[ i ] );
		}
	} else {
		throw new Error( 'Name must be a string or array, cannot be a ' + typeof name );
	}
};
 
/**
 * Get data for a given symbolic name.
 *
 * @param {string} name Symbolic name
 * @return {any|undefined} Data associated with symbolic name
 */
OO.Registry.prototype.lookup = function ( name ) {
	if ( hasOwn.call( this.registry, name ) ) {
		return this.registry[ name ];
	}
};