Skip to content

Commit

Permalink
Merge pull request #2 from tswaters/bugfix/1
Browse files Browse the repository at this point in the history
Return subscription so callers can remove it
  • Loading branch information
tswaters authored Nov 10, 2017
2 parents eaf3034 + 2718ea4 commit b1bb338
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Object.defineProperty(exports, "__esModule", {
exports.default = function (store, selector, cb) {
var data = selector(store.getState());

store.subscribe(function () {
return store.subscribe(function () {
var newData = selector(store.getState());
if (data !== newData) {
data = newData;
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export default (store, selector, cb) => {
let data = selector(store.getState())

store.subscribe(() => {
return store.subscribe(() => {
let newData = selector(store.getState())
if (data !== newData) {
data = newData
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/redux.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const REMOVE_WIDGET = 'REMOVE_WIDGET'
let widget_id = 0
let item_id = 0

export const reset = () => {
widget_id = 0
item_id = 0
}

export const addItem = name => ({type: ADD_ITEM, id: widget_id++, name})
export const removeItem = id => ({type: REMOVE_ITEM, id})
export const getItems = createSelector(state => state.items, items => items)
Expand Down
16 changes: 16 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import subscribe from '../src/index'
import {createStore} from 'redux'

import reducer, {
reset,
getItems,
addItem,
removeItem,
Expand All @@ -19,6 +20,7 @@ describe('redux subscrube reselect', () => {
let store = null

beforeEach(() => {
reset()
store = createStore(reducer)
store.dispatch(addWidget('widget #1'))
store.dispatch(addWidget('widget #2'))
Expand All @@ -45,4 +47,18 @@ describe('redux subscrube reselect', () => {

})

it('should unsubscribe properly', () => {

const widgetSubscriber = sinon.stub()
const subscriber = subscribe(store, getWidgets, widgetSubscriber)

store.dispatch(removeWidget(0))
subscriber()
store.dispatch(removeWidget(1))

assert.equal(widgetSubscriber.callCount, 1)
assert.deepEqual(widgetSubscriber.firstCall.args[0], [{id: 1, name: 'widget #2'}])

})

})

0 comments on commit b1bb338

Please sign in to comment.