Skip to content

Commit 2f9cae0

Browse files
committed
chore: prevent unecessary script component updates
Related to #1102
1 parent c571d95 commit 2f9cae0

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

packages/form-js-viewer/src/render/components/form-fields/JSFunctionField.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import Sandbox from '@jetbrains/websandbox';
22
import { useCallback, useEffect, useRef, useState } from 'preact/hooks';
33
import { useExpressionEvaluation, useDeepCompareMemoize, usePrevious } from '../../hooks';
44
import { isObject } from 'min-dash';
5+
import { isEqual } from 'lodash';
56
import { v4 as uuidv4 } from 'uuid';
67

78
export function JSFunctionField(props) {
8-
const { field, onChange } = props;
9+
const { field, onChange, value } = props;
910
const {
1011
jsFunction: functionDefinition,
1112
functionParameters: paramsDefinition,
@@ -35,20 +36,33 @@ export function JSFunctionField(props) {
3536

3637
}, [ field.key ]);
3738

38-
const safeSetValue = useCallback((value) => {
39+
const valueRef = useRef(value);
3940

40-
if (value !== undefined) {
41+
useEffect(() => {
42+
valueRef.current = value;
43+
}, [ value ]);
4144

42-
// strip out functions and handle unserializeable objects
43-
try {
44-
value = JSON.parse(JSON.stringify(value));
45-
onChange({ field, value });
46-
} catch {
47-
sandboxError('Unparsable return value');
48-
clearValue();
49-
}
45+
const safeSetValue = useCallback((newValue) => {
46+
47+
if (newValue === undefined) {
48+
return;
49+
}
50+
51+
// strip out functions and handle unserializeable objects
52+
try {
53+
newValue = JSON.parse(JSON.stringify(newValue));
54+
} catch {
55+
sandboxError('Unparsable return value');
56+
clearValue();
5057
}
5158

59+
// prevent unnecessary updates
60+
if (isEqual(valueRef.current, newValue)) {
61+
return;
62+
}
63+
64+
onChange({ field, value: newValue });
65+
5266
}, [ onChange, field, sandboxError, clearValue ]);
5367

5468
useEffect(() => {
@@ -93,9 +107,6 @@ export function JSFunctionField(props) {
93107
frameClassName: 'fjs-sandbox-iframe'
94108
});
95109

96-
const iframe = iframeContainerRef.current.querySelector('iframe');
97-
iframe.removeAttribute('allow');
98-
99110
// (3) load user code in sandbox
100111
_sandbox.promise.then((sandboxInstance) => {
101112
sandboxInstance

0 commit comments

Comments
 (0)