This repository has been archived by the owner on Nov 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e200886
commit a218b49
Showing
4 changed files
with
32 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,37 @@ | ||
import { RuntimeException } from '@adonisjs/core/exceptions' | ||
import { ManagerEngineFactory } from './types.js' | ||
import { MagnifyEngine } from './engines/main.js' | ||
|
||
export class MagnifyManager<KnownEngines extends Record<string, ManagerEngineFactory>> { | ||
#cachedEngines: Partial<Record<keyof KnownEngines, MagnifyEngine>> = {} | ||
#cachedEngines: Map<keyof KnownEngines, ReturnType<KnownEngines[keyof KnownEngines]>> = new Map() | ||
|
||
constructor(public config: { default?: keyof KnownEngines; engines: KnownEngines }) {} | ||
|
||
/** | ||
* Use one of the registered engines. | ||
* | ||
* ```ts | ||
* manager.use() // returns default engine | ||
* manager.use('meilisearch') | ||
* manager.engine() // returns default engine | ||
* manager.engine('meilisearch') | ||
* ``` | ||
*/ | ||
engine<EngineName extends keyof KnownEngines>(engine?: EngineName): MagnifyEngine { | ||
const engineToUse = engine || this.config.default | ||
engine<EngineName extends keyof KnownEngines>( | ||
engine?: EngineName | ||
): ReturnType<KnownEngines[EngineName]> { | ||
const engineToUse = (engine || this.config.default) as keyof KnownEngines | ||
if (!engineToUse) throw new RuntimeException('No search engine selected') | ||
|
||
if (!engineToUse) { | ||
throw new RuntimeException( | ||
'Cannot create engine instance. No default engine is defined in the config.' | ||
) | ||
/** | ||
* Check if the search engine was already instantiated | ||
*/ | ||
if (this.#cachedEngines.has(engineToUse)) { | ||
return this.#cachedEngines.get(engineToUse)! | ||
} | ||
|
||
const cachedEngine = this.#cachedEngines[engineToUse] | ||
if (cachedEngine) { | ||
return cachedEngine | ||
} | ||
|
||
return this.config.engines[engineToUse]() | ||
/** | ||
* Otherwise create a new instance and cache it | ||
*/ | ||
const newEngine = this.config.engines[engineToUse]() as ReturnType<KnownEngines[EngineName]> | ||
this.#cachedEngines.set(engineToUse, newEngine) | ||
return newEngine | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters