-
Notifications
You must be signed in to change notification settings - Fork 8k
/
helpers.ts
68 lines (63 loc) · 2.13 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { Draft } from 'immer';
import produce from 'immer';
import type { Reducer, AnyAction } from 'redux';
import type { ActionCreator } from 'typescript-fsa';
import type { ReducerBuilder } from 'typescript-fsa-reducers';
import type { ResolverState, AnalyzerById } from '../types';
import { scaleToZoom } from './camera/scale_to_zoom';
import { analyzerReducer } from './reducer';
export const EMPTY_RESOLVER: ResolverState = {
data: {
currentRelatedEvent: {
loading: false,
data: null,
},
tree: {},
resolverComponentInstanceID: undefined,
indices: [],
detectedBounds: undefined,
},
camera: {
scalingFactor: scaleToZoom(1), // Defaulted to 1 to 1 scale
rasterSize: [0, 0],
translationNotCountingCurrentPanning: [0, 0],
latestFocusedWorldCoordinates: null,
animation: undefined,
panning: undefined,
},
ui: {
ariaActiveDescendant: null,
selectedNode: null,
},
};
/**
* Helper function to support use of immer within action creators.
* This allows reducers to be written in immer (direct mutation in appearance) over spread operators.
* More information on immer: https://immerjs.github.io/immer/
* @param actionCreator action creator
* @param handler reducer written in immer
* @returns reducer builder
*/
export function immerCase<S, P>(
actionCreator: ActionCreator<P>,
handler: (draft: Draft<S>, payload: P) => void
): (reducer: ReducerBuilder<S>) => ReducerBuilder<S> {
return (reducer) =>
reducer.case(actionCreator, (state, payload) =>
produce(state, (draft) => handler(draft, payload))
);
}
export const initialAnalyzerState: AnalyzerById = {};
export function mockReducer(id: string): Reducer<AnalyzerById, AnyAction> {
const testReducer: Reducer<AnalyzerById, AnyAction> = (
state = { [id]: EMPTY_RESOLVER },
action
): AnalyzerById => analyzerReducer(state, action);
return testReducer;
}