Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(external pointer): avoid recursive-loops on pointer events #503

Conversation

markov00
Copy link
Member

@markov00 markov00 commented Dec 27, 2019

Summary

If the consumer code doesn't take care of correctly distribute the pointer events avoiding feeding a chart with its own cursor position, it will end up with a recursive loop that ultimately throw a max stack trace. This provide the fix for such case.

BREAKING CHANGE: The onCursorUpdate Settings property is changed to a more generic
onPointerUpdate. The same apply for the event type CursorEvent that is now PointerEvent and can assume a PointerOverEvent or PointOutEvent shape (see TS types)

To test on previous version:

import React from 'react';
import { Chart, Settings, TooltipType, AreaSeries, PointerEvent } from '../src';
import { KIBANA_METRICS } from '../src/utils/data_samples/test_dataset_kibana';
export class Playground extends React.Component {
  chartRef: React.RefObject<Chart> = React.createRef();
  chartRef2: React.RefObject<Chart> = React.createRef();
  onPointerUpdate = (event: PointerEvent) => {
    if (this.chartRef && this.chartRef.current) {
      this.chartRef.current.dispatchExternalPointerEvent(event);
    }
    if (this.chartRef2 && this.chartRef2.current) {
      this.chartRef2.current.dispatchExternalPointerEvent(event);
    }
  };
  render() {
    return (
      <>
        <button onClick={this.onSnapshot}>Snapshot</button>
        <div className="chart">
          <Chart ref={this.chartRef}>
            <Settings
              tooltip={{ type: TooltipType.VerticalCursor }}
              showLegend
              onPointerUpdate={this.onPointerUpdate}
            />
            <AreaSeries
              id="lines"
              xAccessor={0}
              yAccessors={[1]}
              stackAccessors={[0]}
              data={KIBANA_METRICS.metrics.kibana_os_load[0].data.slice(0, 5)}
            />
          </Chart>
        </div>
        <div className="chart">
          <Chart ref={this.chartRef2}>
            <Settings
              tooltip={{ type: TooltipType.VerticalCursor }}
              showLegend
              onPointerUpdate={this.onPointerUpdate}
            />
            <AreaSeries
              id="lines"
              xAccessor={0}
              yAccessors={[1]}
              stackAccessors={[0]}
              data={KIBANA_METRICS.metrics.kibana_os_load[0].data.slice(0, 5)}
            />
          </Chart>
        </div>
      </>
    );
  }
}

fix #504

Checklist

Use strikethroughs to remove checklist items you don't feel are applicable to this PR.

  • Any consumer-facing exports were added to src/index.ts (and stories only import from ../src except for test data & storybook)
  • This was checked for cross-browser compatibility, including a check against IE11
  • [ ] Proper documentation or storybook story was added for features that require explanation or tutorials
  • Unit tests were updated or added to match the most common scenarios
  • Each commit follows the convention

If the consumer code doesn't take care of correctly distribute the pointer events avoiding feeding a
chart with its own cursor position, it will end up with a recursive loop that ultimately throw a max
stack trace. This provide the fix for such case

BREAKING CHANGE: The `onCursorUpdate` Settings property is changed to a more generic
`onPointerUpdate`. The same apply for the event type `CursorEvent` that is now `PointerEvent` and
can assume a `PointerOverEvent` or `PointOutEvent` shape (see TS types)
@codecov-io
Copy link

codecov-io commented Dec 27, 2019

Codecov Report

Merging #503 into master will increase coverage by 0.7%.
The diff coverage is 94.73%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #503     +/-   ##
=========================================
+ Coverage   83.84%   84.55%   +0.7%     
=========================================
  Files         158      169     +11     
  Lines        4729     4887    +158     
  Branches      963      983     +20     
=========================================
+ Hits         3965     4132    +167     
+ Misses        749      739     -10     
- Partials       15       16      +1
Impacted Files Coverage Δ
src/specs/settings.tsx 100% <100%> (ø) ⬆️
src/components/chart.tsx 77.01% <100%> (ø) ⬆️
...hart/state/selectors/get_elements_at_cursor_pos.ts 95% <100%> (+15%) ⬆️
src/state/actions/events.ts 100% <100%> (+33.33%) ⬆️
..._types/xy_chart/state/selectors/get_cursor_band.ts 90.32% <100%> (+12.9%) ⬆️
src/utils/events.ts 100% <100%> (+50%) ⬆️
src/state/chart_state.ts 85.71% <100%> (+6.12%) ⬆️
.../selectors/get_tooltip_values_highlighted_geoms.ts 90.9% <100%> (+3.4%) ⬆️
...xy_chart/state/selectors/on_pointer_move_caller.ts 95.23% <88.23%> (+37.09%) ⬆️
src/mocks/series/series.ts 81.96% <0%> (ø)
... and 16 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 48db8c8...3e1ba7a. Read the comment docs.

@markov00 markov00 merged commit c170f0d into elastic:master Jan 2, 2020
@markov00 markov00 deleted the 2019-12-27_avoid-circular-loops-pointer-events branch January 2, 2020 09:09
markov00 pushed a commit that referenced this pull request Jan 2, 2020
# [16.0.0](v15.0.5...v16.0.0) (2020-01-02)

### Bug Fixes

* **external pointer:** avoid recursive-loops on pointer events ([#503](#503)) ([c170f0d](c170f0d)), closes [#504](#504)

### BREAKING CHANGES

* **external pointer:** The `onCursorUpdate` Settings property is changed to a more generic
`onPointerUpdate`. The same apply for the event type `CursorEvent` that is now `PointerEvent` and can assume a `PointerOverEvent` or `PointOutEvent` shape (see TS types)
@markov00
Copy link
Member Author

markov00 commented Jan 2, 2020

🎉 This PR is included in version 16.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@markov00 markov00 added the released Issue released publicly label Jan 2, 2020
AMoo-Miki pushed a commit to AMoo-Miki/OpenSearch-Dashboards that referenced this pull request Feb 10, 2022
# [16.0.0](elastic/elastic-charts@v15.0.5...v16.0.0) (2020-01-02)

### Bug Fixes

* **external pointer:** avoid recursive-loops on pointer events ([opensearch-project#503](elastic/elastic-charts#503)) ([8f9c258](elastic/elastic-charts@8f9c258)), closes [opensearch-project#504](elastic/elastic-charts#504)

### BREAKING CHANGES

* **external pointer:** The `onCursorUpdate` Settings property is changed to a more generic
`onPointerUpdate`. The same apply for the event type `CursorEvent` that is now `PointerEvent` and can assume a `PointerOverEvent` or `PointOutEvent` shape (see TS types)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released Issue released publicly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Maximum call stack size exceeded on shared pointer position
2 participants