-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #246 from proto-kit/feature/closeable
Added closeable collection and shutdown mechanism
- Loading branch information
Showing
30 changed files
with
361 additions
and
35 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
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { TypedClass } from "../types"; | ||
|
||
export const injectAliasMetadataKey = "protokit-inject-alias"; | ||
|
||
/** | ||
* Attaches metadata to the class that the ModuleContainer can pick up | ||
* and inject this class in the DI container under the specified aliases. | ||
* This method supports inheritance, therefore also gets aliases defined | ||
* on superclasses | ||
*/ | ||
export function injectAlias(aliases: string[]) { | ||
return (target: TypedClass<unknown>) => { | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
const superAliases = Reflect.getMetadata( | ||
injectAliasMetadataKey, | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
Object.getPrototypeOf(target) | ||
) as string[] | undefined; | ||
|
||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
const existingAliases = Reflect.getMetadata( | ||
injectAliasMetadataKey, | ||
target | ||
) as string[] | undefined; | ||
|
||
let allAliases = aliases; | ||
|
||
if (superAliases !== undefined) { | ||
allAliases = allAliases.concat(superAliases); | ||
} | ||
if (existingAliases !== undefined) { | ||
allAliases = allAliases.concat(existingAliases); | ||
} | ||
|
||
Reflect.defineMetadata( | ||
injectAliasMetadataKey, | ||
allAliases.filter( | ||
(value, index, array) => array.indexOf(value) === index | ||
), | ||
target | ||
); | ||
}; | ||
} | ||
|
||
/** | ||
* Marks the class to implement a certain interface T, while also attaching | ||
* a DI-injection alias as metadata, that will be picked up by the ModuleContainer | ||
* to allow resolving by that interface name | ||
* @param name The name of the injection alias, convention is to use the same as the name of T | ||
*/ | ||
export function implement<T>(name: string) { | ||
return ( | ||
/** | ||
* Check if the target class extends RuntimeModule, while | ||
* also providing static config presets | ||
*/ | ||
target: TypedClass<T> | ||
) => { | ||
injectAlias([name])(target); | ||
}; | ||
} | ||
|
||
export function getInjectAliases(target: TypedClass<unknown>): string[] { | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
const aliases = Reflect.getMetadata( | ||
injectAliasMetadataKey, | ||
target | ||
) as string[]; | ||
return aliases ?? []; | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import "reflect-metadata"; | ||
import { getInjectAliases, injectAlias } from "../../src/config/injectAlias"; | ||
|
||
@injectAlias(["foo", "bar"]) | ||
class TestClass {} | ||
|
||
@injectAlias(["ayy"]) | ||
class TestClass2 extends TestClass {} | ||
|
||
describe("injectAlias metadata", () => { | ||
it("set and retrieve", () => { | ||
expect.assertions(2); | ||
|
||
const aliases = getInjectAliases(TestClass); | ||
|
||
expect(aliases).toHaveLength(2); | ||
expect(aliases).toStrictEqual(["foo", "bar"]); | ||
}); | ||
|
||
it("recursive", () => { | ||
expect.assertions(2); | ||
|
||
const aliases = getInjectAliases(TestClass2); | ||
|
||
expect(aliases).toHaveLength(3); | ||
expect(aliases).toStrictEqual(["ayy", "foo", "bar"]); | ||
}); | ||
}); |
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
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
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
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
Oops, something went wrong.