Page MenuHomeWickedGov Phorge

SearchClientFactory.js
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None

SearchClientFactory.js

/**
* Factory for creating search clients
*
* @module SearchClientFactory
*/
const { CitizenCommandPaletteSearchClient } = require( '../types.js' );
const MwRestSearchClient = require( './MwRestSearchClient.js' );
/**
* Registry of available search client factory functions.
* Each factory returns a client instance.
*
* @type {Object.<string, function(mw.Map): CitizenCommandPaletteSearchClient>}
*/
const searchClientRegistry = {
MwRestSearchClient: () => new MwRestSearchClient()
};
/**
* Create a search client instance based on the configuration.
*
* @return {CitizenCommandPaletteSearchClient}
* @throws {Error} If the configured type is not registered or config is missing.
*/
function create() {
// We don't have any other client type yet
const clientType = 'MwRestSearchClient';
const factory = searchClientRegistry[ clientType ];
if ( !factory ) {
// Use mw.log.error in production? Throwing for now.
throw new Error( `Unknown or unregistered search client type configured: ${ clientType }` );
}
return factory();
}
/**
* Register a new search client type dynamically.
* Useful for extensions adding their own search clients.
*
* @param {string} type Unique identifier for the search client type.
* @param {function(mw.Map): CitizenCommandPaletteSearchClient} factory Function that creates the search client instance.
* @throws {Error} If the type is already registered.
*/
function registerSearchClient( type, factory ) {
if ( searchClientRegistry[ type ] ) {
throw new Error( `Search client type "${ type }" is already registered.` );
}
searchClientRegistry[ type ] = factory;
}
module.exports = {
create,
registerSearchClient
};

File Metadata

Mime Type
text/plain
Expires
Tue, Aug 18, 16:13 (3 w, 5 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
2a/df/3328862da42ef74186b5b56289b9
Default Alt Text
SearchClientFactory.js (1 KB)

Event Timeline