@@ -2,10 +2,11 @@ import Sandbox from '@jetbrains/websandbox';
22import { useCallback , useEffect , useRef , useState } from 'preact/hooks' ;
33import { useExpressionEvaluation , useDeepCompareMemoize , usePrevious } from '../../hooks' ;
44import { isObject } from 'min-dash' ;
5+ import { isEqual } from 'lodash' ;
56import { v4 as uuidv4 } from 'uuid' ;
67
78export 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