How to use the use-memo-one.useCallbackOne function in use-memo-one

To help you get started, we’ve selected a few use-memo-one examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github atlassian / react-beautiful-dnd / src / view / use-drag-handle / use-drag-handle.js View on Github external
export default function useDragHandle(args: Args): ?DragHandleProps {
  // Capturing
  const capturingRef = useRef(null);
  const onCaptureStart = useCallbackOne((abort: () => void) => {
    invariant(
      !capturingRef.current,
      'Cannot start capturing while something else is',
    );
    capturingRef.current = {
      abort,
    };
  }, []);
  const onCaptureEnd = useCallbackOne(() => {
    invariant(
      capturingRef.current,
      'Cannot stop capturing while nothing is capturing',
    );
    capturingRef.current = null;
  }, []);
  const abortCapture = useCallbackOne(() => {
    invariant(capturingRef.current, 'Cannot abort capture when there is none');
    capturingRef.current.abort();
  }, []);

  const { canLift, style: styleContext }: AppContextValue = useRequiredContext(
    AppContext,
  );
  const {
    isDragging,
github atlassian / react-beautiful-dnd / src / view / use-drag-handle / sensor / use-touch-sensor.js View on Github external
{
        eventName: supportedPageVisibilityEventName,
        fn: cancel,
      },
    ];
    return bindings;
  }, [
    callbacks,
    cancel,
    getIsCapturing,
    getShouldRespectForcePress,
    schedule,
    stop,
  ]);

  const bindWindowEvents = useCallbackOne(() => {
    const win: HTMLElement = getWindow();
    const options = { capture: true };

    // setting up our unbind before we bind
    unbindWindowEventsRef.current = () =>
      unbindEvents(win, windowBindings, options);

    bindEvents(win, windowBindings, options);
  }, [getWindow, windowBindings]);

  const startDragging = useCallbackOne(() => {
    const pending: ?PendingDrag = pendingRef.current;
    invariant(pending, 'Cannot start a drag without a pending drag');

    isDraggingRef.current = true;
    pendingRef.current = null;
github atlassian / react-beautiful-dnd / src / view / use-drag-handle / sensor / use-touch-sensor.js View on Github external
schedule,
    stop,
  ]);

  const bindWindowEvents = useCallbackOne(() => {
    const win: HTMLElement = getWindow();
    const options = { capture: true };

    // setting up our unbind before we bind
    unbindWindowEventsRef.current = () =>
      unbindEvents(win, windowBindings, options);

    bindEvents(win, windowBindings, options);
  }, [getWindow, windowBindings]);

  const startDragging = useCallbackOne(() => {
    const pending: ?PendingDrag = pendingRef.current;
    invariant(pending, 'Cannot start a drag without a pending drag');

    isDraggingRef.current = true;
    pendingRef.current = null;
    hasMovedRef.current = false;

    callbacks.onLift({
      clientSelection: pending.point,
      movementMode: 'FLUID',
    });
  }, [callbacks]);

  const startPendingDrag = useCallbackOne(
    (event: TouchEvent) => {
      invariant(!pendingRef.current, 'Expected there to be no pending drag');
github atlassian / react-beautiful-dnd / src / view / use-drag-handle / use-drag-handle.js View on Github external
draggableId,
    callbacks,
    getDraggableRef,
    getShouldRespectForcePress,
    canDragInteractiveElements,
  } = args;
  const lastArgsRef = usePreviousRef(args);

  useValidation(isEnabled, getDraggableRef);

  const getWindow = useCallbackOne(
    (): HTMLElement => getWindowFromEl(getDraggableRef()),
    [getDraggableRef],
  );

  const canStartCapturing = useCallbackOne(
    (event: Event) => {
      // Cannot lift when disabled
      if (!isEnabled) {
        return false;
      }
      // Something on this element might be capturing.
      // A drag might not have started yet
      // We want to prevent anything else from capturing
      if (capturingRef.current) {
        return false;
      }

      // Do not drag if anything else in the system is dragging
      if (!canLift(draggableId)) {
        return false;
      }
github atlassian / react-beautiful-dnd / src / view / use-drag-handle / use-drag-handle.js View on Github external
AppContext,
  );
  const {
    isDragging,
    isEnabled,
    draggableId,
    callbacks,
    getDraggableRef,
    getShouldRespectForcePress,
    canDragInteractiveElements,
  } = args;
  const lastArgsRef = usePreviousRef(args);

  useValidation(isEnabled, getDraggableRef);

  const getWindow = useCallbackOne(
    (): HTMLElement => getWindowFromEl(getDraggableRef()),
    [getDraggableRef],
  );

  const canStartCapturing = useCallbackOne(
    (event: Event) => {
      // Cannot lift when disabled
      if (!isEnabled) {
        return false;
      }
      // Something on this element might be capturing.
      // A drag might not have started yet
      // We want to prevent anything else from capturing
      if (capturingRef.current) {
        return false;
      }
github atlassian / react-beautiful-dnd / src / view / use-droppable-responders / use-droppable-responders.js View on Github external
[],
  );

  const unregister = useCallbackOne((id: DroppableId) => {
    delete cacheRef.current[id];
  }, []);

  const registration: DroppableResponderRegistration = useMemoOne(
    () => ({
      register,
      unregister,
    }),
    [register, unregister],
  );

  const getDroppableResponders = useCallbackOne((id: DroppableId) => {
    const getResponders: ?GetResponders = cacheRef.current[id];

    invariant(getResponders, `Could not find droppable reponder for id: ${id}`);

    return getResponders();
  }, []);

  const result: Result = useMemoOne(
    () => ({
      registration,
      getDroppableResponders,
    }),
    [registration, getDroppableResponders],
  );

  return result;
github atlassian / react-beautiful-dnd / src / view / use-drag-handle / use-drag-handle.js View on Github external
invariant(
      !capturingRef.current,
      'Cannot start capturing while something else is',
    );
    capturingRef.current = {
      abort,
    };
  }, []);
  const onCaptureEnd = useCallbackOne(() => {
    invariant(
      capturingRef.current,
      'Cannot stop capturing while nothing is capturing',
    );
    capturingRef.current = null;
  }, []);
  const abortCapture = useCallbackOne(() => {
    invariant(capturingRef.current, 'Cannot abort capture when there is none');
    capturingRef.current.abort();
  }, []);

  const { canLift, style: styleContext }: AppContextValue = useRequiredContext(
    AppContext,
  );
  const {
    isDragging,
    isEnabled,
    draggableId,
    callbacks,
    getDraggableRef,
    getShouldRespectForcePress,
    canDragInteractiveElements,
  } = args;
github atlassian / react-beautiful-dnd / src / view / use-drag-handle / sensor / use-touch-sensor.js View on Github external
const startDragging = useCallbackOne(() => {
    const pending: ?PendingDrag = pendingRef.current;
    invariant(pending, 'Cannot start a drag without a pending drag');

    isDraggingRef.current = true;
    pendingRef.current = null;
    hasMovedRef.current = false;

    callbacks.onLift({
      clientSelection: pending.point,
      movementMode: 'FLUID',
    });
  }, [callbacks]);

  const startPendingDrag = useCallbackOne(
    (event: TouchEvent) => {
      invariant(!pendingRef.current, 'Expected there to be no pending drag');
      const touch: Touch = event.touches[0];
      const { clientX, clientY } = touch;
      const point: Position = {
        x: clientX,
        y: clientY,
      };
      const longPressTimerId: TimeoutID = setTimeout(
        startDragging,
        timeForLongPress,
      );

      const pending: PendingDrag = {
        point,
        longPressTimerId,
github atlassian / react-beautiful-dnd / src / view / use-drag-handle / sensor / use-touch-sensor.js View on Github external
export default function useTouchSensor(args: Args): OnTouchStart {
  const {
    callbacks,
    getWindow,
    canStartCapturing,
    getShouldRespectForcePress,
    onCaptureStart,
    onCaptureEnd,
  } = args;
  const pendingRef = useRef(null);
  const isDraggingRef = useRef(false);
  const hasMovedRef = useRef(false);
  const unbindWindowEventsRef = useRef<() => void>(noop);
  const getIsCapturing = useCallbackOne(
    () => Boolean(pendingRef.current || isDraggingRef.current),
    [],
  );
  const postDragClickPreventer: EventPreventer = useMemoOne(
    () => createPostDragEventPreventer(getWindow),
    [getWindow],
  );

  const schedule = useMemoOne(() => {
    invariant(
      !getIsCapturing(),
      'Should not recreate scheduler while capturing',
    );
    return createScheduler(callbacks);
  }, [callbacks, getIsCapturing]);
github atlassian / react-beautiful-dnd / src / view / droppable / use-responders.js View on Github external
export default function useResponders(props: Props) {
  const { droppableId } = props;
  const previousRef = usePrevious(props);
  const context: AppContextValue = useRequiredContext(AppContext);
  const registration: DroppableResponderRegistration =
    context.droppableResponderRegistration;

  const getResponders = useCallbackOne(
    (): Responders => ({
      onBeforeDragStart: previousRef.onBeforeDragStart,
      onDragStart: previousRef.onDragStart,
      onDragUpdate: previousRef.onDragUpdate,
      onDragEnd: previousRef.onDragEnd,
    }),
    [],
  );

  useIsomorphicLayoutEffect(() => {
    registration.register(droppableId, getResponders);
    return () => registration.unregister(droppableId);
  }, [droppableId, getResponders, registration]);
}

use-memo-one

useMemo and useCallback but with a stable cache

MIT
Latest version published 2 years ago

Package Health Score

67 / 100
Full package analysis