refactor(connectionContext): memoize context
This commit is contained in:
Vendored
+15
-20
@@ -27933,13 +27933,15 @@
|
|||||||
position = _useState4[0],
|
position = _useState4[0],
|
||||||
setPosition = _useState4[1];
|
setPosition = _useState4[1];
|
||||||
|
|
||||||
var connectionContext = {
|
var connectionContext = React.useMemo(function () {
|
||||||
sourceId: sourceId,
|
return {
|
||||||
setSourceId: setSourceId,
|
sourceId: sourceId,
|
||||||
position: position,
|
setSourceId: setSourceId,
|
||||||
setPosition: setPosition,
|
position: position,
|
||||||
onConnect: onConnect
|
setPosition: setPosition,
|
||||||
};
|
onConnect: onConnect
|
||||||
|
};
|
||||||
|
}, [sourceId, position]);
|
||||||
return React__default.createElement(ConnectionContext.Provider, {
|
return React__default.createElement(ConnectionContext.Provider, {
|
||||||
value: connectionContext
|
value: connectionContext
|
||||||
}, children);
|
}, children);
|
||||||
@@ -33088,8 +33090,7 @@
|
|||||||
onConnect = _ref2.onConnect,
|
onConnect = _ref2.onConnect,
|
||||||
setSourceId = _ref2.setSourceId,
|
setSourceId = _ref2.setSourceId,
|
||||||
setPosition = _ref2.setPosition,
|
setPosition = _ref2.setPosition,
|
||||||
_ref2$className = _ref2.className,
|
className = _ref2.className,
|
||||||
className = _ref2$className === void 0 ? null : _ref2$className,
|
|
||||||
rest = _objectWithoutProperties(_ref2, ["source", "target", "nodeId", "onConnect", "setSourceId", "setPosition", "className"]);
|
rest = _objectWithoutProperties(_ref2, ["source", "target", "nodeId", "onConnect", "setSourceId", "setPosition", "className"]);
|
||||||
|
|
||||||
var handleClasses = classnames('react-graph__handle', className, {
|
var handleClasses = classnames('react-graph__handle', className, {
|
||||||
@@ -33134,7 +33135,7 @@
|
|||||||
}, props));
|
}, props));
|
||||||
});
|
});
|
||||||
TargetHandle.displayName = 'TargetHandle';
|
TargetHandle.displayName = 'TargetHandle';
|
||||||
TargetHandle.whyDidYouRender = false;
|
TargetHandle.whyDidYouRender = true;
|
||||||
|
|
||||||
var SourceHandle = React.memo(function (props) {
|
var SourceHandle = React.memo(function (props) {
|
||||||
var nodeId = React.useContext(NodeIdContext);
|
var nodeId = React.useContext(NodeIdContext);
|
||||||
@@ -33339,12 +33340,6 @@
|
|||||||
x: xPos,
|
x: xPos,
|
||||||
y: yPos
|
y: yPos
|
||||||
};
|
};
|
||||||
|
|
||||||
var _transform = _slicedToArray(transform, 3),
|
|
||||||
x = _transform[0],
|
|
||||||
y = _transform[1],
|
|
||||||
k = _transform[2];
|
|
||||||
|
|
||||||
var nodeClasses = classnames('react-graph__node', {
|
var nodeClasses = classnames('react-graph__node', {
|
||||||
selected: selected
|
selected: selected
|
||||||
});
|
});
|
||||||
@@ -33354,11 +33349,11 @@
|
|||||||
};
|
};
|
||||||
React.useEffect(function () {
|
React.useEffect(function () {
|
||||||
var bounds = nodeElement.current.getBoundingClientRect();
|
var bounds = nodeElement.current.getBoundingClientRect();
|
||||||
var unscaledWith = Math.round(bounds.width * (1 / k));
|
var unscaledWith = Math.round(bounds.width * (1 / transform[2]));
|
||||||
var unscaledHeight = Math.round(bounds.height * (1 / k));
|
var unscaledHeight = Math.round(bounds.height * (1 / transform[2]));
|
||||||
var handleBounds = {
|
var handleBounds = {
|
||||||
source: getHandleBounds('.source', nodeElement.current, bounds, k),
|
source: getHandleBounds('.source', nodeElement.current, bounds, transform[2]),
|
||||||
target: getHandleBounds('.target', nodeElement.current, bounds, k)
|
target: getHandleBounds('.target', nodeElement.current, bounds, transform[2])
|
||||||
};
|
};
|
||||||
dispatch(updateNodeData(id, {
|
dispatch(updateNodeData(id, {
|
||||||
width: unscaledWith,
|
width: unscaledWith,
|
||||||
|
|||||||
@@ -35710,13 +35710,15 @@ var Provider = (0, _react.memo)(function (_ref) {
|
|||||||
position = _useState4[0],
|
position = _useState4[0],
|
||||||
setPosition = _useState4[1];
|
setPosition = _useState4[1];
|
||||||
|
|
||||||
var connectionContext = {
|
var connectionContext = (0, _react.useMemo)(function () {
|
||||||
sourceId: sourceId,
|
return {
|
||||||
setSourceId: setSourceId,
|
sourceId: sourceId,
|
||||||
position: position,
|
setSourceId: setSourceId,
|
||||||
setPosition: setPosition,
|
position: position,
|
||||||
onConnect: onConnect
|
setPosition: setPosition,
|
||||||
};
|
onConnect: onConnect
|
||||||
|
};
|
||||||
|
}, [sourceId, position]);
|
||||||
return _react.default.createElement(ConnectionContext.Provider, {
|
return _react.default.createElement(ConnectionContext.Provider, {
|
||||||
value: connectionContext
|
value: connectionContext
|
||||||
}, children);
|
}, children);
|
||||||
@@ -41346,8 +41348,7 @@ var BaseHandle = (0, _react.memo)(function (_ref2) {
|
|||||||
onConnect = _ref2.onConnect,
|
onConnect = _ref2.onConnect,
|
||||||
setSourceId = _ref2.setSourceId,
|
setSourceId = _ref2.setSourceId,
|
||||||
setPosition = _ref2.setPosition,
|
setPosition = _ref2.setPosition,
|
||||||
_ref2$className = _ref2.className,
|
className = _ref2.className,
|
||||||
className = _ref2$className === void 0 ? null : _ref2$className,
|
|
||||||
rest = _objectWithoutProperties(_ref2, ["source", "target", "nodeId", "onConnect", "setSourceId", "setPosition", "className"]);
|
rest = _objectWithoutProperties(_ref2, ["source", "target", "nodeId", "onConnect", "setSourceId", "setPosition", "className"]);
|
||||||
|
|
||||||
var handleClasses = (0, _classnames.default)('react-graph__handle', className, {
|
var handleClasses = (0, _classnames.default)('react-graph__handle', className, {
|
||||||
@@ -41429,7 +41430,7 @@ var TargetHandle = (0, _react.memo)(function (props) {
|
|||||||
}, props));
|
}, props));
|
||||||
});
|
});
|
||||||
TargetHandle.displayName = 'TargetHandle';
|
TargetHandle.displayName = 'TargetHandle';
|
||||||
TargetHandle.whyDidYouRender = false;
|
TargetHandle.whyDidYouRender = true;
|
||||||
var _default = TargetHandle;
|
var _default = TargetHandle;
|
||||||
exports.default = _default;
|
exports.default = _default;
|
||||||
},{"react":"../node_modules/react/index.js","./BaseHandle":"../src/NodeRenderer/HandleTypes/BaseHandle.js","../../ConnectionContext":"../src/ConnectionContext/index.js","../NodeIdContext":"../src/NodeRenderer/NodeIdContext.js"}],"../src/NodeRenderer/HandleTypes/SourceHandle.js":[function(require,module,exports) {
|
},{"react":"../node_modules/react/index.js","./BaseHandle":"../src/NodeRenderer/HandleTypes/BaseHandle.js","../../ConnectionContext":"../src/ConnectionContext/index.js","../NodeIdContext":"../src/NodeRenderer/NodeIdContext.js"}],"../src/NodeRenderer/HandleTypes/SourceHandle.js":[function(require,module,exports) {
|
||||||
@@ -41758,12 +41759,6 @@ var _default = function _default(NodeComponent) {
|
|||||||
x: xPos,
|
x: xPos,
|
||||||
y: yPos
|
y: yPos
|
||||||
};
|
};
|
||||||
|
|
||||||
var _transform = _slicedToArray(transform, 3),
|
|
||||||
x = _transform[0],
|
|
||||||
y = _transform[1],
|
|
||||||
k = _transform[2];
|
|
||||||
|
|
||||||
var nodeClasses = (0, _classnames.default)('react-graph__node', {
|
var nodeClasses = (0, _classnames.default)('react-graph__node', {
|
||||||
selected: selected
|
selected: selected
|
||||||
});
|
});
|
||||||
@@ -41773,11 +41768,11 @@ var _default = function _default(NodeComponent) {
|
|||||||
};
|
};
|
||||||
(0, _react.useEffect)(function () {
|
(0, _react.useEffect)(function () {
|
||||||
var bounds = nodeElement.current.getBoundingClientRect();
|
var bounds = nodeElement.current.getBoundingClientRect();
|
||||||
var unscaledWith = Math.round(bounds.width * (1 / k));
|
var unscaledWith = Math.round(bounds.width * (1 / transform[2]));
|
||||||
var unscaledHeight = Math.round(bounds.height * (1 / k));
|
var unscaledHeight = Math.round(bounds.height * (1 / transform[2]));
|
||||||
var handleBounds = {
|
var handleBounds = {
|
||||||
source: getHandleBounds('.source', nodeElement.current, bounds, k),
|
source: getHandleBounds('.source', nodeElement.current, bounds, transform[2]),
|
||||||
target: getHandleBounds('.target', nodeElement.current, bounds, k)
|
target: getHandleBounds('.target', nodeElement.current, bounds, transform[2])
|
||||||
};
|
};
|
||||||
dispatch((0, _actions.updateNodeData)(id, {
|
dispatch((0, _actions.updateNodeData)(id, {
|
||||||
width: unscaledWith,
|
width: unscaledWith,
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -1,4 +1,4 @@
|
|||||||
import React, { createContext, useState, memo } from 'react';
|
import React, { createContext, useState, memo, useMemo } from 'react';
|
||||||
|
|
||||||
export const ConnectionContext = createContext({});
|
export const ConnectionContext = createContext({});
|
||||||
|
|
||||||
@@ -6,13 +6,13 @@ export const Provider = memo(({ onConnect, children }) => {
|
|||||||
const [sourceId, setSourceId] = useState(null);
|
const [sourceId, setSourceId] = useState(null);
|
||||||
const [position, setPosition] = useState({ x:0, y: 0 });
|
const [position, setPosition] = useState({ x:0, y: 0 });
|
||||||
|
|
||||||
const connectionContext = {
|
const connectionContext = useMemo(() => ({
|
||||||
sourceId,
|
sourceId,
|
||||||
setSourceId,
|
setSourceId,
|
||||||
position,
|
position,
|
||||||
setPosition,
|
setPosition,
|
||||||
onConnect
|
onConnect
|
||||||
};
|
}), [sourceId, position]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ConnectionContext.Provider value={connectionContext}>
|
<ConnectionContext.Provider value={connectionContext}>
|
||||||
|
|||||||
@@ -39,7 +39,10 @@ function onMouseDown(evt, { nodeId, setSourceId, setPosition, onConnect, isTarg
|
|||||||
document.addEventListener('mouseup', onMouseUp)
|
document.addEventListener('mouseup', onMouseUp)
|
||||||
}
|
}
|
||||||
|
|
||||||
const BaseHandle = memo(({ source, target, nodeId, onConnect, setSourceId, setPosition, className = null, ...rest }) => {
|
const BaseHandle = memo(({
|
||||||
|
source, target, nodeId, onConnect,
|
||||||
|
setSourceId, setPosition, className, ...rest
|
||||||
|
}) => {
|
||||||
const handleClasses = cx(
|
const handleClasses = cx(
|
||||||
'react-graph__handle',
|
'react-graph__handle',
|
||||||
className,
|
className,
|
||||||
@@ -59,4 +62,4 @@ const BaseHandle = memo(({ source, target, nodeId, onConnect, setSourceId, setPo
|
|||||||
BaseHandle.displayName = 'BaseHandle';
|
BaseHandle.displayName = 'BaseHandle';
|
||||||
BaseHandle.whyDidYouRender = false;
|
BaseHandle.whyDidYouRender = false;
|
||||||
|
|
||||||
export default BaseHandle;
|
export default BaseHandle;
|
||||||
|
|||||||
@@ -21,6 +21,6 @@ const TargetHandle = memo((props) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
TargetHandle.displayName = 'TargetHandle';
|
TargetHandle.displayName = 'TargetHandle';
|
||||||
TargetHandle.whyDidYouRender = false;
|
TargetHandle.whyDidYouRender = true;
|
||||||
|
|
||||||
export default TargetHandle;
|
export default TargetHandle;
|
||||||
|
|||||||
@@ -92,17 +92,16 @@ export default NodeComponent => {
|
|||||||
} = props;
|
} = props;
|
||||||
|
|
||||||
const position = { x: xPos, y: yPos };
|
const position = { x: xPos, y: yPos };
|
||||||
const [ x, y, k ] = transform;
|
|
||||||
const nodeClasses = cx('react-graph__node', { selected });
|
const nodeClasses = cx('react-graph__node', { selected });
|
||||||
const nodeStyle = { zIndex: selected ? 10 : 3, transform: `translate(${xPos}px,${yPos}px)` };
|
const nodeStyle = { zIndex: selected ? 10 : 3, transform: `translate(${xPos}px,${yPos}px)` };
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const bounds = nodeElement.current.getBoundingClientRect();
|
const bounds = nodeElement.current.getBoundingClientRect();
|
||||||
const unscaledWith = Math.round(bounds.width * (1 / k));
|
const unscaledWith = Math.round(bounds.width * (1 / transform[2]));
|
||||||
const unscaledHeight = Math.round(bounds.height * (1 / k));
|
const unscaledHeight = Math.round(bounds.height * (1 / transform[2]));
|
||||||
const handleBounds = {
|
const handleBounds = {
|
||||||
source: getHandleBounds('.source', nodeElement.current, bounds, k),
|
source: getHandleBounds('.source', nodeElement.current, bounds, transform[2]),
|
||||||
target: getHandleBounds('.target', nodeElement.current, bounds, k)
|
target: getHandleBounds('.target', nodeElement.current, bounds, transform[2])
|
||||||
};
|
};
|
||||||
|
|
||||||
dispatch(updateNodeData(id, { width: unscaledWith, height: unscaledHeight, handleBounds }));
|
dispatch(updateNodeData(id, { width: unscaledWith, height: unscaledHeight, handleBounds }));
|
||||||
@@ -110,7 +109,7 @@ export default NodeComponent => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<ReactDraggable.DraggableCore
|
<ReactDraggable.DraggableCore
|
||||||
onStart={evt => onStart(evt, { setOffset,transform, position })}
|
onStart={evt => onStart(evt, { setOffset, transform, position })}
|
||||||
onDrag={evt => onDrag(evt, { dispatch, setDragging, id, offset, transform })}
|
onDrag={evt => onDrag(evt, { dispatch, setDragging, id, offset, transform })}
|
||||||
onStop={() => onStop({ onNodeDragStop, isDragging, setDragging, id, type, position, data })}
|
onStop={() => onStop({ onNodeDragStop, isDragging, setDragging, id, type, position, data })}
|
||||||
scale={transform[2]}
|
scale={transform[2]}
|
||||||
|
|||||||
Reference in New Issue
Block a user