Skip to content

Commit

Permalink
Avoid separate op for groupValuesRefresh
Browse files Browse the repository at this point in the history
  • Loading branch information
zyc9012 committed Dec 21, 2023
1 parent d9c6a86 commit 42b3c22
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
12 changes: 0 additions & 12 deletions packages/react/src/components/Sheet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
Canvas,
updateContextWithCanvas,
updateContextWithSheetData,
groupValuesRefresh,
handleGlobalWheel,
initFreeze,
Sheet as SheetType,
Expand Down Expand Up @@ -238,17 +237,6 @@ const Sheet: React.FC<Props> = ({ sheet }) => {
}
}, [context, refs.canvas, refs.globalCache.freezen, setContext, sheet.id]);

/**
* Apply the calculation results
*/
useEffect(() => {
if (context.groupValuesRefreshData.length > 0) {
setContext((draftCtx) => {
groupValuesRefresh(draftCtx);
});
}
}, [context.groupValuesRefreshData.length, setContext]);

const onWheel = useCallback(
(e: WheelEvent) => {
setContext((draftCtx) => {
Expand Down
17 changes: 16 additions & 1 deletion packages/react/src/components/Workbook/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
insertRowCol,
locale,
calcSelectionInfo,
groupValuesRefresh,
} from "@fortune-sheet/core";
import React, {
useMemo,
Expand Down Expand Up @@ -59,6 +60,20 @@ type AdditionalProps = {
onOp?: (op: Op[]) => void;
};

const triggerGroupValuesRefresh = (ctx: Context) => {
if (ctx.groupValuesRefreshData.length > 0) {
groupValuesRefresh(ctx);
}
};

const concatProducer = (...producers: ((ctx: Context) => void)[]) => {
return (ctx: Context) => {
producers.forEach((producer) => {
producer(ctx);
});
};
};

const Workbook = React.forwardRef<WorkbookInstance, Settings & AdditionalProps>(
({ onChange, onOp, data: originalData, ...props }, ref) => {
const globalCache = useRef<GlobalCache>({ undoList: [], redoList: [] });
Expand Down Expand Up @@ -246,7 +261,7 @@ const Workbook = React.forwardRef<WorkbookInstance, Settings & AdditionalProps>(
setContext((ctx_) => {
const [result, patches, inversePatches] = produceWithPatches(
ctx_,
recipe
concatProducer(recipe, triggerGroupValuesRefresh)
);
if (patches.length > 0 && !options.noHistory) {
if (options.logPatch) {
Expand Down

0 comments on commit 42b3c22

Please sign in to comment.