-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
abb3abc
commit 2e9b757
Showing
9 changed files
with
139 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// @flow | ||
import type { | ||
DraggableId, | ||
DroppableId, | ||
DraggableDimensionMap, | ||
DroppableDimensionMap, | ||
DimensionMap, | ||
DraggableDimension, | ||
DroppableDimension, | ||
} from '../../types'; | ||
|
||
type Args = {| | ||
getDraggableDimension: (id: DraggableId) => DraggableDimension, | ||
getDroppableDimension: (id: DroppableId) => DroppableDimension, | ||
publish: (map: DimensionMap) => void, | ||
|} | ||
|
||
type Collector = {| | ||
collectDraggable: (id: DraggableId) => void, | ||
collectDroppable: (id: DroppableId) => void, | ||
|} | ||
|
||
type ToCollect = {| | ||
draggables: DraggableId[], | ||
droppables: DroppableId[], | ||
|} | ||
|
||
const toMap = array => array.reduce((previous, current) => { | ||
previous[current.descriptor.id] = current; | ||
return previous; | ||
}, {}); | ||
|
||
export default ({ | ||
getDraggableDimension, | ||
getDroppableDimension, | ||
publish, | ||
}: Args): Collector => { | ||
const toCollect: ToCollect = { | ||
draggables: [], | ||
droppables: [], | ||
}; | ||
let frameId: ?AnimationFrameID = null; | ||
|
||
const collect = () => { | ||
if (frameId) { | ||
return; | ||
} | ||
|
||
frameId = requestAnimationFrame(() => { | ||
frameId = null; | ||
const draggables: DraggableDimensionMap = toMap( | ||
toCollect.draggables.map((id: DraggableId): DraggableDimension => getDraggableDimension(id)) | ||
); | ||
|
||
const droppables: DroppableDimensionMap = toMap( | ||
toCollect.droppables.map((id: DroppableId): DroppableDimension => getDroppableDimension(id)) | ||
); | ||
|
||
const dimensions: DimensionMap = { draggables, droppables }; | ||
|
||
// reset toCollect | ||
toCollect.draggables.length = 0; | ||
toCollect.droppables.length = 0; | ||
|
||
publish(dimensions); | ||
}); | ||
}; | ||
|
||
const collectDraggable = (id: DraggableId) => { | ||
toCollect.draggables.push(id); | ||
collect(); | ||
}; | ||
|
||
const collectDroppable = (id: DroppableId) => { | ||
toCollect.droppables.push(id); | ||
collect(); | ||
}; | ||
|
||
return { collectDraggable, collectDroppable }; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.