Skip to content

Support circular structure for logger #327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"chat": "cd examples/chat && webpack-dev-server --inline --hot --config ../webpack.shared.config.js",
"build": "node build/build.js",
"build-examples": "BABEL_ENV=development webpack --config examples/webpack.build-all.config.js",
"unit": "BABEL_ENV=development mocha test/unit/test.js --compilers js:babel-core/register",
"unit": "BABEL_ENV=development mocha test/unit/*.js --compilers js:babel-core/register",
"pree2e": "npm run build-examples",
"e2e": "casperjs test --concise ./test/e2e",
"test": "eslint src && npm run unit && npm run e2e",
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/logger.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
// Credits: borrowed code from fcomb/redux-logger

import { deepCopy } from '../util'

export default function createLogger ({
collapsed = true,
transformer = state => state,
mutationTransformer = mut => mut
} = {}) {
return store => {
let prevState = JSON.parse(JSON.stringify(store.state))
let prevState = deepCopy(store.state)

store.subscribe((mutation, state) => {
if (typeof console === 'undefined') {
return
}
const nextState = JSON.parse(JSON.stringify(state))
const nextState = deepCopy(state)
const time = new Date()
const formattedTime = ` @ ${pad(time.getHours(), 2)}:${pad(time.getMinutes(), 2)}:${pad(time.getSeconds(), 2)}.${pad(time.getMilliseconds(), 3)}`
const formattedMutation = mutationTransformer(mutation)
Expand Down
48 changes: 48 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,54 @@ export function mergeObjects (arr) {
}, {})
}

/**
* Get the first item that pass the test
* by second argument function
*
* @param {Array} list
* @param {Function} f
* @return {*}
*/
function find (list, f) {
return list.filter(f)[0]
}

/**
* Deep copy the given object considering circular structure.
* This function caches all nested objects and its copies.
* If it detects circular structure, use cached copy to avoid infinite loop.
*
* @param {*} obj
* @param {Array<Object>} cache
* @return {*}
*/
export function deepCopy (obj, cache = []) {
// just return if obj is immutable value
if (obj === null || typeof obj !== 'object') {
return obj
}

// if obj is hit, it is in circular structure
const hit = find(cache, c => c.original === obj)
if (hit) {
return hit.copy
}

const copy = Array.isArray(obj) ? [] : {}
// put the copy into cache at first
// because we want to refer it in recursive deepCopy
cache.push({
original: obj,
copy
})

Object.keys(obj).forEach(key => {
copy[key] = deepCopy(obj[key], cache)
})

return copy
}

/**
* Check whether the given value is Object or not
*
Expand Down
42 changes: 42 additions & 0 deletions test/unit/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect } from 'chai'
import { deepCopy } from '../../src/util'

describe('util', () => {
it('deepCopy: nornal structure', () => {
const original = {
a: 1,
b: 'string',
c: true,
d: null,
e: undefined
}
const copy = deepCopy(original)

expect(copy).to.deep.equal(original)
})

it('deepCopy: nested structure', () => {
const original = {
a: {
b: 1,
c: [2, 3, {
d: 4
}]
}
}
const copy = deepCopy(original)

expect(copy).to.deep.equal(original)
})

it('deepCopy: circular structure', () => {
const original = {
a: 1
}
original.circular = original

const copy = deepCopy(original)

expect(copy).to.deep.equal(original)
})
})