@@ -5,6 +5,15 @@ import ChannelStore from './ChannelStore';
55const getDisplayName = WrappedComponent =>
66 WrappedComponent . displayName || WrappedComponent . name || 'Component' ;
77
8+ const tryToSelect = fn => store => {
9+ try {
10+ return fn ( store ) ;
11+ } catch ( err ) {
12+ console . warn ( err ) ;
13+ return undefined ;
14+ }
15+ } ;
16+
817const withChannel = ( {
918 EVENT_ID_INIT ,
1019 EVENT_ID_DATA ,
@@ -14,30 +23,48 @@ const withChannel = ({
1423 panel,
1524 parameters,
1625 storyId,
26+ storeSelectors = { } ,
1727 createActions = { } ,
1828} ) => WrappedComponent =>
1929 class extends React . Component {
2030 static displayName = `WithChannel(${ getDisplayName ( WrappedComponent ) } )` ;
2131
22- state = {
23- data : {
32+ constructor ( props , ...args ) {
33+ super ( props , ...args ) ;
34+ const initStateData = {
2435 ...initData ,
25- ...this . props . initData ,
36+ ...props . initData ,
2637 ...parameters ,
27- } ,
28- isReceived : false ,
29- } ;
38+ } ;
39+
40+ this . state = {
41+ data : initStateData ,
42+ selectors : this . prepareSelectors ( initStateData ) ,
43+ isReceived : false ,
44+ } ;
45+
46+ this . store = new ChannelStore ( {
47+ EVENT_ID_INIT ,
48+ EVENT_ID_DATA ,
49+ EVENT_ID_BACK ,
50+ name : props . pointName ,
51+ initData : this . state . data ,
52+ isPanel : this . isPanel ,
53+ storyId,
54+ } ) ;
55+
56+ this . actions = this . prepareActions ( ) ;
57+ }
3058
3159 isPanel = this . props . panel || panel ;
32- store = new ChannelStore ( {
33- EVENT_ID_INIT ,
34- EVENT_ID_DATA ,
35- EVENT_ID_BACK ,
36- name : this . props . pointName ,
37- initData : this . state . data ,
38- isPanel : this . isPanel ,
39- storyId,
40- } ) ;
60+
61+ prepareSelectors = store => {
62+ return Object . entries ( storeSelectors )
63+ . map ( ( [ name , selector ] ) => ( {
64+ [ name ] : tryToSelect ( selector ) ( store ) ,
65+ } ) )
66+ . reduce ( ( akk , cur ) => ( { ...akk , ...cur } ) , { } ) ;
67+ } ;
4168
4269 prepareActions = ( ) => {
4370 const {
@@ -52,7 +79,6 @@ const withChannel = ({
5279 . reduce ( ( acc , cur ) => ( { ...acc , ...cur } ) , { } ) ;
5380 return actions ;
5481 } ;
55- actions = this . prepareActions ( ) ;
5682
5783 componentDidMount ( ) {
5884 this . debugLog ( 'componentDidMount' ) ;
@@ -83,12 +109,16 @@ const withChannel = ({
83109 } ;
84110
85111 onData = data => {
86- this . setState ( { data, isReceived : true } ) ;
112+ this . setState ( {
113+ data,
114+ isReceived : true ,
115+ selectors : this . prepareSelectors ( data ) ,
116+ } ) ;
87117 } ;
88118
89119 render ( ) {
90120 const { pointName, initData, active, onData, ...props } = this . props ;
91- const { data, isReceived } = this . state ;
121+ const { data, isReceived, selectors } = this . state ;
92122
93123 if ( active === false ) return null ;
94124 if ( ! isReceived ) return null ;
@@ -100,6 +130,7 @@ const withChannel = ({
100130 store = { this . store }
101131 active = { active }
102132 parameters = { parameters }
133+ selectors = { selectors }
103134 actions = { this . actions }
104135 { ...props }
105136 />
0 commit comments