1
+ /* eslint-disable no-param-reassign */
2
+ import produce from 'immer' ;
1
3
import * as types from '../constants/actionTypes' ;
2
4
3
- export default function mainReducer ( state , action ) {
5
+ export default ( state , action ) => produce ( state , ( draft ) => {
4
6
const {
5
7
port, currentTab, tabs,
6
- } = state ;
8
+ } = draft ;
7
9
const {
8
10
snapshots, mode, intervalId, viewIndex, sliderIndex,
9
11
} = ( tabs [ currentTab ] || { } ) ;
@@ -18,13 +20,8 @@ export default function mainReducer(state, action) {
18
20
19
21
tabs [ currentTab ] . sliderIndex = newIndex ;
20
22
tabs [ currentTab ] . playing = false ;
21
-
22
- return {
23
- ...state ,
24
- tabs,
25
- } ;
26
23
}
27
- return state ;
24
+ break ;
28
25
}
29
26
case types . MOVE_FORWARD : {
30
27
if ( sliderIndex < snapshots . length - 1 ) {
@@ -39,50 +36,36 @@ export default function mainReducer(state, action) {
39
36
clearInterval ( intervalId ) ;
40
37
tabs [ currentTab ] . playing = false ;
41
38
}
42
- // message is coming from the setInterval
43
- return {
44
- ...state ,
45
- tabs,
46
- } ;
47
39
}
48
- return state ;
40
+ break ;
49
41
}
50
42
case types . CHANGE_VIEW : {
51
43
// unselect view if same index was selected
52
44
if ( viewIndex === action . payload ) tabs [ currentTab ] . viewIndex = - 1 ;
53
45
else tabs [ currentTab ] . viewIndex = action . payload ;
54
-
55
- return {
56
- ...state ,
57
- tabs,
58
- } ;
46
+ break ;
59
47
}
60
48
case types . CHANGE_SLIDER : {
61
49
port . postMessage ( { action : 'jumpToSnap' , payload : snapshots [ action . payload ] , tabId : currentTab } ) ;
62
50
tabs [ currentTab ] . sliderIndex = action . payload ;
63
- return { ... state , tabs } ;
51
+ break ;
64
52
}
65
53
case types . EMPTY : {
66
54
port . postMessage ( { action : 'emptySnap' , tabId : currentTab } ) ;
67
55
tabs [ currentTab ] . sliderIndex = 0 ;
68
56
tabs [ currentTab ] . viewIndex = - 1 ;
69
57
tabs [ currentTab ] . playing = false ;
70
58
tabs [ currentTab ] . snapshots . splice ( 1 ) ;
71
- return {
72
- ...state ,
73
- tabs,
74
- } ;
59
+ break ;
75
60
}
76
61
case types . SET_PORT : {
77
- return { ...state , port : action . payload } ;
62
+ draft . port = action . payload ;
63
+ break ;
78
64
}
79
65
case types . IMPORT : {
80
66
port . postMessage ( { action : 'import' , payload : action . payload , tabId : currentTab } ) ;
81
67
tabs [ currentTab ] . snapshots = action . payload ;
82
- return {
83
- ...state ,
84
- tabs,
85
- } ;
68
+ break ;
86
69
}
87
70
case types . TOGGLE_MODE : {
88
71
mode [ action . payload ] = ! mode [ action . payload ] ;
@@ -101,66 +84,60 @@ export default function mainReducer(state, action) {
101
84
default :
102
85
}
103
86
port . postMessage ( { action : actionText , payload : newMode , tabId : currentTab } ) ;
104
- return { ... state , tabs } ;
87
+ break ;
105
88
}
106
89
case types . PAUSE : {
107
90
clearInterval ( intervalId ) ;
108
91
tabs [ currentTab ] . playing = false ;
109
92
tabs [ currentTab ] . intervalId = null ;
110
- return { ... state , tabs } ;
93
+ break ;
111
94
}
112
95
case types . PLAY : {
113
96
tabs [ currentTab ] . playing = true ;
114
97
tabs [ currentTab ] . intervalId = action . payload ;
115
- return {
116
- ...state ,
117
- tabs,
118
- } ;
98
+ break ;
119
99
}
120
100
case types . INITIAL_CONNECT : {
121
101
const { payload } = action ;
122
102
Object . keys ( payload ) . forEach ( ( tab ) => {
123
- payload [ tab ] = {
124
- ...payload [ tab ] ,
125
- sliderIndex : 0 ,
126
- viewIndex : - 1 ,
127
- intervalId : null ,
128
- playing : false ,
129
- } ;
103
+ // check if tab exists in memory
104
+ if ( ! tabs [ tab ] ) {
105
+ // add new tab
106
+ tabs [ tab ] = {
107
+ ...payload [ tab ] ,
108
+ sliderIndex : 0 ,
109
+ viewIndex : - 1 ,
110
+ intervalId : null ,
111
+ playing : false ,
112
+ } ;
113
+ }
130
114
} ) ;
131
115
132
116
// only set first tab if current tab is non existent
133
117
const firstTab = parseInt ( Object . keys ( payload ) [ 0 ] , 10 ) ;
134
- return {
135
- ...state ,
136
- currentTab : ( currentTab === null ) ? firstTab : currentTab ,
137
- tabs : payload ,
138
- } ;
118
+ draft . currentTab = ( currentTab === null ) ? firstTab : currentTab ;
119
+
120
+ break ;
139
121
}
140
122
case types . NEW_SNAPSHOTS : {
141
123
const { payload } = action ;
142
124
143
125
Object . keys ( payload ) . forEach ( ( tab ) => {
144
126
const { snapshots : newSnaps } = payload [ tab ] ;
145
- payload [ tab ] = {
127
+ tabs [ tab ] = {
146
128
...tabs [ tab ] ,
147
129
...payload [ tab ] ,
148
130
sliderIndex : newSnaps . length - 1 ,
149
131
} ;
150
132
} ) ;
151
133
152
- return {
153
- ...state ,
154
- tabs : payload ,
155
- } ;
134
+ break ;
156
135
}
157
136
case types . SET_TAB : {
158
- return {
159
- ...state ,
160
- currentTab : action . payload ,
161
- } ;
137
+ draft . currentTab = action . paylod ;
138
+ break ;
162
139
}
163
140
default :
164
141
throw new Error ( `nonexistent action: ${ action . type } ` ) ;
165
142
}
166
- }
143
+ } ) ;
0 commit comments