Commit ac2125c5 authored by Vasily Belolapotkov's avatar Vasily Belolapotkov

refactor: separate chart state and controller

parent 826ccb62
import {
ChartEventListener,
IChartState,
IChartStateController,
IDataSet,
} from './types';
export function makeChartStateController(
chartState: IChartState,
): IChartStateController {
const state = chartState;
const listeners: Map<string, ChartEventListener[]> = new Map();
return Object.freeze({
getState,
changeXViewRange,
toggleDataSetVisibility,
addListener,
showInspector,
hideInspector,
toggleInspector,
moveInspector,
});
function getState() {
return state;
}
function addListener(eventName: string, listener: ChartEventListener) {
const eventListeners = listeners.get(eventName) || [];
eventListeners.push(listener);
listeners.set(eventName, eventListeners);
}
function fireEvent(eventName: string) {
const eventListeners = listeners.get(eventName) || [];
eventListeners.forEach(listener => listener(state));
}
function changeXViewRange(xScale?: number, xOffset?: number) {
const xScaleChanged = typeof xScale === 'number' && state.xScale !== xScale;
const xOffsetChanged =
typeof xOffset === 'number' && state.xOffset !== xOffset;
if (!xScaleChanged && !xOffsetChanged) {
return;
}
const nextXScale = xScaleChanged ? xScale : state.xScale;
const nextXOffset = xOffsetChanged ? xOffset : state.xOffset;
state.setXViewRange(nextXScale, nextXOffset);
hideInspector();
fireEvent('xViewRangeChanged');
fireEvent('yViewRangeChanged');
}
function toggleDataSetVisibility(dataSet: IDataSet) {
state.toggleDataSetVisibility(dataSet);
state.setInspectorVisibility(false);
hideInspector();
fireEvent('yViewRangeChanged');
fireEvent('dataSetVisibilityChanged');
}
function showInspector() {
state.setInspectorVisibility(true);
fireEvent('inspectorVisibilityChanged');
}
function hideInspector() {
state.setInspectorVisibility(false);
fireEvent('inspectorVisibilityChanged');
}
function toggleInspector() {
state.setInspectorVisibility(!state.inspectorIsActive);
fireEvent('inspectorVisibilityChanged');
}
function moveInspector(xOffset: number) {
state.moveInspector(xOffset);
state.setInspectorVisibility(true);
fireEvent('moveInspector');
}
}
import {
IChartData,
IChartState,
IChartStateController,
IDataSet,
IInspectorValue,
IInspectorValues,
......@@ -176,83 +175,3 @@ export function makeChartState(chartData: IChartData): IChartState {
return { xValue, yValues };
}
}
export type ChartEventListener = (chartState: IChartState) => void;
export function makeChartStateController(
chartState: IChartState,
): IChartStateController {
const state = chartState;
const listeners: Map<string, ChartEventListener[]> = new Map();
return Object.freeze({
getState,
changeXViewRange,
toggleDataSetVisibility,
addListener,
showInspector,
hideInspector,
toggleInspector,
moveInspector,
});
function getState() {
return state;
}
function addListener(eventName: string, listener: ChartEventListener) {
const eventListeners = listeners.get(eventName) || [];
eventListeners.push(listener);
listeners.set(eventName, eventListeners);
}
function fireEvent(eventName: string) {
const eventListeners = listeners.get(eventName) || [];
eventListeners.forEach(listener => listener(state));
}
function changeXViewRange(xScale?: number, xOffset?: number) {
const xScaleChanged = typeof xScale === 'number' && state.xScale !== xScale;
const xOffsetChanged =
typeof xOffset === 'number' && state.xOffset !== xOffset;
if (!xScaleChanged && !xOffsetChanged) {
return;
}
const nextXScale = xScaleChanged ? xScale : state.xScale;
const nextXOffset = xOffsetChanged ? xOffset : state.xOffset;
state.setXViewRange(nextXScale, nextXOffset);
hideInspector();
fireEvent('xViewRangeChanged');
fireEvent('yViewRangeChanged');
}
function toggleDataSetVisibility(dataSet: IDataSet) {
state.toggleDataSetVisibility(dataSet);
state.setInspectorVisibility(false);
hideInspector();
fireEvent('yViewRangeChanged');
fireEvent('dataSetVisibilityChanged');
}
function showInspector() {
state.setInspectorVisibility(true);
fireEvent('inspectorVisibilityChanged');
}
function hideInspector() {
state.setInspectorVisibility(false);
fireEvent('inspectorVisibilityChanged');
}
function toggleInspector() {
state.setInspectorVisibility(!state.inspectorIsActive);
fireEvent('inspectorVisibilityChanged');
}
function moveInspector(xOffset: number) {
state.moveInspector(xOffset);
state.setInspectorVisibility(true);
fireEvent('moveInspector');
}
}
import { makeHtmlElement } from '../utils/ui';
import { makeChartPreview } from './chart-preview';
import { makeChartState, makeChartStateController } from './chart-state';
import { IChartData, IChartComponent } from './types';
import { makeChartState } from './chart-state';
import { makePlotArea } from './plot-area';
import { makeChartPreview } from './chart-preview';
import { makeLegend } from './legend';
import { makeHtmlElement } from '../utils/ui';
import { makeChartStateController } from './chart-state-controller';
export function makeChart(chartData: IChartData): IChartComponent<any> {
const chartState = makeChartState(chartData);
const chartStateController = makeChartStateController(chartState);
......
import { ChartEventListener } from './chart-state';
export type Timestamp = number;
export enum ChartType {
......@@ -87,6 +85,8 @@ export interface IChartStateController {
moveInspector: (xOffset: number) => void;
}
export type ChartEventListener = (chartState: IChartState) => void;
export type ChartComponentFactory<TProps> = (
stateController: IChartStateController,
) => IChartComponent<TProps>;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment