Skip to content

Commit

Permalink
Merge pull request #29 from cyclejs-community/add-eslint
Browse files Browse the repository at this point in the history
Add ESLint
  • Loading branch information
lmatteis authored Feb 13, 2017
2 parents 03b423d + 84137ec commit b249273
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 67 deletions.
29 changes: 29 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = {
"env": {
"browser": true,
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"never"
]
}
};
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
}
],
"scripts": {
"test": "npm run build && jest",
"eslint": "eslint --fix src/ test/",
"test": "npm run build && npm run eslint && jest",
"build": "babel --presets es2015 src --out-dir dist",
"prepublish": "npm run build"
},
"jest": {
"testPathIgnorePatterns": ["/example"]
"testPathIgnorePatterns": [
"/example"
]
},
"license": "MIT",
"dependencies": {
Expand All @@ -35,6 +38,7 @@
"babel-cli": "^6.18.0",
"babel-jest": "^18.0.0",
"babel-preset-es2015": "^6.9.0",
"eslint": "^3.15.0",
"jest": "^18.1.0",
"redux": "^3.6.0"
}
Expand Down
16 changes: 8 additions & 8 deletions src/combineCycles.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import xs from 'xstream';
import xs from 'xstream'

export default function combineCycles(...mains) {
return sources => {
const sinks = mains.map(main => main(sources));
const sinks = mains.map(main => main(sources))

const drivers = Object.keys(
sinks.reduce((drivers, sink) => Object.assign(drivers, sink), {})
);
)

const combinedSinks = drivers
.reduce((combinedSinks, driver) => {
const driverSinks = sinks
.filter(sink => sink[driver])
.map(sink => sink[driver]);
.map(sink => sink[driver])

combinedSinks[driver] = xs.merge(...driverSinks);
return combinedSinks;
}, {});
combinedSinks[driver] = xs.merge(...driverSinks)
return combinedSinks
}, {})

return combinedSinks;
return combinedSinks
}
}
42 changes: 21 additions & 21 deletions src/createCycleMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@
import {run} from '@cycle/xstream-run';
import xs from 'xstream';
import {run} from '@cycle/xstream-run'
import xs from 'xstream'

export default function createCycleMiddleware(mainFn, drivers = {}) {
return store =>
next => {
let actionListener = null;
let stateListener = null;
let actionListener = null
let stateListener = null

function actionDriver(outgoing$) {
outgoing$.addListener({
next: outgoing => {
store.dispatch(outgoing);
store.dispatch(outgoing)
},
error: () => {},
complete: () => {},
});
})

return xs.create({
start: listener => {
actionListener = listener;
actionListener = listener
},
stop: () => {},
});
})
}

const isSame = {};
const getCurrent = store.getState;
const isSame = {}
const getCurrent = store.getState
function stateDriver() {
return xs.create({
start: listener => {
stateListener = listener;
stateListener = listener
},
stop: () => {},
})
.fold((prevState, currState) => {
if (prevState === getCurrent) {
prevState = getCurrent();
prevState = getCurrent()
}
if (prevState === currState) {
return isSame;
return isSame
}
return currState;
return currState
}, getCurrent)
.map(state => state === getCurrent ? getCurrent() : state)
.filter(state => state !== isSame);
.filter(state => state !== isSame)
}

drivers.ACTION = actionDriver;
drivers.STATE = stateDriver;
run(mainFn, drivers);
drivers.ACTION = actionDriver
drivers.STATE = stateDriver
run(mainFn, drivers)

return action => {
let result = next(action)
if (actionListener) {
actionListener.next(action);
actionListener.next(action)
}
if (stateListener) {
stateListener.next(store.getState());
stateListener.next(store.getState())
}
return result
}
}
}
}
73 changes: 37 additions & 36 deletions test/createCycleMiddleware.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { createCycleMiddleware } from '../';
import { createStore, applyMiddleware } from 'redux';
import xs from 'xstream';
jest.useFakeTimers();
/* eslint-disable no-undef */
import { createCycleMiddleware } from '../'
import { createStore, applyMiddleware } from 'redux'
import xs from 'xstream'
jest.useFakeTimers()

function initStore(main, drivers, reducer = null) {
const rootReducer = reducer || ((state = [], action) => state.concat(action));
const cycleMiddleware = createCycleMiddleware(main, drivers);
const rootReducer = reducer || ((state = [], action) => state.concat(action))
const cycleMiddleware = createCycleMiddleware(main, drivers)
const store = createStore(
rootReducer,
applyMiddleware(cycleMiddleware)
);
return store;
)
return store
}

describe('Redux cycle middleware', () => {
it('dispatches a PING to see whether the middleware dispatches a PONG', (done) => {
function main(sources) {
const pong$ = sources.ACTION
.filter(action => action.type === 'PING')
.mapTo({ type: 'PONG' });
.mapTo({ type: 'PONG' })

return {
ACTION: pong$
Expand All @@ -36,19 +37,19 @@ describe('Redux cycle middleware', () => {

expect(store.getState()).toMatchObject(expectedActions)

done();
done()
})

it('dispatches a PING to see whether the middleware dispatches a PONG after 10 seconds', (done) => {
function main(sources) {
const pong$ = sources.ACTION
.filter(action => action.type === 'PING')
.map(a =>
.map(() =>
xs.periodic(10000)
.take(1)
.mapTo({ type: 'PONG' })
)
.flatten();
.flatten()

return {
ACTION: pong$
Expand All @@ -69,36 +70,36 @@ describe('Redux cycle middleware', () => {
{ type: 'PING' }
])

expect(store.getState()).not.toMatchObject(expectedActions);
jest.runAllTimers();
expect(setInterval.mock.calls[0][1]).toBe(10000);
expect(store.getState()).toMatchObject(expectedActions);
done();
expect(store.getState()).not.toMatchObject(expectedActions)
jest.runAllTimers()
expect(setInterval.mock.calls[0][1]).toBe(10000)
expect(store.getState()).toMatchObject(expectedActions)
done()
})

it('dispatches INCREMENT_ASYNC and INCREMENT_IF_ODD actions to check whether state updates correctly', (done) => {
function main(sources) {
const state$ = sources.STATE;
const state$ = sources.STATE
const isOdd$ = state$
.map(state => state % 2 === 1)
.take(1);
.take(1)

const incrementIfOdd$ = sources.ACTION
.filter(action => action.type === 'INCREMENT_IF_ODD')
.map(action =>
.map(() =>
isOdd$
)
.flatten()
.filter(isOdd => isOdd)
.mapTo({ type: 'INCREMENT' });
.mapTo({ type: 'INCREMENT' })

const increment$ = sources.ACTION
.filter(action => action.type === 'INCREMENT_ASYNC')
.mapTo({ type: 'INCREMENT' });
.mapTo({ type: 'INCREMENT' })

const decrement$ = sources.ACTION
.filter(action => action.type === 'DECREMENT_ASYNC')
.mapTo({ type: 'DECREMENT' });
.mapTo({ type: 'DECREMENT' })

const both$ = xs.merge(increment$, decrement$)

Expand All @@ -109,29 +110,29 @@ describe('Redux cycle middleware', () => {

const store = initStore(main, {}, (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
})

store.dispatch({ type: 'INCREMENT_ASYNC' })
expect(store.getState()).toBe(1);
expect(store.getState()).toBe(1)
store.dispatch({ type: 'INCREMENT_ASYNC' })
expect(store.getState()).toBe(2);
expect(store.getState()).toBe(2)
store.dispatch({ type: 'INCREMENT_ASYNC' })
expect(store.getState()).toBe(3);
expect(store.getState()).toBe(3)
store.dispatch({ type: 'INCREMENT_IF_ODD' })
expect(store.getState()).toBe(4);
expect(store.getState()).toBe(4)
store.dispatch({ type: 'INCREMENT_IF_ODD' })
expect(store.getState()).toBe(4);
expect(store.getState()).toBe(4)
store.dispatch({ type: 'INCREMENT_ASYNC' })
expect(store.getState()).toBe(5);
expect(store.getState()).toBe(5)

done();
done()

})
})

0 comments on commit b249273

Please sign in to comment.