-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathChannelStore.js
More file actions
204 lines (171 loc) · 4.77 KB
/
ChannelStore.js
File metadata and controls
204 lines (171 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { addons } from '@storybook/addons';
import deepEqual from 'deep-equal';
const GLOBAL = 'global';
export default class ChannelStore {
constructor({
EVENT_ID_INIT,
EVENT_ID_DATA,
EVENT_ID_BACK,
name = 'store',
initData = {},
isPanel = false,
storyId,
}) {
this.EVENT_ID_INIT = EVENT_ID_INIT;
this.EVENT_ID_DATA = EVENT_ID_DATA;
this.EVENT_ID_BACK = EVENT_ID_BACK;
this.name = name;
this.initData = initData;
this.isPanel = isPanel;
this.id = storyId;
this.store = {
[GLOBAL]: { init: this.initData || {}, over: {} },
};
}
selectorId = null;
subscriber = () => {};
onConnectedFn = () => {};
channel = addons.getChannel();
connect = () => {
if (this.isPanel) {
this.channel.on(this.EVENT_ID_INIT, this.onInitChannel);
this.channel.on(this.EVENT_ID_DATA, this.onDataChannel);
} else {
this.channel.on(this.EVENT_ID_BACK, this.onDataChannel);
}
this.onConnectedFn();
};
emit = data =>
this.channel.emit(this.isPanel ? this.EVENT_ID_BACK : this.EVENT_ID_DATA, {
data,
id: this.id,
});
init = data => this.channel.emit(this.EVENT_ID_INIT, { data, id: this.id });
removeInit = () =>
this.channel.removeListener(this.EVENT_ID_INIT, this.onInitChannel);
removeData = () =>
this.channel.removeListener(
this.isPanel ? this.EVENT_ID_DATA : this.EVENT_ID_BACK,
this.onDataChannel
);
onInitChannel = initData => {
const { data, id } = initData;
const selectorId = id || GLOBAL;
const selectedData = { ...(this.store[selectorId] || {}) };
/**
* Previous behavior didn't reset state on init event
* it caused that we didn't see changes after
* updating story parameters
* So i'm removing this, but if we need to make it optional
* this is how to revert it:
* selectedData.over = selectedData.over || {};
*
* Update:
* Now we check if coming initial data the same as we already have in the store
* this allow us to not reset changes while switching stories
*
* it works if stories don't contain parameters or changing data any other way
*
* Additional it's better if actions don't return whole store
* compare:
*
* // right way:
* store => ({
* currentTheme: store.currentTheme + 1,
* })
*
* vs
*
* // wrong way:
* store => ({
* ...store, // this cause an overriding of whole store
* currentTheme: store.currentTheme + 1,
* })
*
* the better solution would be to granularly commit updates and store only changed values
*
*/
if (deepEqual(selectedData.init, data)) {
selectedData.over = selectedData.over || {};
} else {
selectedData.init = data;
selectedData.over = {};
}
this.store[selectorId] = selectedData;
this.selectorId = selectorId;
this.subscriber();
this.send();
};
onDataChannel = updData => {
const { data, id } = updData;
if (this.isPanel) {
const selectorId = id || GLOBAL;
const selectedData = this.store[selectorId];
selectedData.over = data;
this.selectorId = selectorId;
} else {
this.store = data;
}
this.subscriber();
};
selectData = () => {
const id = this.isPanel ? this.selectorId : this.id;
const { global = {} } = this.store;
const local = this.store[id] || {};
const finalData = {
...global.init,
...local.init,
...global.over,
...local.over,
};
return finalData;
};
onData = subscriberFn => {
this.subscriber = () => {
const data = this.selectData();
subscriberFn(data);
};
};
onConnected = onConnectedFn => {
this.onConnectedFn = onConnectedFn;
};
send = () => {
this.emit(this.store);
};
defaultReducer = (store, payload) => ({
...store,
...payload,
});
_createAction = (reducer, getSubId) => {
return async payload => {
const subId = getSubId();
const subData = this.store[subId];
const current = {
...subData.init,
...subData.over,
};
const over = await (reducer || this.defaultReducer)(current, payload);
subData.over = over;
this.send();
this.subscriber();
};
};
createGlobalAction = reducer => this._createAction(reducer, () => GLOBAL);
createLocalAction = reducer =>
this._createAction(reducer, () => this.selectorId || this.id);
sendInit = data => {
this.init(data);
};
disconnect = () => {
this.removeInit();
this.removeData();
};
}
let singleStore;
export const getSingleStore = (...args) => {
singleStore = singleStore || new ChannelStore(...args);
return singleStore;
};
export const getNewStore = (...args) => {
return new ChannelStore(...args);
};