@@ -59,6 +59,17 @@ define((require) => {
5959 this . disconnectFromPython ( ) ;
6060 }
6161
62+ UNSAFE_componentWillReceiveProps ( nextProps ) {
63+ this . disconnectFromPython ( ) ;
64+ this . id = ( nextProps . id === undefined ) ? nextProps . model : nextProps . id ;
65+
66+ GEPPETTO . ComponentFactory . addExistingComponent ( this . state . componentType , this ) ;
67+ this . connectToPython ( this . state . componentType , nextProps . model ) ;
68+ if ( this . state . value !== nextProps . value ) {
69+ this . setState ( { value : ( nextProps . value === undefined ) ? '' : nextProps . value } ) ;
70+ }
71+ }
72+
6273 componentDidMount ( ) {
6374 this . _isMounted = true ;
6475 GEPPETTO . ComponentFactory . addExistingComponent ( this . state . componentType , this , true ) ;
@@ -68,16 +79,6 @@ define((require) => {
6879 if ( this . props . value !== undefined ) {
6980 this . setState ( { value : this . props . value } ) ;
7081 }
71- this . refreshPython ( ) ;
72- }
73- refreshPython ( ) {
74- this . disconnectFromPython ( ) ;
75- GEPPETTO . ComponentFactory . addExistingComponent ( this . state . componentType , this ) ;
76- this . connectToPython ( this . state . componentType , this . props . model ) ;
77- }
78- UNSAFE_componentWillReceiveProps ( nextProps ) {
79- if ( this . props !== nextProps )
80- this . refreshPython ( ) ;
8182 }
8283 }
8384
@@ -100,19 +101,52 @@ define((require) => {
100101 this . handleChange = ( this . props . handleChange === undefined ) ? this . handleChange . bind ( this ) : this . props . handleChange . bind ( this ) ;
101102 this . handleUpdateInput = this . handleUpdateInput . bind ( this ) ;
102103 this . handleUpdateCheckbox = this . handleUpdateCheckbox . bind ( this ) ;
103- this . commands = undefined ;
104104 }
105105
106106 componentDidMount ( ) {
107107 super . componentDidMount ( ) ;
108- this . refreshPython ( ) ;
109- //this.UNRELIABLE_SyncDefaultValueWithPython();
108+ this . UNRELIABLE_SyncDefaultValueWithPython ( ) ;
109+ }
110+
111+ /*
112+ * since we don't know when a component will be synched with python,
113+ * we can't know when to check if this.state.value should be replaced
114+ * with this.props.default
115+ */
116+ UNRELIABLE_SyncDefaultValueWithPython ( timeInterval = 600 , attemps = 0 ) {
117+ if ( attemps < 3 ) {
118+ setTimeout ( ( ) => {
119+ if ( this . props . default && this . state . value === '' ) {
120+ if ( this . syncValueWithPython ) {
121+ // this function is added by jupyter_geppetto after the component is synched with python
122+ this . syncValueWithPython ( this . props . default ) ;
123+ } else {
124+ this . UNRELIABLE_SyncDefaultValueWithPython ( timeInterval * 2 , attemps + 1 ) ;
125+ }
126+ }
127+ } , timeInterval ) ;
128+ } else {
129+ console . warn ( `Tried to sync default value for ${ this . props . model } and failed after 3 attempts.` ) ;
130+ }
110131 }
111132
112- refreshPython ( ) {
133+ UNSAFE_componentWillReceiveProps ( nextProps ) {
113134 this . disconnectFromPython ( ) ;
135+ this . id = ( nextProps . id === undefined ) ? nextProps . model : nextProps . id ;
114136 GEPPETTO . ComponentFactory . addExistingComponent ( this . state . componentType , this ) ;
115- this . connectToPython ( this . state . componentType , this . props . model ) ;
137+ this . connectToPython ( this . state . componentType , nextProps . model ) ;
138+ if ( ( this . state . searchText !== nextProps . searchText ) && ( nextProps . searchText !== undefined ) ) {
139+ this . setState ( { searchText : nextProps . searchText } ) ;
140+ }
141+ if ( ( this . state . checked !== nextProps . checked ) && ( nextProps . checked !== undefined ) ) {
142+ this . setState ( { checked : nextProps . checked } ) ;
143+ }
144+ if ( ( this . state . value !== nextProps . value ) && ( nextProps . value !== undefined ) ) {
145+ this . setState ( { value : nextProps . value } ) ;
146+ }
147+ if ( ( this . state . model !== nextProps . model ) && ( nextProps . model !== undefined ) ) {
148+ this . setState ( { model : nextProps . model } ) ;
149+ }
116150 }
117151
118152 componentDidUpdate ( prevProps , prevState ) {
@@ -151,7 +185,7 @@ define((require) => {
151185 && this . state . value === ''
152186 && this . props . default
153187 ) {
154- // this.UNRELIABLE_SyncDefaultValueWithPython(1000);
188+ this . UNRELIABLE_SyncDefaultValueWithPython ( 1000 ) ;
155189 }
156190 }
157191
@@ -242,7 +276,6 @@ define((require) => {
242276 // Checkbox
243277 handleUpdateCheckbox ( event , isInputChecked ) {
244278 this . updatePythonValue ( isInputChecked ) ;
245- this . refreshPython ( ) ;
246279 }
247280
248281 render ( ) {
@@ -329,8 +362,12 @@ define((require) => {
329362 }
330363
331364 UNSAFE_componentWillReceiveProps ( nextProps ) {
332- if ( this . props !== nextProps )
333- this . refreshPython ( ) ;
365+ this . disconnectFromPython ( ) ;
366+ this . id = ( nextProps . id === undefined ) ? nextProps . model : nextProps . id ;
367+
368+ GEPPETTO . ComponentFactory . addExistingComponent ( this . state . componentType , this ) ;
369+ this . connectToPython ( this . state . componentType , nextProps . model ) ;
370+ this . callPythonMethod ( ) ;
334371 }
335372
336373 updatePythonValue ( newValue ) {
@@ -383,7 +420,7 @@ define((require) => {
383420 }
384421
385422 callPythonMethod = ( value ) => {
386- const params = this . props ?. pythonparams || [ ] ;
423+ const params = this . props ?. pythonParams || [ ] ;
387424 if ( this . props . method ) {
388425 GeppettoUtils . evalPythonMessage ( this . props . method , params )
389426 . then ( ( response ) => {
0 commit comments