Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
alexreardon committed May 4, 2018
1 parent bed898d commit d951a2d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 95 deletions.
62 changes: 0 additions & 62 deletions src/state/dimension-marshal/buffer-marshal.js

This file was deleted.

10 changes: 5 additions & 5 deletions src/state/dimension-marshal/collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import type {
DroppableId,
DraggableDimension,
DroppableDimension,
ScrollOptions,
Collection,
} from '../../types';
import type { ToBeCollected } from './dimension-marshal-types';

Expand Down Expand Up @@ -49,20 +47,21 @@ export default ({

const collectFromDOM = (toBeCollected: ToBeCollected, options?: CollectionOptions): Collected => {
invariant(isActive, 'Should not collect when not active');
console.log('excluding', options);

const droppables: DroppableDimension[] = toBeCollected.droppables
.filter((id: DroppableId): boolean => Boolean(options && options.exclude.droppableId === id))
.filter((id: DroppableId): boolean => Boolean(options && options.exclude.droppableId !== id))
.map((id: DroppableId): DroppableDimension => getDroppable(id));

const draggables: DraggableDimension[] = toBeCollected.draggables
.filter((id: DraggableId): boolean => Boolean(options && options.exclude.draggableId === id))
.filter((id: DraggableId): boolean => Boolean(options && options.exclude.draggableId !== id))
.map((id: DraggableId): DraggableDimension => getDraggable(id));

return { draggables, droppables };
};

const run = (options?: CollectionOptions) => {
invariant(isRunning, 'Cannot start a new run when a run is already occurring');
invariant(!isRunning, 'Cannot start a new run when a run is already occurring');

isRunning = true;

Expand Down Expand Up @@ -112,6 +111,7 @@ export default ({
// Queue another run
if (isRunning) {
isQueued = true;
return;
}

run();
Expand Down
7 changes: 1 addition & 6 deletions src/state/dimension-marshal/dimension-marshal-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ export type DroppableCallbacks = {|
getDimensionAndWatchScroll: GetDroppableDimensionFn,
// scroll a droppable
scroll: (change: Position) => void,
// Droppable must listen to scroll events and publish them using the
// onChange callback. If the Droppable is not in a scroll container then
// it does not need to do anything
watchScroll: (options: ScrollOptions) => void,
// If the Droppable is listening for scrol events - it needs to stop!
// This may be called even if watchScroll was not previously called
// If the Droppable is listening for scroll events - it needs to stop!
unwatchScroll: () => void,
|}

Expand Down
9 changes: 5 additions & 4 deletions src/state/dimension-marshal/dimension-marshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,8 @@ export default (callbacks: Callbacks) => {
entry.callbacks.scroll(change);
};

const collectCriticalDimensions = (request: ?LiftRequest) => {
invariant(collection, 'Cannot start capturing dimensions for a drag it is already dragging');
invariant(request, 'Cannot start capturing dimensions with an invalid request', request);
const collectCriticalDimensions = (request: LiftRequest) => {
invariant(!collection, 'Cannot start capturing critical dimensions as there is already a collection');

const draggables: DraggableEntryMap = entries.draggables;
const droppables: DroppableEntryMap = entries.droppables;
Expand Down Expand Up @@ -311,10 +310,11 @@ export default (callbacks: Callbacks) => {

const stopCollecting = () => {
invariant(collection, 'Cannot stop collecting when there is no collection');

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

collection = null;
Expand All @@ -325,6 +325,7 @@ export default (callbacks: Callbacks) => {
const phase: Phase = current.phase;

if (phase === 'COLLECTING_INITIAL_DIMENSIONS') {
invariant(current.dimension.request, 'Cannot start collecting dimensions without a request');
collectCriticalDimensions(current.dimension.request);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,25 @@ export default class DraggableDimensionPublisher extends Component<Props> {
}));

publish = () => {
const marshal: DimensionMarshal = this.context[dimensionMarshalKey];
const descriptor: DraggableDescriptor = this.getMemoizedDescriptor(
this.props.draggableId,
this.props.droppableId,
this.props.index
);

// No changes to the descriptor
if (descriptor === this.publishedDescriptor) {
if (!this.publishedDescriptor) {
marshal.registerDraggable(descriptor, this.getDimension);
this.publishedDescriptor = descriptor;
return;
}

if (this.publishedDescriptor) {
this.unpublish();
// No changes to the descriptor
if (descriptor === this.publishedDescriptor) {
return;
}

const marshal: DimensionMarshal = this.context[dimensionMarshalKey];
marshal.registerDraggable(descriptor, this.getDimension);
marshal.updateDraggable(this.publishedDescriptor, descriptor, this.getDimension);
this.publishedDescriptor = descriptor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default class DroppableDimensionPublisher extends Component<Props> {
/* eslint-disable react/sort-comp */
closestScrollable: ?Element = null;
isWatchingScroll: boolean = false;
isDragOccurring: boolean = false;
scrollOptions: ?ScrollOptions = null;
callbacks: DroppableCallbacks;
publishedDescriptor: ?DroppableDescriptor = null;
Expand Down Expand Up @@ -129,10 +130,11 @@ export default class DroppableDimensionPublisher extends Component<Props> {
};

unwatchScroll = () => {
invariant(this.isWatchingScroll,
`Droppable should not be instructed to stop watching a scroll
when it was not listening to scroll changes`
);
// It is possible for a Droppable to be asked to unwatch a scroll
// (Eg it has not been collected yet, and the drag ends)
if (!this.isWatchingScroll) {
return;
}

this.isWatchingScroll = false;
this.scrollOptions = null;
Expand Down Expand Up @@ -196,6 +198,7 @@ export default class DroppableDimensionPublisher extends Component<Props> {

if (!this.publishedDescriptor) {
marshal.registerDroppable(descriptor, this.callbacks);
this.publishedDescriptor = descriptor;
return;
}

Expand All @@ -205,15 +208,12 @@ export default class DroppableDimensionPublisher extends Component<Props> {
}

// already published and there has been changes
marshal.updateDroppable(this.publishedDescriptor.id, descriptor, this.callbacks);
marshal.updateDroppable(this.publishedDescriptor, descriptor, this.callbacks);
this.publishedDescriptor = descriptor;
}

unpublish = () => {
if (!this.publishedDescriptor) {
console.error('Cannot unpublish descriptor when none is published');
return;
}
invariant(this.publishedDescriptor, 'Cannot unpublish descriptor when none is published');

// Using the previously published id to unpublish. This is to guard
// against the case where the id dynamically changes. This is not
Expand All @@ -234,8 +234,7 @@ export default class DroppableDimensionPublisher extends Component<Props> {
const targetRef: ?HTMLElement = getDroppableRef();
const descriptor: ?DroppableDescriptor = this.publishedDescriptor;

invariant(targetRef, 'DimensionPublisher cannot calculate a dimension when not attached to the DOM');
invariant(!this.isWatchingScroll, 'Attempting to recapture Droppable dimension while already watching scroll on previous capture');
invariant(targetRef, 'Cannot calculate a dimension when not attached to the DOM');
invariant(descriptor, 'Cannot get dimension for unpublished droppable');

const scrollableRef: ?Element = getClosestScrollable(targetRef);
Expand Down
3 changes: 2 additions & 1 deletion stories/src/dynamic/quote-app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import QuoteList from '../primatives/quote-list';
import { DragDropContext } from '../../../src/';
import { generateQuoteMap, authors } from '../data';
import { reorderQuoteMap } from '../reorder';
import { grid } from '../constants';
import type { Quote, QuoteMap, Author } from '../types';
import type { DropResult } from '../../../src/types';
import { grid } from '../constants';

const intial: QuoteMap = generateQuoteMap(20);

Expand Down Expand Up @@ -167,6 +167,7 @@ export default class QuoteApp extends React.Component<*, State> {
<Container>
{Object.keys(quoteMap).map((key: string) => (
<QuoteList
key={key}
listId={key}
quotes={quoteMap[key]}
/>
Expand Down

0 comments on commit d951a2d

Please sign in to comment.