-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use Symbol.metadata for storing Handlers and Hooks configur…
…ation
- Loading branch information
Showing
11 changed files
with
107 additions
and
273 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { IErrorHandler, IHandler, IHook } from '../../interfaces/index.js'; | ||
import { ERROR_HANDLERS, HANDLERS, HOOKS, METADATA } from '../../symbols/index.js'; | ||
import { Container } from './container.js'; | ||
|
||
// TODO: Support for ES Decorators | ||
export function getHandlersContainer<T extends object>(target: T): Container<IHandler> { | ||
return getContainer(target, HANDLERS); | ||
} | ||
|
||
export function getHooksContainer<T extends object>(target: T): Container<IHook> { | ||
return getContainer(target, HOOKS); | ||
} | ||
|
||
export function getErrorHandlerContainer<T extends object>(target: T): Container<IErrorHandler> { | ||
return getContainer(target, ERROR_HANDLERS); | ||
} | ||
|
||
/// Internal | ||
|
||
function getContainer<T extends object, Type>(target: T, name: symbol): Container<Type> { | ||
const metadata = getMetadata(target); | ||
const base = (target as new () => unknown).prototype.__proto__?.constructor?.[METADATA]?.[name] as Container<Type>; | ||
|
||
if (!Object.prototype.hasOwnProperty.call(metadata, name)) { | ||
Reflect.defineProperty(metadata, name, { | ||
value: new Container(base), | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
}); | ||
} | ||
|
||
return metadata[name as keyof typeof metadata] as Container<Type>; | ||
} | ||
|
||
function getMetadata<T extends object>(target: T): Record<symbol, Container<unknown>> { | ||
ensureMetadata(target); | ||
return target[METADATA]; | ||
} | ||
|
||
function ensureMetadata<T extends object>(target: T): asserts target is T & { [METADATA]: Record<symbol, Container<unknown>> } { | ||
if (!Object.prototype.hasOwnProperty.call(target, METADATA)) { | ||
Reflect.defineProperty(target, METADATA, { | ||
value: {}, | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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.