Skip to content

Commit

Permalink
fix(seep): stateProp deep check not work properly
Browse files Browse the repository at this point in the history
  • Loading branch information
nampdn committed Nov 25, 2019
1 parent e0586b6 commit 4adfc91
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 40 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"dependencies": {
"@barajs/core": "3.0.0-alpha.429",
"@barajs/formula": "^3.0.0-alpha.470",
"lodash.isequal": "4.5.0",
"redux": "4.0.4"
},
"devDependencies": {
Expand All @@ -32,6 +33,7 @@
"@semantic-release/git": "7.1.0-beta.9",
"@semantic-release/github": "5.6.0-beta.4",
"@semantic-release/npm": "6.0.0-beta.4",
"@types/lodash.isequal": "4.5.5",
"@types/redux": "3.6.0",
"prettier": "1.18.2",
"semantic-release": "16.0.0-beta.35",
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ const Redux = portion<any, ReduxContext, ReduxMold>({
const store = !predefinedStore
? createStore(reducers, initialState)
: predefinedStore
return { store, initialState: initialState || store.getState() }
const currentState = initialState || store.getState()
return {
store,
initialState: currentState,
lastState: {},
}
},
whenInitialized: flow<unknown, ReduxContext, ReduxMold>({
bootstrap: ({ context, next }) => {
Expand Down
18 changes: 13 additions & 5 deletions src/seep/state.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
import { getContext } from '@barajs/core'
import { lensProp } from '@barajs/formula'
import { Store } from 'redux'
import isEqual from 'lodash.isequal'

import { BARA_REDUX } from '../types'

let lastState: any = {}
export const stateProp = (subscribeProp: string) => (
export const stateProp = (subscribeProp: string, debug?: boolean) => (
_: any,
contextes: any,
) => {
const data: any = getContext(BARA_REDUX, contextes)
const store: Store = data.store
const initialState = data.initialState
const currentState = lensProp(subscribeProp)(store.getState())
const lastState = data.lastState
const currentStateValue = lensProp(subscribeProp)(store.getState())
const lastStateValue =
lastState[subscribeProp] === undefined
? lensProp(subscribeProp)(initialState)
: lensProp(subscribeProp)(lastState)
const shouldPass = currentState !== lastStateValue
if (debug) {
console.log(
`Last state: ${JSON.stringify(lastStateValue, null, 2)}`,
`Current state: ${JSON.stringify(currentStateValue, null, 2)}`,
)
}
const shouldPass = !isEqual(currentStateValue, lastStateValue)
if (shouldPass) {
lastState[subscribeProp] = currentState
lastState[subscribeProp] = currentStateValue
return true
}
return false
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export interface ReduxMold {

export interface ReduxContext {
store: Store
initialState: any
lastState: any
}

export const BARA_REDUX = 'bara-redux'
Expand Down
53 changes: 19 additions & 34 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,18 @@
"@types/minimatch" "*"
"@types/node" "*"

"@types/lodash.isequal@4.5.5":
version "4.5.5"
resolved "https://registry.npmjs.org/@types/lodash.isequal/-/lodash.isequal-4.5.5.tgz#4fed1b1b00bef79e305de0352d797e9bb816c8ff"
integrity sha512-4IKbinG7MGP131wRfceK6W4E/Qt3qssEFLF30LnJbjYiSfHGGRU/Io8YxXrZX109ir+iDETC8hw8QsDijukUVg==
dependencies:
"@types/lodash" "*"

"@types/lodash@*":
version "4.14.149"
resolved "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.149.tgz#1342d63d948c6062838fbf961012f74d4e638440"
integrity sha512-ijGqzZt/b7BfzcK9vTrS6MFljQRPn5BFWOx8oE0GYxribu6uV+aA9zZuXI1zc/etK9E8nrgdoF2+LgUw7+9tJQ==

"@types/minimatch@*":
version "3.0.3"
resolved "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
Expand Down Expand Up @@ -1114,7 +1126,7 @@ debug@^4.0.0:
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.npmjs.org/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down Expand Up @@ -2053,7 +2065,7 @@ import-lazy@^2.1.0:
resolved "https://registry.npmjs.org/import-lazy/-/import-lazy-2.1.0.tgz#05698e3d45c88e8d7e9d92cb0584e77f096f3e43"
integrity sha1-BWmOPUXIjo1+nZLLBYTnfwlvPkM=

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
Expand Down Expand Up @@ -2734,11 +2746,6 @@ lockfile@^1.0.4:
dependencies:
signal-exit "^3.0.2"

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.npmjs.org/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=

lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.npmjs.org/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
Expand All @@ -2747,33 +2754,11 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.npmjs.org/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.npmjs.org/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.npmjs.org/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.npmjs.org/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=

lodash._getnative@*, lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.npmjs.org/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=

lodash._root@~3.0.0:
version "3.0.1"
resolved "https://registry.npmjs.org/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
Expand All @@ -2799,6 +2784,11 @@ lodash.get@^4.4.2:
resolved "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
integrity sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=

lodash.isequal@4.5.0:
version "4.5.0"
resolved "https://registry.npmjs.org/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
integrity sha1-QVxEePK8wwEgwizhDtMib30+GOA=

lodash.ismatch@^4.4.0:
version "4.4.0"
resolved "https://registry.npmjs.org/lodash.ismatch/-/lodash.ismatch-4.4.0.tgz#756cb5150ca3ba6f11085a78849645f188f85f37"
Expand All @@ -2814,11 +2804,6 @@ lodash.isstring@^4.0.1:
resolved "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=

lodash.restparam@*:
version "3.6.1"
resolved "https://registry.npmjs.org/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=

lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.npmjs.org/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
Expand Down

0 comments on commit 4adfc91

Please sign in to comment.