Skip to content
This repository has been archived by the owner on Jun 18, 2018. It is now read-only.

Commit

Permalink
Fix #24 where DragDropMonitor failes to notify subscribeToStateChange…
Browse files Browse the repository at this point in the history
… subscribers in some cases
  • Loading branch information
mattiasgronlund committed Jan 27, 2016
1 parent 403f183 commit bb05510
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/DragDropMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,21 @@ export default class DragDropMonitor {
'handlerIds, when specified, must be an array of strings.'
);

let prevStateId = this.store.getState().stateId;
const handleChange = () => {
if (areDirty(this.store.getState().dirtyHandlerIds, handlerIds)) {
listener();
const state = this.store.getState();
const currentStateId = state.stateId;
try {
const canSkipListener = currentStateId === prevStateId || (
currentStateId === prevStateId + 1 &&
!areDirty(state.dirtyHandlerIds, handlerIds)
)

if (!canSkipListener) {
listener();
}
} finally {
prevStateId = currentStateId;
}
};

Expand Down
4 changes: 3 additions & 1 deletion src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { default as dragOffset } from './dragOffset';
import { default as dragOperation } from './dragOperation';
import { default as refCount } from './refCount';
import { default as dirtyHandlerIds } from './dirtyHandlerIds';
import { default as stateId } from './stateId';

export default function (state = {}, action) {
return {
dirtyHandlerIds: dirtyHandlerIds(state.dirtyHandlerIds, action, state.dragOperation),
dragOffset: dragOffset(state.dragOffset, action),
refCount: refCount(state.refCount, action),
dragOperation: dragOperation(state.dragOperation, action)
dragOperation: dragOperation(state.dragOperation, action),
stateId: stateId(state.stateId)
};
}
3 changes: 3 additions & 0 deletions src/reducers/stateId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function stateId(state = 0) {
return state + 1;
}
23 changes: 23 additions & 0 deletions test/DragDropMonitor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,29 @@ describe('DragDropMonitor', () => {
expect(raisedChange).to.equal(false);
});

it('raises global change event on beginDrag(), even if redux did not notify us for all stat updates.', (done) => {
const source = new NormalSource();
const sourceId = registry.addSource(Types.FOO, source);
const target = new NormalTarget()

let notified = false;
monitor.subscribeToStateChange(
() => {
if (!notified)
{
notified = true;
// Add target will send an action to redux, which will trigger a new state update.
// The redux behaviour then is to send only the latest state to the subscribers
// which means that the subscription below will not receive this state.
registry.addTarget(Types.FOO, target)
}
});

monitor.subscribeToStateChange(
() => { done()});
backend.simulateBeginDrag([sourceId]);
});

it('throws when passing clientOffset without getSourceClientOffset', () => {
const source = new NormalSource();
const sourceId = registry.addSource(Types.FOO, source);
Expand Down

0 comments on commit bb05510

Please sign in to comment.