-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
26 changed files
with
615 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
dist | ||
coverage |
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,6 +1,7 @@ | ||
{ | ||
"cSpell.words": [ | ||
"antfu", | ||
"Autowired", | ||
"bumpp", | ||
"Containerable", | ||
"Naily", | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { InjectionToken } from '../types' | ||
import { Container } from '../container' | ||
|
||
export function Constant<Value>(token: InjectionToken, value: Value): PropertyDecorator & ClassDecorator & MethodDecorator { | ||
return (() => { | ||
new Container().createConstantWrapper(token, value).save() | ||
}) as ClassDecorator & PropertyDecorator | ||
} |
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,4 @@ | ||
export * from './constant.decorator' | ||
export * from './inject.decorator' | ||
export * from './injectable.decorator' | ||
export * from './post-construct.decorator' |
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,27 +1,43 @@ | ||
import type { InjectOptions } from '../types' | ||
import type { InjectionToken, InjectOptions } from '../types' | ||
import { InjectWatermark } from '../constant' | ||
|
||
export function Inject(options: Partial<Omit<InjectOptions, 'parameterIndex' | 'propertyKey'>> = {}): ParameterDecorator & PropertyDecorator { | ||
return ((target, propertyKey, parameterIndex) => { | ||
Reflect.defineMetadata(InjectWatermark, [ | ||
...(Reflect.getMetadata(InjectWatermark, typeof parameterIndex === 'number' ? target : target.constructor) || []), | ||
const oldMetadata: InjectOptions[] = Reflect.getMetadata(InjectWatermark, typeof parameterIndex === 'number' ? target : target.constructor) || [] | ||
const equal = (injectOptions: InjectOptions): boolean => injectOptions.parameterIndex === parameterIndex && injectOptions.propertyKey === propertyKey | ||
const injectOptions = oldMetadata.find(equal) | ||
if (!injectOptions) return Reflect.defineMetadata(InjectWatermark, [ | ||
...oldMetadata, | ||
{ | ||
optional: false, | ||
...options, | ||
parameterIndex: typeof parameterIndex === 'number' ? parameterIndex : undefined, | ||
propertyKey: typeof parameterIndex === 'number' ? undefined : propertyKey, | ||
parameterIndex, | ||
propertyKey, | ||
}, | ||
] as InjectOptions[], typeof parameterIndex === 'number' ? target.constructor : target) | ||
] as InjectOptions[], typeof parameterIndex === 'number' ? target : target.constructor) | ||
|
||
Reflect.defineMetadata(InjectWatermark, oldMetadata.map((injectOptions) => { | ||
return equal(injectOptions) | ||
? { | ||
...injectOptions, | ||
...options, | ||
} | ||
: injectOptions | ||
}), typeof parameterIndex === 'number' ? target : target.constructor) | ||
}) as ParameterDecorator & PropertyDecorator | ||
} | ||
|
||
export function Autowired(injectionToken?: InjectionToken, options: Partial<Omit<InjectOptions, 'parameterIndex' | 'propertyKey' | 'injectionToken'>> = {}): ParameterDecorator & PropertyDecorator { | ||
return Inject({ ...options, injectionToken }) | ||
} | ||
|
||
export function Optional(): ParameterDecorator & PropertyDecorator { | ||
return ((target, propertyKey, parameterIndex) => { | ||
const metadata: InjectOptions[] = Reflect.getMetadata(InjectWatermark, typeof parameterIndex === 'number' ? target : target.constructor) || [] | ||
const injectOptions = metadata.find((injectOptions) => { | ||
if (typeof parameterIndex === 'number') return injectOptions.parameterIndex === parameterIndex && injectOptions.propertyKey === propertyKey | ||
return injectOptions.propertyKey === propertyKey | ||
}) | ||
if (!injectOptions) return Inject({ optional: true })(target, propertyKey, parameterIndex) | ||
else return Inject({ ...injectOptions, optional: true })(target, propertyKey, parameterIndex) | ||
const injectOptions = metadata.find(injectOptions => | ||
(injectOptions.parameterIndex === parameterIndex) && (injectOptions.propertyKey === propertyKey), | ||
) | ||
|
||
return Inject({ ...(injectOptions || {}), optional: true })(target, propertyKey, parameterIndex) | ||
}) as ParameterDecorator & PropertyDecorator | ||
} |
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,8 +1,10 @@ | ||
import type { InjectableOptions } from '../types' | ||
import { InjectWatermark } from '../constant' | ||
import type { Class, InjectableOptions } from '../types' | ||
import { InjectableWatermark } from '../constant' | ||
import { Container } from '../container' | ||
|
||
export function Injectable(options: Partial<InjectableOptions> = {}): ClassDecorator { | ||
return ((target) => { | ||
Reflect.defineMetadata(InjectWatermark, options || {}, target) | ||
return ((target: Class) => { | ||
Reflect.defineMetadata(InjectableWatermark, options || {}, target) | ||
new Container().createClassWrapper(target).save() | ||
}) as ClassDecorator | ||
} |
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,14 @@ | ||
import type { CallType, PostConstructMetadata } from '../types' | ||
import { PostConstructWatermark } from '../constant' | ||
|
||
export function PostConstruct(callType: CallType = 'parallel'): MethodDecorator { | ||
return ((target: Object, propertyKey: string | symbol) => { | ||
Reflect.defineMetadata(PostConstructWatermark, [ | ||
...(Reflect.getMetadata(PostConstructWatermark, target) || []), | ||
{ | ||
propertyKey, | ||
callType, | ||
}, | ||
] as PostConstructMetadata[], target.constructor) | ||
}) as MethodDecorator | ||
} |
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,25 @@ | ||
export class TaskRunner { | ||
async runTasksSequentially(tasks: Array<() => any>): Promise<void> { | ||
for (const task of tasks) { | ||
try { | ||
await task() | ||
} | ||
catch (error) { | ||
return error as any | ||
} | ||
} | ||
} | ||
|
||
async runTasksInParallel(tasks: Array<() => any>): Promise<void> { | ||
const taskPromises = tasks.map(async (task) => { | ||
try { | ||
await task() | ||
} | ||
catch (error) { | ||
return error | ||
} | ||
}) | ||
|
||
await Promise.allSettled(taskPromises) | ||
} | ||
} |
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.