Skip to content

Commit

Permalink
Pass a custom replacer when exporting data
Browse files Browse the repository at this point in the history
Related to #278.
  • Loading branch information
zalmoxisus committed Jan 3, 2017
1 parent 36d7286 commit 42c4538
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/app/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ function tryCatchStringify(obj) {
}
}

function stringify(obj, serialize) {
function defaultReplacer(key, value) {
if (value && typeof value.toJS === 'function') return value.toJS();
return value;
}

function stringify(obj, serialize, replacer) {
if (typeof serialize === 'undefined') {
return tryCatchStringify(obj);
}
if (serialize === true) {
return jsan.stringify(obj, function(key, value) {
if (value && typeof value.toJS === 'function') return value.toJS();
return value;
}, null, true);
return jsan.stringify(obj, replacer && replacer.replacer || defaultReplacer, null, true);
}
return jsan.stringify(obj, serialize.replacer, null, serialize.options);
}
Expand All @@ -47,9 +49,9 @@ export function toContentScript(message, serializeState, serializeAction) {
message.computedStates = stringify(computedStates, serializeState);
message.committedState = typeof committedState !== 'undefined';
} else if (message.type === 'EXPORT') {
message.payload = stringify(message.payload, serializeAction);
message.payload = stringify(message.payload, true, serializeAction);
if (typeof message.committedState !== 'undefined') {
message.committedState = stringify(message.committedState, serializeState);
message.committedState = stringify(message.committedState, true, serializeState);
}
}
post(message);
Expand Down
2 changes: 1 addition & 1 deletion src/browser/extension/inject/pageScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const devToolsExtension = function(reducer, preloadedState, config) {
});
toContentScript({
type: 'EXPORT', payload, committedState: liftedState.committedState, source, instanceId
}, true, true);
}, serializeState, serializeAction);
}

function relay(type, state, action, nextActionId, libConfig) {
Expand Down

0 comments on commit 42c4538

Please sign in to comment.