Skip to content

Commit

Permalink
Refactor: spike to get rid of select 3
Browse files Browse the repository at this point in the history
  • Loading branch information
monfera committed Feb 15, 2019
1 parent 67e6db3 commit 26d878c
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ export const WorkpadPage = compose(
};
}),
withState('aeroelastic', 'setAeroelastic', props => {
console.log('initializing state');
const shapes = props.elements
.map(elementToShape)
.filter((d, i, a) => !d.id.startsWith('group') || a.find(s => s.parent === d.id));
Expand Down
7 changes: 7 additions & 0 deletions x-pack/plugins/canvas/public/lib/aeroelastic/dagStart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Selectors directly from a state object
*
* (we could turn gesture.js into a factory, with this state root - primaryUpdate - being passed...)
*/

export const state = d => d
6 changes: 4 additions & 2 deletions x-pack/plugins/canvas/public/lib/aeroelastic/gestures.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { select } from './select';
import { state } from './dagStart';
import { getScene } from './layout_functions';

// Only needed to shuffle some modifier keys for Apple keyboards as per vector editing software conventions,
Expand All @@ -25,7 +26,8 @@ const appleKeyboard = Boolean(
* (we could turn gesture.js into a factory, with this state root - primaryUpdate - being passed...)
*/

const primaryUpdate = state => state.primaryUpdate;
const scene = select(getScene)(state);
const primaryUpdate = select(state => state.primaryUpdate)(state);

const gestureStatePrev = select(
scene =>
Expand All @@ -37,7 +39,7 @@ const gestureStatePrev = select(
mouseIsDown: false,
mouseButtonState: { buttonState: 'up', downX: null, downY: null },
}
)(getScene);
)(scene);

export const gestureEnd = select(
action =>
Expand Down
15 changes: 9 additions & 6 deletions x-pack/plugins/canvas/public/lib/aeroelastic/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import {
dragging,
dragVector,
gestureEnd,
gestureState,
metaHeld,
mouseButton,
mouseDowned,
mouseIsDown,
optionHeld,
shiftHeld,
gestureState,
} from './gestures';

import {
Expand Down Expand Up @@ -67,26 +67,29 @@ import {
primaryUpdate,
resizeAnnotationsFunction,
} from './layout_functions';
import { state } from './dagStart';

/**
* Scenegraph update based on events, gestures...
*/

export const shapes = select(getShapes)(getScene);
const scene = select(getScene)(state);

export const shapes = select(getShapes)(scene);

const hoveredShapes = select(getHoveredShapes)(configuration, shapes, cursorPosition);

const hoveredShape = select(getHoveredShape)(hoveredShapes);

const draggedShape = select(draggingShape)(getScene, hoveredShape, mouseIsDown, mouseDowned);
const draggedShape = select(draggingShape)(scene, hoveredShape, mouseIsDown, mouseDowned);

export const focusedShape = select(getFocusedShape)(draggedShape, hoveredShape);

const alterSnapGesture = select(getAlterSnapGesture)(metaHeld);

const multiselectModifier = shiftHeld; // todo abstract out keybindings

const mouseTransformGesturePrev = select(getMouseTransformGesturePrev)(getScene);
const mouseTransformGesturePrev = select(getMouseTransformGesturePrev)(scene);

const mouseTransformState = select(getMouseTransformState)(
mouseTransformGesturePrev,
Expand All @@ -105,9 +108,9 @@ const dragBox = select(getDragBox)(dragging, draggedShape, dragVector);
// directSelect is an API entry point (via the `shapeSelect` action) that lets the client directly specify what thing
const directSelect = select(getDirectSelect)(primaryUpdate);

const selectedShapeObjects = select(getSelectedShapeObjects)(getScene);
const selectedShapeObjects = select(getSelectedShapeObjects)(scene);

const selectedShapesPrev = select(getSelectedShapesPrev)(getScene);
const selectedShapesPrev = select(getSelectedShapesPrev)(scene);

const boxHovered = select(getDragBoxHovered)(dragBox, shapes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { getId } from './../../lib/get_id';
import { landmarkPoint, shapesAt, insideAABB } from './geometry';
import { insideAABB, landmarkPoint, shapesAt } from './geometry';

import {
compositeComponent,
Expand Down Expand Up @@ -384,7 +384,6 @@ const shapeApplyLocalTransforms = intents => shape => {
.filter(identity)
);

if(!shape.localTransformMatrix) debugger
const baselineLocalTransformMatrix = multiply(
shape.baselineLocalTransformMatrix || shape.localTransformMatrix,
...transformIntents
Expand All @@ -394,7 +393,6 @@ const shapeApplyLocalTransforms = intents => shape => {
const localTransformMatrix = cumulativeTransformIntents.length
? multiply(baselineLocalTransformMatrix, cumulativeTransformIntentMatrix)
: baselineLocalTransformMatrix;
if(!localTransformMatrix) debugger
const cumulativeSizeIntentMatrix = multiply2d(...cumulativeSizeIntents);
const sizeVector = mvMultiply2d(
cumulativeSizeIntents.length
Expand Down
25 changes: 6 additions & 19 deletions x-pack/plugins/canvas/public/lib/aeroelastic/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,14 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { ActionId, NodeFunction, NodeResult } from './types';
import { ActionId, NodeFunc, NodeResult } from './types';

const selectDebug = (fun: NodeFunction): NodeFunction => (
...inputs: NodeFunction[]
): NodeResult => (state: NodeResult) => fun(...inputs.map(input => input(state)));

const selectFast = (fun: NodeFunction): NodeFunction => (...inputs: NodeFunction[]): NodeResult => {
// last-value memoizing version of this single line function:
// fun => (...inputs) => state => fun(...inputs.map(input => input(state)))
let value: NodeResult;
let actionId: ActionId;
export const select = (fun: NodeFunc): NodeFunc => (...inputs: NodeFunc[]): NodeResult => {
let { value, actionId } = { value: null as NodeResult, actionId: NaN as ActionId };
return (state: NodeResult) => {
const lastActionId: ActionId = state.primaryUpdate.payload.uid;
if (actionId === lastActionId) {
return value;
}

value = fun(...inputs.map(input => input(state)));
actionId = lastActionId;
const previousActionId: ActionId = state.primaryUpdate.payload.uid;
value = actionId === previousActionId ? value : fun.apply(0, inputs.map(input => input(state)));
actionId = previousActionId;
return value;
};
};

export const select = selectFast;
5 changes: 1 addition & 4 deletions x-pack/plugins/canvas/public/lib/aeroelastic/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ export type transformMatrix2d = [f, f, f, f, f, f, f, f, f] &
export type transformMatrix3d = [f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f] &
ReadonlyArray<f> & { __nominal: 'transformMatrix3d' };

export interface Meta {
silent: boolean;
}
export type ActionId = number;
export type TypeName = string;
export type NodeResult = any;
export type NodeFunction = (...args: any[]) => any;
export type NodeFunc = (...args: any[]) => any;

0 comments on commit 26d878c

Please sign in to comment.