11import addons from '@storybook/addons' ;
22
3+ const GLOBAL = 'global' ;
4+
35export default class ChannelStore {
46 constructor ( {
57 EVENT_ID_INIT ,
@@ -9,24 +11,28 @@ export default class ChannelStore {
911 name = 'store' ,
1012 initData = { } ,
1113 isPanel = false ,
14+ storyId,
1215 } ) {
1316 this . EVENT_ID_INIT = EVENT_ID_INIT ;
1417 this . EVENT_ID_DATA = EVENT_ID_DATA ;
1518 this . EVENT_ID_BACK = EVENT_ID_BACK ;
1619 this . name = name ;
1720 this . initData = initData ;
1821 this . isPanel = isPanel ;
22+ this . id = storyId ;
1923
20-
21-
22- console . log ( `New Store Created for ${ isPanel ? 'Panel' : 'Preview' } ` ,
23- EVENT_ID_INIT ,
24- EVENT_ID_DATA ,
25- EVENT_ID_BACK ,
24+ console . log (
25+ `New Store Created for ${ isPanel ? 'Panel' : 'Preview' } ` ,
26+ EVENT_ID_INIT ,
27+ EVENT_ID_DATA ,
28+ EVENT_ID_BACK
2629 ) ;
2730 }
2831
29- store = this . initData ;
32+ store = {
33+ [ GLOBAL ] : { init : this . initData || { } , over : { } } ,
34+ } ;
35+ selectorId = null ;
3036
3137 subscriber = ( ) => { } ;
3238 onConnectedFn = ( ) => { } ;
@@ -44,12 +50,12 @@ export default class ChannelStore {
4450 } ;
4551
4652 emit = data =>
47- this . channel . emit (
48- this . isPanel ? this . EVENT_ID_BACK : this . EVENT_ID_DATA ,
49- data
50- ) ;
53+ this . channel . emit ( this . isPanel ? this . EVENT_ID_BACK : this . EVENT_ID_DATA , {
54+ data ,
55+ id : this . id ,
56+ } ) ;
5157
52- init = data => this . channel . emit ( this . EVENT_ID_INIT , data ) ;
58+ init = data => this . channel . emit ( this . EVENT_ID_INIT , { data, id : this . id } ) ;
5359
5460 removeInit = ( ) =>
5561 this . channel . removeListener ( this . EVENT_ID_INIT , this . onInitChannel ) ;
@@ -61,41 +67,78 @@ export default class ChannelStore {
6167 ) ;
6268
6369 onInitChannel = initData => {
64- console . log (
65- `Channel Store (${
66- this . isPanel ? 'Panel' : 'Decorator'
67- } ) Received Init Data`,
68- initData
69- ) ;
70- this . store = initData ;
71- this . subscriber ( this . store ) ;
70+ const { data, id } = initData ;
71+ const selectorId = id || GLOBAL ;
72+ const selectedData = this . store [ selectorId ] ;
73+ selectedData . init = data ;
74+ selectedData . over = selectedData . over || { } ;
75+ this . selectorId = selectorId ;
76+ this . subscriber ( ) ;
7277 } ;
7378
7479 onDataChannel = updData => {
75- this . store = {
76- ...this . store ,
77- ...updData ,
80+ const { data, id } = updData ;
81+ if ( this . isPanel ) {
82+ const selectorId = id || GLOBAL ;
83+ const selectedData = this . store [ selectorId ] ;
84+ selectedData . over = data ;
85+ this . selectorId = selectorId ;
86+ } else {
87+ this . store = updData ;
88+ }
89+
90+ this . subscriber ( ) ;
91+ } ;
92+
93+ selectData = ( ) => {
94+ const id = this . isPanel ? this . selectorId : this . id ;
95+
96+ const { global } = this . store ;
97+ const local = this . store [ id ] ;
98+
99+ const finalData = {
100+ ...global . init ,
101+ ...local . init ,
102+ ...global . over ,
103+ ...local . over ,
78104 } ;
79- this . subscriber ( this . store ) ;
105+
106+ return finalData ;
80107 } ;
81108
82109 onData = subscriberFn => {
83- this . subscriber = subscriberFn ;
110+ this . subscriber = ( ) => {
111+ const data = this . selectData ( ) ;
112+ subscriberFn ( data ) ;
113+ } ;
84114 } ;
85115
86116 onConnected = onConnectedFn => {
87117 this . onConnectedFn = onConnectedFn ;
88118 } ;
89119
90- send = data => {
91- this . store = {
92- ...this . store ,
93- ...data ,
94- } ;
120+ send = ( ) => {
95121 this . emit ( this . store ) ;
96- this . subscriber ( this . store ) ;
97122 } ;
98123
124+ defaultReducer = ( store , payload ) => ( {
125+ ...store ,
126+ ...payload ,
127+ } ) ;
128+
129+ _createAction = ( reducer = this . defaultReducer , subId ) => {
130+ return payload => {
131+ const subData = this . store [ subId ] ;
132+ subData . over = reducer ( global . over , payload ) ;
133+ this . send ( ) ;
134+ this . subscriber ( ) ;
135+ } ;
136+ } ;
137+
138+ createGlobalAction = reducer => this . _createAction ( reducer , GLOBAL ) ;
139+ createLocalAction = reducer =>
140+ this . _createAction ( reducer , this . selectedId || this . id ) ;
141+
99142 sendInit = data => {
100143 this . init ( data ) ;
101144 } ;
0 commit comments