Skip to content
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

Push initial value to Redux selector observers #75

Merged
merged 4 commits into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- prettier-ignore-start -->
# Changelog

## 25 September 2018

| Package | Version | Type | Changes |
| --- | --- | --- | --- |
| refract-redux-callbag<br>refract-redux-most<br>refract-redux-rxjs<br>refract-redux-xstream | 1.1.0 | Bug | [(#75)](https://github.com/fanduel-oss/refract/pull/75) initialise Redux selector observers with their initial value |
<!-- prettier-ignore-end -->
3 changes: 3 additions & 0 deletions base/redux/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ export const observeFactory = (store): ObserveFn => {
}

if (typeof actionOrSelector === 'function') {
const initialValue: T = actionOrSelector(store.getState())

return storeObservable.pipe<T>(
map(actionOrSelector),
startWith(initialValue),
distinctUntilChanged<T>()
)
}
Expand Down
10 changes: 9 additions & 1 deletion base/redux/observable_callbag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fromObs = require('callbag-from-obs')
const dropRepeats = require('callbag-drop-repeats')
const map = require('callbag-map')
const pipe = require('callbag-pipe')
const startWith = require('callbag-start-with')

import { Selector } from './baseTypes'

Expand Down Expand Up @@ -38,7 +39,14 @@ export const observeFactory = (store): ObserveFn => {
}

if (typeof actionOrSelector === 'function') {
return pipe(storeObservable, map(actionOrSelector), dropRepeats())
const initialValue: T = actionOrSelector(store.getState())

return pipe(
storeObservable,
map(actionOrSelector),
startWith(initialValue),
dropRepeats()
)
}
}
}
9 changes: 7 additions & 2 deletions base/redux/observable_most.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { from, Stream, Subscriber as Listener } from 'most'
import { from, Stream, Subscriber as Listener, of } from 'most'
import $$observable from 'symbol-observable'
import { Selector } from './baseTypes'

Expand Down Expand Up @@ -27,7 +27,12 @@ export const observeFactory = (store): ObserveFn => {
}

if (typeof actionOrSelector === 'function') {
return storeObservable.map(actionOrSelector).skipRepeats()
const initialValue: T = actionOrSelector(store.getState())

return storeObservable
.map(actionOrSelector)
.merge(of(initialValue))
.skipRepeats()
}
}
}
7 changes: 6 additions & 1 deletion base/redux/observable_xstream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ export const observeFactory = (store): ObserveFn => {
}

if (typeof actionOrSelector === 'function') {
return storeObservable.map(actionOrSelector).compose(dropRepeats())
const initialValue: T = actionOrSelector(store.getState())

return storeObservable
.map(actionOrSelector)
.startWith(initialValue)
.compose(dropRepeats())
}
}
}
2 changes: 1 addition & 1 deletion docs/usage/observing-redux.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Each observed action is passed into your stream with all its data - including th

## Observing State

If you pass in a function, `store.observe` will treat it as a Redux selector, and return a stream which subscribes to the state using your function. Any time the selected slice of state changes, its new value will be piped to your stream.
If you pass in a function, `store.observe` will treat it as a Redux selector, and return a stream which subscribes to the state using your function. It is initialised with the selector current value and any time the selected slice of state changes, its new value will be piped to your stream if changed (`===` comparison).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

selector -> selector's, plus comma after current value?


```js
const storeShape = {
Expand Down
18 changes: 12 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"copy": "node ./scripts/copy",
"generatePackages": "node ./scripts/generatePkg",
"prebuild": "npm run generatePackages && npm run copy",
"build":
"npm run build:react && npm run build:redux && npm run build:inferno && npm run build:preact",
"build": "npm run build:react && npm run build:redux && npm run build:inferno && npm run build:preact",
"build:react": "MAIN_LIB=react rollup -c rollup.config.js",
"build:redux": "MAIN_LIB=redux rollup -c rollup.config.js",
"build:preact": "MAIN_LIB=preact rollup -c rollup.config.js",
Expand All @@ -20,8 +19,7 @@
"test:preact": "jest --config base/__tests__/preact/jest.config.js",
"test:inferno": "jest --config base/__tests__/inferno/jest.config.js",
"test:redux": "jest --config base/__tests__/redux/jest.config.js",
"test":
"npm run test:react && npm run test:inferno && npm run test:preact && npm run test:redux",
"test": "npm run test:react && npm run test:inferno && npm run test:preact && npm run test:redux",
"precommit": "lint-staged",
"prerelease": "npm run build",
"release": "node ./scripts/publish"
Expand All @@ -36,6 +34,7 @@
"callbag-from-obs": "~1.2.0",
"callbag-map": "~1.0.1",
"callbag-pipe": "~1.1.1",
"callbag-start-with": "~3.1.0",
"callbag-to-obs": "~1.0.0",
"checksum": "~0.1.1",
"enzyme": "~3.3.0",
Expand Down Expand Up @@ -75,7 +74,14 @@
"xstream": "~11.7.0"
},
"lint-staged": {
"*.{ts,tsx}": ["prettier --write", "tslint", "git add"],
"*.{js,md}": ["prettier --write", "git add"]
"*.{ts,tsx}": [
"prettier --write",
"tslint",
"git add"
],
"*.{js,md}": [
"prettier --write",
"git add"
]
}
}
3 changes: 2 additions & 1 deletion packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ const extraDependencies = {
'refract-redux-callbag': {
'callbag-drop-repeats': '~1.0.0',
'callbag-map': '~1.0.1',
'callbag-pipe': '~1.1.1'
'callbag-pipe': '~1.1.1',
'callbag-start-with': '~3.1.0'
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/refract-redux-callbag/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"callbag-from-obs": "~1.2.0",
"callbag-map": "~1.0.1",
"callbag-pipe": "~1.1.1",
"callbag-start-with": "~3.1.0",
"symbol-observable": "~1.2.0"
},
"keywords": [
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,10 @@ callbag-skip@1.0.x:
version "1.0.0"
resolved "https://registry.yarnpkg.com/callbag-skip/-/callbag-skip-1.0.0.tgz#91f60c434d1a7aba02b8b7ef1aad82da0a805ceb"

callbag-start-with@~3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/callbag-start-with/-/callbag-start-with-3.1.0.tgz#58c7f7cb3f3f44049e130166c2cf884c76e332ac"

callbag-take@1.0.x:
version "1.0.0"
resolved "https://registry.yarnpkg.com/callbag-take/-/callbag-take-1.0.0.tgz#d3d3acf3f73ef9a7cda42958dfb4de37b9c0a2b8"
Expand Down