Skip to content

Commit

Permalink
feat: 🎸 user rxjs for epic observables
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 17, 2018
1 parent 267ac1c commit 98b1ac6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"dependencies": {
"symbol-observable": "^1.2.0"
},
"peerDependencies": {
"rxjs": "^6.2.1"
},
"devDependencies": {
"husky": "0.14.3",
"jest": "22.4.4",
Expand All @@ -58,7 +61,6 @@
"standard": "11.0.1",
"redux": "3.7.2",
"react-redux": "5.0.7",
"rxjs": "^6.2.1",
"react": "16.4.0",
"react-dom": "16.4.0",
"@storybook/react": "3.4.7",
Expand All @@ -67,7 +69,8 @@
"@storybook/addons": "3.4.7",
"babel-runtime": "6.26.0",
"benchmark": "2.1.4",
"travis-deploy-once": "4.4.1"
"travis-deploy-once": "4.4.1",
"rxjs": "^6.2.1"
},
"config": {
"commitizen": {
Expand Down
35 changes: 35 additions & 0 deletions src/plugins/__tests__/epic.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// import {fromObservable} from 'rxjs/internal/observable/fromObservable';
import {from} from 'rxjs'
import {filter, map} from 'rxjs/operators'
import {createStore} from '../../'
import pluginDispatch from '../dispatch'
import pluginReducer from '../reducer'
Expand Down Expand Up @@ -64,5 +67,37 @@ describe('plugin', () => {
{ type: 'NOT_PING' }
])
})

it('works with rxjs', async () => {
const log = []
const epic = (observable, store) =>
from(observable).pipe(
filter(({type}) => type === 'PING'),
map(() => ({type: 'PONG'}))
)
const store = createStoreWithPlugin(epic)
const dispatch = store.dispatch

store.dispatch = (action) => {
log.push(action)
dispatch(action)
}

await delay(100)

store.dispatch({
type: 'PING'
})

store.dispatch({
type: 'NOT_PING'
})

expect(log).toEqual([
{ type: 'PING' },
{ type: 'PONG' },
{ type: 'NOT_PING' }
])
})
})
})
35 changes: 14 additions & 21 deletions src/plugins/epic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import $$observable from 'symbol-observable'
import {Observable} from 'rxjs/internal/Observable'

const plugin = (epic) => store => {
if (process.env.NODE_ENV !== 'production') {
Expand All @@ -7,31 +7,24 @@ const plugin = (epic) => store => {
}
}

const observable = {
subscribe (observer) {
const dispatch = store.dispatch
let live = true
const observable = Observable.create(observer => {
const dispatch = store.dispatch
let live = true

store.dispatch = (action) => {
console.log('A', action)
dispatch(action)
store.dispatch = (action) => {
dispatch(action)

if (live) {
observer.next(action)
}
if (live) {
observer.next(action)
}
}

const unsubscribe = () => {
live = false
}

return {unsubscribe}
},

[$$observable] () {
return this
const unsubscribe = () => {
live = false
}
}

return {unsubscribe}
})

epic(observable, store).subscribe({
next: (action) => store.dispatch(action)
Expand Down

0 comments on commit 98b1ac6

Please sign in to comment.