-
Notifications
You must be signed in to change notification settings - Fork 13
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 #51 from owja/token
Add support for type-safe token based injection Published as 2.0.0-alpha.2
- Loading branch information
Showing
11 changed files
with
164 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,4 +1,7 @@ | ||
import {token} from "../../ioc/token"; | ||
import {MyOtherService} from "./my-other-service"; | ||
|
||
export const TYPE = { | ||
MyService: Symbol("MyService"), | ||
MyOtherService: Symbol("MyOtherService"), | ||
MyOtherService: token<MyOtherService>("MyOtherService"), | ||
}; |
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 |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import {Container} from "./container"; | ||
import {define} from "./define"; | ||
import {MaybeToken} from "./token"; | ||
|
||
export function createDecorator(container: Container) { | ||
return (type: symbol, ...args: symbol[]) => { | ||
return <T>(target: T, property: keyof T): void => { | ||
define(target, property, container, type, args); | ||
return <T>(token: MaybeToken<T>, ...args: MaybeToken[]) => { | ||
return <TTarget extends {[key in TProp]: T}, TProp extends string>(target: TTarget, property: TProp): void => { | ||
define(target, property, container, token, args); | ||
}; | ||
}; | ||
} |
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,32 @@ | ||
export function token<T>(name: string) { | ||
return {type: Symbol(name)} as Token<T>; | ||
} | ||
|
||
declare const typeMarker: unique symbol; | ||
|
||
export interface Token<T> { | ||
type: symbol; | ||
[typeMarker]: T; | ||
} | ||
|
||
export type MaybeToken<T = unknown> = Token<T> | symbol; | ||
|
||
function isToken<T>(token: MaybeToken<T>): token is Token<T> { | ||
return typeof token != "symbol"; | ||
} | ||
|
||
export function stringifyToken(token: MaybeToken): string { | ||
if (isToken(token)) { | ||
return `Token(${token.type.toString()})`; | ||
} else { | ||
return token.toString(); | ||
} | ||
} | ||
|
||
export function getType(token: MaybeToken): symbol { | ||
if (isToken(token)) { | ||
return token.type; | ||
} else { | ||
return token; | ||
} | ||
} |
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,14 @@ | ||
import {Container} from "./container"; | ||
import {define} from "./define"; | ||
import {MaybeToken} from "./token"; | ||
|
||
export function createWire(container: Container) { | ||
return <T>(target: T, property: keyof T, type: symbol, ...args: symbol[]) => { | ||
define(target, property, container, type, args); | ||
return <TVal, TTarget extends {[key in TProp]: TVal}, TProp extends string>( | ||
target: TTarget, | ||
property: TProp, | ||
token: MaybeToken<TVal>, | ||
...args: MaybeToken[] | ||
) => { | ||
define(target, property, container, token, args); | ||
}; | ||
} |