export function createOnPointerUpdateCaller()

in packages/charts/src/chart_types/heatmap/state/selectors/on_pointer_update_caller.ts [44:95]


export function createOnPointerUpdateCaller(): (state: GlobalChartState) => void {
  let prevPointerEvent: PointerEvent | null = null;

  let selector: Selector<GlobalChartState, void> | null = null;
  return (state: GlobalChartState) => {
    if (selector === null && state.chartType === ChartType.Heatmap) {
      selector = createCustomCachedSelector(
        [
          getHeatmapSpecSelector,
          getSettingsSpecSelector,
          getActivePointerPosition,
          getPickedGridCell,
          getChartIdSelector,
        ],
        (spec, settings: SettingsSpec, currentPointer, gridCell, chartId): void => {
          if (!spec) {
            return;
          }

          if (prevPointerEvent === null) {
            prevPointerEvent = { chartId, type: PointerEventType.Out };
          }
          const tempPrev = { ...prevPointerEvent };
          const nextPointerEvent: PointerEvent = gridCell
            ? {
                chartId: state.chartId,
                type: currentPointer.x === -1 && currentPointer.y === -1 ? PointerEventType.Out : PointerEventType.Over,
                scale: spec.xScale.type,
                x: gridCell.x,
                y: [{ value: gridCell.y, groupId: '' }],
                smHorizontalValue: null,
                smVerticalValue: null,
              }
            : { chartId, type: PointerEventType.Out };
          // we have to update the prevPointerEvents before possibly calling the onPointerUpdate
          // to avoid a recursive loop of calls caused by the impossibility to update the prevPointerEvent
          prevPointerEvent = nextPointerEvent;

          if (
            settings.onPointerUpdate &&
            hasPointerEventChanged(tempPrev, nextPointerEvent, settings.pointerUpdateTrigger)
          ) {
            settings.onPointerUpdate(nextPointerEvent);
          }
        },
      );
    }
    if (selector) {
      selector(state);
    }
  };
}