|
| 1 | +import { ReflectiveInjector } from 'injection-js'; |
1 | 2 | import React from 'react';
|
2 | 3 |
|
3 | 4 | import { CounterSpeed } from './components/Counter';
|
| 5 | +import { TodoApp } from './components/Todo'; |
| 6 | +import { TodoService } from './services/TodoService'; |
4 | 7 |
|
5 |
| -import { StatedBeanApplication, StatedBeanProvider } from 'stated-bean'; |
| 8 | +import { StatedBeanApplication, StatedBeanProvider, IBeanFactory, BeanDefinition } from 'stated-bean'; |
6 | 9 |
|
7 | 10 | const app = new StatedBeanApplication();
|
8 | 11 |
|
| 12 | +class InjectionFactory implements IBeanFactory { |
| 13 | + rootInjector = ReflectiveInjector.resolveAndCreate([TodoService]); |
| 14 | + |
| 15 | + createBean<T>(beanDefinition: BeanDefinition<T>) { |
| 16 | + let provide; |
| 17 | + let provider; |
| 18 | + |
| 19 | + if (beanDefinition.isFactoryBean) { |
| 20 | + provide = beanDefinition.factoryBeanType; |
| 21 | + provider = { provide: provide, useFactory: beanDefinition.getFactory()! }; |
| 22 | + } else { |
| 23 | + provide = beanDefinition.beanType; |
| 24 | + provider = { provide: provide, useClass: beanDefinition.beanType }; |
| 25 | + } |
| 26 | + const injector = ReflectiveInjector.resolveAndCreate([provider], this.rootInjector); |
| 27 | + |
| 28 | + return injector.get(provide); |
| 29 | + } |
| 30 | + |
| 31 | + destroyBean<T>(beanDefinition: BeanDefinition<T>) { |
| 32 | + console.info('destroyed', beanDefinition); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +app.setBeanFactory(new InjectionFactory()); |
| 37 | + |
9 | 38 | export const DevApp = () => {
|
10 | 39 | return (
|
11 | 40 | <StatedBeanProvider application={app}>
|
12 | 41 | <CounterSpeed />
|
| 42 | + |
| 43 | + <TodoApp /> |
13 | 44 | </StatedBeanProvider>
|
14 | 45 | );
|
15 | 46 | };
|
0 commit comments