Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
[3.0.3] Add in Container.find, update typings for Container.$ref,…
Browse files Browse the repository at this point in the history
… fix GitHub URL to point to Noelware/Lilith
  • Loading branch information
auguwu committed May 21, 2021
1 parent 70132da commit ce42620
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
13 changes: 11 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ declare namespace Lilith {
/**
* Returns a reference from the component, singleton, or service tree
* @param ref The reference to find
* @typeparam Ref The reference by class (it'll return `typeof <ref>`, use the second generic to return the class)
* @typeparam TReturn The return value
*/
public $ref<Ref extends any, TReturn extends any = any>(ref: Ref): TReturn;
public $ref<TReturn extends BaseSingleton | BaseService | BaseComponent>(ref: any): TReturn;

/**
* Injects all pending references to the target class
Expand Down Expand Up @@ -110,6 +109,16 @@ declare namespace Lilith {
* Runs all injections for components/services
*/
public runInjections(): void;

/**
* Finds a component, service, or singleton by a specific `predicate` function
* @param func The predicate function to find the component, service, or singleton's constructor type or name
* @returns The component, service, singleton found or `null` if nothing was found
*/
public find<S extends BaseComponent | BaseService | BaseSingleton, ThisArg = Container>(
func: (value: BaseComponent | BaseService | BaseSingleton) => boolean,
thisArg?: ThisArg
): S | null;
}

// ~ Decorators ~
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "@augu/lilith",
"description": "🧵 Simple application framework made in TypeScript (made for personal usage)",
"version": "3.0.2",
"version": "3.0.3",
"main": "build/index.js",
"license": "MIT",
"author": "Chris \"August\" Hernandez <[email protected]>",
"repository": "https://github.com/auguwu/Lillith",
"repository": "https://github.com/Noelware/Lilith",
"bugs": "https://github.com/Noelware/Lilith/issues",
"types": "index.d.ts",
"files": [
"build/",
Expand Down
26 changes: 25 additions & 1 deletion src/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export class Container extends utils.EventBus<ContainerEvents> {
* @typeparam Ref The reference by class (it'll return `typeof <ref>`, use the second generic to return the class)
* @typeparam TReturn The return value
*/
$ref<Ref extends any, TReturn extends any = any>(ref: Ref): TReturn {
$ref<TReturn extends BaseSingleton | BaseService | BaseComponent>(ref: any): TReturn {
const $ref = this.#references.get(ref);
if ($ref === undefined)
throw new TypeError('Reference was not found');
Expand Down Expand Up @@ -474,4 +474,28 @@ export class Container extends utils.EventBus<ContainerEvents> {
this.#references.set(service._classRef.constructor, service.name);
this.services.set(service.name, service);
}

/**
* Finds a component, service, or singleton by a specific `predicate` function
* @param func The predicate function to find the component, service, or singleton's constructor type or name
* @returns The component, service, singleton found or `null` if nothing was found
*/
find<S extends BaseComponent | BaseService | BaseSingleton, ThisArg = Container>(
func: (value: BaseComponent | BaseService | BaseSingleton) => boolean,
thisArg?: ThisArg
) {
const values: (BaseComponent | BaseService | BaseSingleton)[] = ([] as any[]).concat(
this.components.toArray(),
this.services.toArray(),
this.singletons.toArray()
);

for (let i = 0; i < values.length; i++) {
const value = values[i];
if (func.call(thisArg !== undefined ? thisArg : this, value))
return value as S;
}

return null;
}
}

0 comments on commit ce42620

Please sign in to comment.