Skip to content

Commit

Permalink
improving hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
alexreardon committed May 14, 2018
1 parent f7b265b commit 37dd5db
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/state/create-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ export default ({
composeEnhancers(
applyMiddleware(
// ## Debug middleware

// > uncomment to use
// debugging logger
require('./debug-middleware/log-middleware').default,
// require('./debug-middleware/log-middleware').default,
// debugging timer
// require('./debug-middleware/timing-middleware').default,
// average action timer
// require('./debug-middleware/timing-average-middleware').default(20),

// ## Application middleware

// Style updates do not cause more actions. It is important to update styles
// before hooks are called: specifically the onDragEnd hook. We need to clear
// the transition styles off the elements before a reorder to prevent strange
Expand All @@ -58,6 +61,9 @@ 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 @@ -67,10 +73,6 @@ export default ({
dropAnimationFinish,
pendingDrop,
autoScroll,

// TODO: where should this go?
hooks(getHooks, announce),

),
),
);
4 changes: 3 additions & 1 deletion src/state/middleware/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,19 @@ export default (getHooks: () => Hooks, announce: Announce) => {

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

// No change to publish
if (areLocationsEqual(lastLocation, location)) {
return;
}

lastLocation = location;
const update: DragUpdate = {
...publishedStart,
destination: location,
};

withTimings('onDragMove', () => execute(getHooks().onDragEnd, update, messagePreset.onDragUpdate));
withTimings('onDragUpdate', () => execute(getHooks().onDragUpdate, update, messagePreset.onDragUpdate));
};

const end = (result: DropResult) => {
Expand Down
2 changes: 2 additions & 0 deletions src/view/drag-drop-context/drag-drop-context.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export default class DragDropContext extends React.Component<Props> {
// Lazy reference to dimension marshal get around circular dependency
getDimensionMarshal: (): DimensionMarshal => this.dimensionMarshal,
styleMarshal: this.styleMarshal,
// This is a function as users are allowed to change their hook functions
// at any time
getHooks: (): Hooks => ({
onDragStart: this.props.onDragStart,
onDragEnd: this.props.onDragEnd,
Expand Down

0 comments on commit 37dd5db

Please sign in to comment.