-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
bbf4007
commit 990339b
Showing
6 changed files
with
232 additions
and
26 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,12 @@ | ||
import {observable, action} from "mobx"; | ||
import { provide } from './IoC'; | ||
|
||
@provide.singleton() | ||
export class CounterStore { | ||
@observable counter: number = 0; | ||
|
||
@action.bound | ||
increment() { | ||
this.counter++; | ||
} | ||
} |
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,62 @@ | ||
import 'reflect-metadata'; | ||
import { Container, ContainerModule, interfaces } from 'inversify'; | ||
import { fluentProvide } from 'inversify-binding-decorators'; | ||
import { RouterStore } from 'mobx-react-router'; | ||
|
||
const container = new Container({ | ||
autoBindInjectable: true, | ||
defaultScope: 'Singleton', | ||
}); | ||
|
||
// You can add here external services to container | ||
container.bind(RouterStore).toConstantValue(new RouterStore()); | ||
|
||
const provide = { | ||
singleton: () => (target: any) => | ||
fluentProvide(target) | ||
.inSingletonScope() | ||
.done()(target), | ||
|
||
transient: () => (target: any) => | ||
fluentProvide(target) | ||
.inTransientScope() | ||
.done()(target), | ||
}; | ||
|
||
interface IProvideSyntax { | ||
constraint: (bind: interfaces.Bind, target: any) => any; | ||
implementationType: any; | ||
} | ||
|
||
const PROVIDE_METADATA_KEY = 'inversify-binding-decorators:provide'; | ||
|
||
const lazyInject = (identifier: any) => (target: any, key: string) => { | ||
debugger; | ||
const isBound = container.isBound(identifier); | ||
if (!isBound) { | ||
const provideMetadata = ( | ||
Reflect.getMetadata(PROVIDE_METADATA_KEY, Reflect) || [] | ||
).filter( | ||
(metadata: IProvideSyntax) => metadata.implementationType === identifier | ||
); | ||
|
||
if (provideMetadata.length === 0) { | ||
throw new Error(`Provided identifier isn't registered: ${identifier}`); | ||
} | ||
|
||
container.load( | ||
new ContainerModule((bind: any) => { | ||
provideMetadata.forEach((metadata: IProvideSyntax) => | ||
metadata.constraint(bind, metadata.implementationType) | ||
); | ||
}) | ||
); | ||
} | ||
|
||
Object.defineProperty(target, key, { | ||
get: () => container.get(identifier), | ||
enumerable: true, | ||
}); | ||
}; | ||
|
||
export { container, provide, lazyInject }; |
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