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: do not call dateRangeChange in live mode #220

Merged
merged 1 commit into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/synchro-charts/src/components/sc-table/sc-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isThreshold } from '../charts/common/annotations/utils';
import { Trend } from '../charts/common/trends/types';
import { Annotations, ChartConfig, Threshold } from '../charts/common/types';
import { constructTableData, Row } from './constructTableData';
import { viewportEndDate, viewportStartDate } from '../../utils/viewPort';
import { viewportEndDate, viewportStartDate, isInLiveMode } from '../../utils/viewPort';
import { isMinimalStaticViewport } from '../../utils/predicates';
import { parseDuration } from '../../utils/time';
import { webGLRenderer } from '../sc-webgl-context/webglContext';
Expand Down Expand Up @@ -53,8 +53,8 @@ export class ScTable implements ChartConfig {
const hasViewPortChanged =
viewportStartDate(this.viewport).getTime() !== start.getTime() ||
viewportEndDate(this.viewport).getTime() !== end.getTime();
const isInLiveMode = Boolean(duration);
if (hasViewPortChanged && !isInLiveMode) {
const inLiveMode = isInLiveMode(this.viewport);
if (hasViewPortChanged && !inLiveMode) {
this.onDateRangeChange([start, end, this.viewport.group]);
}
// Update active viewport
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { getThresholds } from '../charts/common/annotations/utils';
import { breachedThreshold } from '../charts/common/annotations/breachedThreshold';
import { streamPairs } from '../../utils/streamPairs';
import { RenderCell } from './types';
import { viewportEndDate, viewportStartDate } from '../../utils/viewPort';
import { viewportEndDate, viewportStartDate, isInLiveMode } from '../../utils/viewPort';
import { Annotations, ChartConfig, Threshold, WidgetConfigurationUpdate } from '../charts/common/types';
import { LabelsConfig } from '../common/types';
import { DATA_ALIGNMENT } from '../charts/common/constants';
Expand Down Expand Up @@ -120,8 +120,8 @@ export class ScWidgetGrid implements ChartConfig {
const hasViewPortChanged =
viewportStartDate(this.viewport).getTime() !== start.getTime() ||
viewportEndDate(this.viewport).getTime() !== end.getTime();
const isInLiveMode = Boolean(duration);
if (hasViewPortChanged && !isInLiveMode) {
const inLiveMode = isInLiveMode(this.viewport);
if (hasViewPortChanged && !inLiveMode) {
this.onDateRangeChange([start, end, this.viewport.group]);
}
// Update active viewport
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import uuid from 'uuid/v4';

import { ViewportHandler } from './viewportHandler';
import { ViewPortManager } from './types';
import { SECOND_IN_MS } from '../../utils/time';
import { SECOND_IN_MS, MINUTE_IN_MS } from '../../utils/time';
import { SizeConfig } from '../../utils/dataTypes';

const viewportManager = (viewportGroup?: string): ViewPortManager => ({
Expand Down Expand Up @@ -159,7 +159,7 @@ describe('syncing managers', () => {

/** manager added to existing view port group that has had it's viewport synced should have it's viewport synced to the group */
expect(manager2.updateViewPort).toBeCalledTimes(1);
expect(manager2.updateViewPort).toBeCalledWith({ start: START, end: END });
expect(manager2.updateViewPort).toBeCalledWith(expect.objectContaining({ start: START, end: END }));
});

it('will not update viewport when syncUpdate is false', () => {
Expand Down Expand Up @@ -409,4 +409,22 @@ describe('internal clock', () => {
})
);
});

it('blocks dateRangeChanged event emission when viewport group with duration updated', () => {
const groups = new ViewportHandler();
const VIEWPORT_GROUP_1 = 'view-port-group-1';
const manager = viewportManager(VIEWPORT_GROUP_1);
const duration = 10 * MINUTE_IN_MS;

/** Create a viewport group and sync it's viewport */
groups.add({ manager, duration: MINUTE_IN_MS, chartSize });
groups.add({ manager, duration, chartSize });

expect(manager.updateViewPort).toBeCalledWith(
expect.objectContaining({
duration,
shouldBlockDateRangeChangedEvent: true,
})
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ export class ViewportHandler<T extends ViewPortManager> {
* the current viewport groups time span.
*/
if (manager.viewportGroup && this.viewportMap[manager.viewportGroup] && shouldSync) {
manager.updateViewPort(this.viewportMap[manager.viewportGroup]);
const shouldBlockDateRangeChangedEvent = Boolean(duration);
manager.updateViewPort({
...this.viewportMap[manager.viewportGroup],
duration,
shouldBlockDateRangeChangedEvent,
});
}
// If duration is not null, this means that we want to have live mode
if (duration != null) {
Expand Down
12 changes: 11 additions & 1 deletion packages/synchro-charts/src/utils/viewPort.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DAY_IN_MS } from './time';
import { viewportEndDate, viewportStartDate } from './viewPort';
import { viewportEndDate, viewportStartDate, isInLiveMode } from './viewPort';

const mockCurrentTime = (mockedDate: Date) => {
// @ts-ignore
Expand Down Expand Up @@ -31,3 +31,13 @@ describe('viewportEnd', () => {
expect(viewportEndDate({ duration: DAY_IN_MS })).toEqual(TIME);
});
});

describe('isInLiveMode', () => {
it('returns true when in live mode', () => {
expect(isInLiveMode({ duration: 60000 })).toBeTrue();
});

it('returns false when viewport is static', () => {
expect(isInLiveMode({ start: new Date(), end: new Date() })).toBeFalse();
});
});
4 changes: 4 additions & 0 deletions packages/synchro-charts/src/utils/viewPort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ export const viewportStartDate = (viewportConfig: MinimalViewPortConfig): Date =
export const viewportEndDate = (viewportConfig: MinimalViewPortConfig): Date => {
return isMinimalStaticViewport(viewportConfig) ? new Date(viewportConfig.end) : new Date(Date.now());
};

export const isInLiveMode = (viewport: MinimalViewPortConfig): boolean => {
return !isMinimalStaticViewport(viewport) && Boolean(viewport.duration);
};