Skip to content

Commit 389e146

Browse files
committed
Fix store update on story changes
1 parent 24e90c9 commit 389e146

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

src/ChannelStore.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ export default class ChannelStore {
6666
const selectorId = id || GLOBAL;
6767
const selectedData = { ...(this.store[selectorId] || {}) };
6868
selectedData.init = data;
69-
selectedData.over = selectedData.over || {};
69+
/**
70+
* Previous behavior didn't reset state on init event
71+
* it caused that we didn't see changes after
72+
* updating story parameters
73+
* So i'm removing this, but if we need to make it optional
74+
* this is how to revert it:
75+
* electedData.over = selectedData.over || {};
76+
*/
77+
selectedData.over = {};
7078
this.store[selectorId] = selectedData;
7179
this.selectorId = selectorId;
7280
this.subscriber();

src/decorator.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import withChannel from './withChannel';
33

44
import { getConfig } from './config';
55

6-
const DecoratorHOC = ({ actions, selectors, Component, ...props }) => (
7-
<Component {...actions} {...selectors} {...props} />
8-
);
6+
const DecoratorHOC = ({ actions, selectors, Component, parameters, resetParameters, ...props }) => {
7+
return <Component {...actions} {...selectors} {...props} />
8+
};
99

1010
export const createDecorator = (storeSelectors, createActions) => (
1111
Component,

src/withChannel.js

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const withChannel = ({
4141

4242
this.state = {
4343
data: initStateData,
44-
selectors: isReceived ? this.prepareSelectors(initStateData) : {},
44+
selectors: isReceived ? this.executeSelectors(initStateData) : {},
4545
isReceived,
4646
};
4747

@@ -60,7 +60,7 @@ const withChannel = ({
6060

6161
isPanel = this.props.panel || panel;
6262

63-
prepareSelectors = store => {
63+
executeSelectors = store => {
6464
return Object.entries(storeSelectors)
6565
.map(([name, selector]) => ({
6666
[name]: tryToSelect(selector)(store),
@@ -93,14 +93,14 @@ const withChannel = ({
9393

9494
componentWillUnmount() {
9595
this.debugLog('componentWillUnmount');
96-
this.store.disconnect();
97-
}
96+
this.store.disconnect();
97+
}
9898

99-
// debug = true;
100-
debug = false;
99+
// debug = true;
100+
debug = false;
101101

102-
debugLog = message => {
103-
if (!this.debug) {
102+
debugLog = message => {
103+
if (!this.debug) {
104104
return;
105105
}
106106
console.log(
@@ -114,16 +114,42 @@ const withChannel = ({
114114
this.setState({
115115
data,
116116
isReceived: true,
117-
selectors: this.prepareSelectors(data),
117+
selectors: this.executeSelectors(data),
118118
});
119119
};
120120

121+
resetParameters = parameters => {
122+
const initStateData = {
123+
...initData,
124+
...this.props.initData,
125+
...parameters,
126+
};
127+
this.setState({
128+
data: initStateData,
129+
selectors: this.state.isReceived ? this.executeSelectors(initStateData) : {},
130+
});
131+
this.store.sendInit(initStateData);
132+
}
133+
121134
render() {
122135
const { pointName, initData, active, onData, ...props } = this.props;
123-
const { data, isReceived, selectors } = this.state;
136+
const { data, isReceived } = this.state;
124137

125138
if (active === false) return null;
126139

140+
const initStateData = {
141+
...initData,
142+
...parameters,
143+
...data,
144+
};
145+
146+
let selectors;
147+
try {
148+
selectors = this.executeSelectors(initStateData)
149+
} catch (err) {
150+
selectors = this.state.selectors;
151+
}
152+
127153
return (
128154
<WrappedComponent
129155
data={data}
@@ -134,6 +160,7 @@ const withChannel = ({
134160
selectors={selectors}
135161
actions={this.actions}
136162
isFirstDataReceived={isReceived}
163+
resetParameters={this.resetParameters}
137164
{...props}
138165
/>
139166
);

0 commit comments

Comments
 (0)