Skip to content

Commit

Permalink
Don't lift IMPORT action for redux store
Browse files Browse the repository at this point in the history
Related to #278 and dcd346e
  • Loading branch information
zalmoxisus committed Jan 5, 2017
1 parent 58bb58b commit a5e82ab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
27 changes: 17 additions & 10 deletions src/app/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,6 @@ function handleMessages(event) {
if (!message || message.source !== '@devtools-extension') return;
Object.keys(listeners).forEach(id => {
if (message.id && id !== message.id) return;
if (message.type === 'IMPORT') {
message.type = 'DISPATCH';
message.payload = {
type: 'IMPORT_STATE',
...importState(message.state, {})
};
message.state = undefined;
}
if (typeof listeners[id] === 'function') listeners[id](message);
else listeners[id].forEach(fn => { fn(message); });
});
Expand All @@ -96,6 +88,20 @@ export function setListener(onMessage, instanceId) {
window.addEventListener('message', handleMessages, false);
}

const liftListener = (listener, config) => message => {
let data = {};
if (message.type === 'IMPORT') {
data.type = 'DISPATCH';
data.payload = {
type: 'IMPORT_STATE',
...importState(message.state, config)
};
} else {
data = message;
}
listener(data);
};

export function disconnect() {
window.removeEventListener('message', handleMessages);
post({ type: 'DISCONNECT', source });
Expand All @@ -108,10 +114,11 @@ export function connect(config = {}) {
const subscribe = (listener) => {
if (!listener) return undefined;
if (!listeners[id]) listeners[id] = [];
listeners[id].push(listener);
const liftedListener = liftListener(listener, config);
listeners[id].push(liftedListener);

return function unsubscribe() {
const index = listeners[id].indexOf(listener);
const index = listeners[id].indexOf(liftedListener);
listeners[id].splice(index, 1);
};
};
Expand Down
3 changes: 1 addition & 2 deletions test/app/inject/enhancer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ describe('Redux enhancer', () => {
source: '@devtools-extension'
}, '*');
});
expect(message.type).toBe('DISPATCH');
expect(message.payload.type).toBe('IMPORT_STATE');
expect(message.type).toBe('IMPORT');
message = await listenMessage();
expect(message.type).toBe('STATE');
expect(window.store.getState()).toBe(2);
Expand Down

0 comments on commit a5e82ab

Please sign in to comment.