Skip to content

Commit 1b7c783

Browse files
committed
Fix local storage mutations
1 parent 3824cff commit 1b7c783

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

dev/register.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@ const AddonPanel = ({
1717
rect,
1818
Layout,
1919
Block,
20-
selectInd
20+
nextInd,
21+
update,
2122
}) => {
2223
return (
2324
<Layout style={{ padding: 0 }}>
2425
<Block style={{ ...blockStyle, minWidth: 50 }} size={200}>
2526
kind: {kind}
2627
<br />
27-
<button onClick={() => selectInd(3)}>setData</button>
28+
<button onClick={() => nextInd()}>setData</button>
29+
<br />
30+
<button onClick={() => update({ themes: ['T1', 'T2', 'T3'] })}>
31+
Update
32+
</button>
2833
</Block>
2934
{/* <Block style={blockStyle}>
3035
<small>{JSON.stringify(api.getCurrentStoryData())}</small>
@@ -38,5 +43,13 @@ const AddonPanel = ({
3843
};
3944

4045
register(({ global, local }) => ({
41-
selectInd: global((store, ind) => ({ store, currentTheme: ind })),
46+
nextInd: local((store) => ({
47+
currentTheme: store.currentTheme + 1,
48+
})),
49+
update: global(),
4250
}))(AddonPanel);
51+
52+
// register({
53+
// nextInd: (store) => ({ ...store, currentTheme: store.currentTheme + 1 }),
54+
// update: null, // <-- it works :)
55+
// })(AddonPanel);

dev/withAdk.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ const DecoratorUI = ({ context, getStory, data, parameters }) => (
1717
</div>
1818
);
1919

20-
export const withAdk = createDecorator(DecoratorUI, {isGlobal: true});
20+
export const withAdk = createDecorator(DecoratorUI, {isGlobal: false});
2121
export const adkParams = setParameters()

src/ChannelStore.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ export default class ChannelStore {
6262
onInitChannel = initData => {
6363
const { data, id } = initData;
6464
const selectorId = id || GLOBAL;
65-
const selectedData = this.store[selectorId];
65+
console.log('TCL: ChannelStore -> onConnectedFn -> selectorId', selectorId);
66+
const selectedData = { ...(this.store[selectorId] || {}) };
6667
selectedData.init = data;
6768
selectedData.over = selectedData.over || {};
69+
this.store[selectorId] = selectedData;
6870
this.selectorId = selectorId;
6971
this.subscriber();
7072
this.send();
@@ -120,20 +122,31 @@ export default class ChannelStore {
120122
...payload,
121123
});
122124

123-
_createAction = (reducer = this.defaultReducer, subId) => {
125+
_createAction = (reducer, getSubId) => {
124126
return payload => {
127+
const subId = getSubId();
125128
const subData = this.store[subId];
126-
subData.over = reducer(global.over, payload);
129+
console.log('TCL: ChannelStore -> _createAction -> subId', subId);
130+
console.log('TCL: ChannelStore -> _createAction -> subData', subData);
131+
const current = {
132+
...subData.init,
133+
...subData.over,
134+
};
135+
const over = (reducer || this.defaultReducer)(current, payload);
136+
subData.over = over;
137+
127138
this.send();
128139
this.subscriber();
129140
};
130141
};
131142

132-
createGlobalAction = reducer => this._createAction(reducer, GLOBAL);
143+
createGlobalAction = reducer => this._createAction(reducer, () => GLOBAL);
133144
createLocalAction = reducer =>
134-
this._createAction(reducer, this.selectedId || this.id);
145+
this._createAction(reducer, () => this.selectorId || this.id);
135146

136147
sendInit = data => {
148+
console.log('TCL: ChannelStore -> onConnectedFn -> id', this.id);
149+
137150
this.init(data);
138151
};
139152

src/decorator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import withChannel from './withChannel';
33

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

6-
export const createDecorator = (Component, { isGlobal } = {}) => initData => (getStory, context) => {
6+
export const createDecorator = (Component, { isGlobal = true } = {}) => initData => (getStory, context) => {
77
const {
88
ADDON_ID,
99
EVENT_ID_INIT,

0 commit comments

Comments
 (0)