@@ -862,6 +862,20 @@ qx.Class.define("osparc.data.model.Workbench", {
862862 } ) ;
863863
864864 // first, remove nodes
865+ if ( nodesRemoved . length ) {
866+ this . __removeNodesFromPatches ( nodesRemoved , workbenchPatchesByNode ) ;
867+ }
868+ // second, add nodes if any
869+ if ( nodesAdded . length ) {
870+ // this will call update nodes once finished
871+ this . __addNodesFromPatches ( nodesAdded , workbenchPatchesByNode ) ;
872+ } else {
873+ // third, update nodes
874+ this . __updateNodesFromPatches ( workbenchPatchesByNode ) ;
875+ }
876+ } ,
877+
878+ __removeNodesFromPatches : function ( nodesRemoved , workbenchPatchesByNode ) {
865879 nodesRemoved . forEach ( nodeId => {
866880 const node = this . getNode ( nodeId ) ;
867881 if ( node ) {
@@ -870,11 +884,27 @@ qx.Class.define("osparc.data.model.Workbench", {
870884 this . __nodeRemoved ( nodeId ) ;
871885 delete workbenchPatchesByNode [ nodeId ] ;
872886 } ) ;
887+ } ,
873888
874- // second, add nodes
875- console . log ( "Adding nodes" , nodesAdded ) ;
889+ __addNodesFromPatches : function ( nodesAdded , workbenchPatchesByNode ) {
890+ // this is an async operation with an await
891+ const promises = nodesAdded . map ( nodeId => {
892+ const nodeData = workbenchPatchesByNode [ nodeId ] [ 0 ] . value ;
893+ return this . createNode ( nodeData [ "key" ] , nodeData [ "version" ] ) ;
894+ } ) ;
895+ return Promise . all ( promises )
896+ . then ( nodes => {
897+ // may populate it
898+ // OM? delete the node add from the workbenchPatchesByNode
899+ // after adding nodes, we can apply the patches
900+ this . __updateNodesFromPatches ( workbenchPatchesByNode ) ;
901+ } )
902+ . catch ( err => {
903+ console . error ( "Error adding nodes from patches:" , err ) ;
904+ } ) ;
905+ } ,
876906
877- // third, update nodes
907+ __updateNodesFromPatches : function ( workbenchPatchesByNode ) {
878908 Object . keys ( workbenchPatchesByNode ) . forEach ( nodeId => {
879909 const node = this . getNode ( nodeId ) ;
880910 if ( node === null ) {
0 commit comments