Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
alexreardon committed May 14, 2018
1 parent 37dd5db commit 53cbb39
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/debug/timings.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const flag: string = '__react-beautiful-dnd-debug-timings-hook__';

const isTimingsEnabled = (): boolean => Boolean(window[flag]);

// TEMP
window[flag] = true;
// Debug: uncomment to enable
// window[flag] = true;

export const start = (key: string) => {
if (!isTimingsEnabled()) {
Expand Down
5 changes: 2 additions & 3 deletions src/state/create-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ export default ({
// We need to stop the marshal before hooks fire as hooks can cause
// dimension registration changes in response to reordering
dimensionMarshalStopper(getDimensionMarshal),
// Fire application hooks
// TODO: where should this be?
hooks(getHooks, announce),
// Fire application hooks in response to drag changes
lift(getDimensionMarshal),
// When a drop is pending and a bulk publish occurs, we need
Expand All @@ -73,6 +70,8 @@ export default ({
dropAnimationFinish,
pendingDrop,
autoScroll,
// Fire hooks for consumers
hooks(getHooks, announce),
),
),
);
48 changes: 40 additions & 8 deletions src/state/dimension-marshal/dimension-marshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ import type {
Collection,
} from './dimension-marshal-types';

const advancedUsageWarning = () => (() => {
let hasAnnounced: boolean = false;

return () => {
if (hasAnnounced) {
return;
}

hasAnnounced = true;

if (process.env.NODE_ENV !== 'production') {
console.warn(`
Warning: you are triggering a recollection of dimensions during a drag.
This is fairly advanced feature used to support interactions such as lazy loading lists.
You might not have intended to trigger this collection. A collection will be triggered
whenever a Droppable or Draggable is added or removed; or when:
- Draggable: 'id' or 'index' change
- Droppable: 'id' change ('type' change is not permitted during a drag)
`.trim());
}
};
})();

export default (callbacks: Callbacks) => {
const entries: Entries = {
droppables: {},
Expand All @@ -38,6 +62,11 @@ export default (callbacks: Callbacks) => {

const collect = ({ includeCritical }: {| includeCritical: boolean |}) => {
invariant(collection, 'Cannot collect without a collection occurring');

if (includeCritical) {
advancedUsageWarning();
}

collector.collect({
collection,
includeCritical,
Expand Down Expand Up @@ -81,7 +110,7 @@ export default (callbacks: Callbacks) => {

invariant(descriptor.id !== collection.critical.draggable.id, 'Cannot unregister dragging item during a drag');

collect({ includeCritical: false });
collect({ includeCritical: true });
};

const updateDraggable = (
Expand All @@ -104,7 +133,7 @@ export default (callbacks: Callbacks) => {
const home: ?DroppableEntry = entries.droppables[descriptor.droppableId];
invariant(home, 'Cannot update a Draggable that does not have a home');

collect({ includeCritical: false });
collect({ includeCritical: true });
};

const unregisterDraggable = (descriptor: DraggableDescriptor) => {
Expand All @@ -126,7 +155,7 @@ export default (callbacks: Callbacks) => {

invariant(descriptor.id !== collection.critical.draggable.id, 'Cannot unregister dragging item during a drag');

collect({ includeCritical: false });
collect({ includeCritical: true });
};

const registerDroppable = (
Expand Down Expand Up @@ -157,7 +186,7 @@ export default (callbacks: Callbacks) => {

invariant(descriptor.id !== collection.critical.droppable.id, 'Cannot register home droppable during a drag');

collect({ includeCritical: false });
collect({ includeCritical: true });
};

const updateDroppable = (
Expand All @@ -176,7 +205,7 @@ export default (callbacks: Callbacks) => {

registerDroppable(descriptor, droppableCallbacks);

collect({ includeCritical: false });
collect({ includeCritical: true });
};

const unregisterDroppable = (descriptor: DroppableDescriptor) => {
Expand All @@ -202,7 +231,7 @@ export default (callbacks: Callbacks) => {

invariant(descriptor.id !== collection.critical.droppable.id, 'Cannot unregister home droppable during a drag');

collect({ includeCritical: false });
collect({ includeCritical: true });
};

const updateDroppableIsEnabled = (id: DroppableId, isEnabled: boolean) => {
Expand Down Expand Up @@ -268,15 +297,18 @@ export default (callbacks: Callbacks) => {
if (!collection) {
return;
}
const home: DroppableDescriptor = collection.critical.droppable;
// Stop any pending dom collections or publish
collector.stop();

// Tell all droppables to stop watching scroll
// all good if they where not already listening
const home: DroppableDescriptor = collection.critical.droppable;
Object.keys(entries.droppables)
.filter((id: DroppableId): boolean => entries.droppables[id].descriptor.type === home.type)
.forEach((id: DroppableId) => entries.droppables[id].callbacks.unwatchScroll());

// Finally - clear our collection
collection = null;
collector.stop();
};

const startPublishing = (request: LiftRequest, windowScroll: Position) => {
Expand Down
8 changes: 4 additions & 4 deletions src/state/middleware/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default (getHooks: () => Hooks, announce: Announce) => {
};

const move = (location: ?DraggableLocation) => {
invariant(publishedStart, 'Cannot fire onDragMove when onDragStart has not been called');
invariant(isDragStartPublished(), 'Cannot fire onDragMove when onDragStart has not been called');

// No change to publish
if (areLocationsEqual(lastLocation, location)) {
Expand All @@ -133,7 +133,7 @@ export default (getHooks: () => Hooks, announce: Announce) => {
};

const end = (result: DropResult) => {
invariant(isDragStartPublished, 'Cannot fire onDragEnd when there is no matching onDragStart');
invariant(isDragStartPublished(), 'Cannot fire onDragEnd when there is no matching onDragStart');
publishedStart = null;
lastLocation = null;
withTimings('onDragEnd', () => execute(getHooks().onDragEnd, result, messagePreset.onDragEnd));
Expand Down Expand Up @@ -191,7 +191,7 @@ export default (getHooks: () => Hooks, announce: Announce) => {
// we should fire a onDragEnd hook
if (action.type === 'CLEAN') {
// Unmatched drag start call - need to cancel
if (publisher.isDragStartPublished) {
if (publisher.isDragStartPublished()) {
publisher.cancel();
}

Expand All @@ -202,7 +202,7 @@ export default (getHooks: () => Hooks, announce: Announce) => {
// ## Perform drag updates

// No drag updates required
if (!publisher.isDragStartPublished) {
if (!publisher.isDragStartPublished()) {
next(action);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/integration/hooks-integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ describe('hooks integration', () => {
});

describe('drag end', () => {
it('should call the onDragEnd hook when a drag ends', () => {
it.only('should call the onDragEnd hook when a drag ends', () => {
drag.perform();

wasDragCompleted();
Expand Down
Empty file.
Empty file.
44 changes: 44 additions & 0 deletions test/unit/state/middleware/hooks/drag-start.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// @flow

describe('start', () => {
it('should call the onDragStart hook when a initial publish occurs', () => {

});

it('should throw an exception if an initial publish is called before a drag ends', () => {

});
});

describe('drop', () => {
it('should call the onDragEnd hook when a DROP_COMPLETE action occurs', () => {

});

it('should throw an exception if there was no drag start published', () => {

});
});

describe('cancel', () => {
it('should publish an on drag end with no destination', () => {

});

// the start location can change after a dynamic update
it('should use the current critical descriptor as the start location', () => {

});

it('should not do anything if a drag start had not been published', () => {

});
});

describe('update', () => {

});

describe('announcements', () => {

});
Empty file.

0 comments on commit 53cbb39

Please sign in to comment.