11import { Children , Component , PropTypes } from 'react' ;
2- import { compose , concatAll , find , map , uniq , without , withoutAll } from './utils' ;
2+ import { compose , find , map , uniqBy , without , withoutAll } from './utils' ;
33
44class InjectablesProvider extends Component {
55 static childContextTypes = {
6- produceElements : PropTypes . func . isRequired ,
7- removeProducer : PropTypes . func . isRequired ,
8- consumeElements : PropTypes . func . isRequired ,
9- stopConsumingElements : PropTypes . func . isRequired ,
6+ registerInjector : PropTypes . func . isRequired ,
7+ removeInjector : PropTypes . func . isRequired ,
8+ registerInjectable : PropTypes . func . isRequired ,
9+ removeInjectable : PropTypes . func . isRequired ,
1010 } ;
1111
1212 static propTypes = {
@@ -20,13 +20,13 @@ class InjectablesProvider extends Component {
2020
2121 getChildContext ( ) {
2222 return {
23- produceElements : ( args ) => this . produceElements ( args ) ,
23+ registerInjector : ( args ) => this . registerInjector ( args ) ,
2424
25- removeProducer : ( args ) => this . removeProducer ( args ) ,
25+ removeInjector : ( args ) => this . removeInjector ( args ) ,
2626
27- consumeElements : ( args ) => this . consumeElements ( args ) ,
27+ registerInjectable : ( args ) => this . registerInjectable ( args ) ,
2828
29- stopConsumingElements : ( args ) => this . stopConsumingElements ( args )
29+ removeInjectable : ( args ) => this . removeInjectable ( args )
3030 } ;
3131 }
3232
@@ -38,11 +38,10 @@ class InjectablesProvider extends Component {
3838 ) ( this . registrations ) ;
3939
4040 if ( ! registration ) {
41- // Need to create the registration.
4241 registration = {
4342 injectionId,
4443 injectables : [ ] ,
45- injectors : [ ]
44+ injections : [ ]
4645 } ;
4746
4847 this . registrations . push ( registration ) ;
@@ -51,15 +50,14 @@ class InjectablesProvider extends Component {
5150 return registration ;
5251 }
5352
54- notifyConsumers ( args : { registration : Object } ) {
53+ runInjections ( args : { registration : Object } ) {
5554 const { registration } = args ;
56- const { injectables, injectors } = registration ;
55+ const { injectables, injections } = registration ;
5756
5857 const elements = compose (
59- uniq ,
60- concatAll ,
61- map ( x => x . elements )
62- ) ( injectors ) ;
58+ map ( x => x . injector . getInjectElement ( ) ) ,
59+ uniqBy ( `injectorId` )
60+ ) ( injections ) ;
6361
6462 injectables . forEach ( injectable => {
6563 injectable . consume ( elements ) ;
@@ -71,56 +69,66 @@ class InjectablesProvider extends Component {
7169 this . registrations = without ( registration ) ( this . registrations ) ;
7270 }
7371
74- consumeElements ( args : { injectionId : string , injectable : Object } ) {
72+ registerInjectable ( args : { injectionId : string , injectable : Object } ) {
7573 const { injectionId, injectable } = args ;
7674 const registration = this . getRegistration ( { injectionId } ) ;
7775
7876 if ( withoutAll ( registration . injectables ) ( [ injectable ] ) . length > 0 ) {
7977 registration . injectables = [ ...registration . injectables , injectable ] ;
80- this . notifyConsumers ( { registration } ) ; // First time consumption.
78+ this . runInjections ( { registration } ) ; // First time consumption.
8179 }
8280 }
8381
84- stopConsumingElements ( args : { injectionId : string , injectable : Object } ) {
82+ removeInjectable ( args : { injectionId : string , injectable : Object } ) {
8583 const { injectionId, injectable } = args ;
8684 const registration = this . getRegistration ( { injectionId } ) ;
8785
8886 const injectables = without ( injectable ) ( registration . injectables ) ;
8987
90- if ( injectables . length === 0 && registration . injectors . length === 0 ) {
88+ if ( injectables . length === 0 && registration . injections . length === 0 ) {
9189 this . removeRegistration ( { registration } ) ;
9290 } else {
9391 registration . injectables = injectables ;
9492 }
9593 }
9694
97- findProducer ( { registration, injector } ) {
98- return find ( x => Object . is ( x . injector , injector ) ) ( registration . injectors ) ;
95+ findInjection ( { registration, injector } ) {
96+ return find ( x => Object . is ( x . injector , injector ) ) ( registration . injections ) ;
9997 }
10098
101- produceElements ( args : { injectionId : string , injector : Object , elements : Array < Object > } ) {
102- const { injectionId, injector , elements } = args ;
99+ registerInjector ( args : { injectionId : string , injectorId : string , injector : Object } ) {
100+ const { injectionId, injectorId , injector } = args ;
103101 const registration = this . getRegistration ( { injectionId } ) ;
104- const existingProducer = this . findProducer ( { registration, injector } ) ;
102+ const existingInjection = this . findInjection ( { registration, injector } ) ;
105103
106- if ( existingProducer ) {
104+ if ( existingInjection ) {
107105 return ;
108106 }
109107
110- const newInjector = { injector, elements } ;
111- registration . injectors = [
112- ...registration . injectors ,
113- newInjector
108+ const newInjection = { injector, injectorId } ;
109+ registration . injections = [
110+ ...registration . injections ,
111+ newInjection
114112 ] ;
115- this . notifyConsumers ( { registration } ) ;
113+
114+ this . runInjections ( { registration } ) ;
116115 }
117116
118- removeProducer ( args : { injectionId : string , injector : Object } ) {
117+ removeInjector ( args : { injectionId : string , injector : Object } ) {
119118 const { injectionId, injector } = args ;
120119 const registration = this . getRegistration ( { injectionId } ) ;
121- const existingInjector = this . findProducer ( { registration, injector } ) ;
122- registration . injectors = without ( existingInjector ) ( registration . injectors ) ;
123- this . notifyConsumers ( { registration } ) ;
120+ const injection = this . findInjection ( { registration, injector } ) ;
121+
122+ if ( injection ) {
123+ registration . injections = without ( injection ) ( registration . injections ) ;
124+ this . runInjections ( { registration } ) ;
125+ } else {
126+ /* istanbul ignore next */
127+ if ( process . env . NODE_ENV === `development` ) {
128+ throw new Error (
129+ `Trying to remove an injector which has not been registered` ) ;
130+ }
131+ }
124132 }
125133
126134 render ( ) {
0 commit comments