@@ -117,7 +117,8 @@ class CornerstoneViewport extends Component {
117117 super ( props ) ;
118118
119119 // TODO: Allow viewport as a prop
120- const { stack } = props . viewportData ;
120+ const viewportDataStack = props . viewportData . stack ;
121+ const stack = Object . assign ( { } , viewportDataStack ) ;
121122 this . state = {
122123 stack,
123124 displaySetInstanceUid : props . viewportData . displaySetInstanceUid ,
@@ -149,6 +150,55 @@ class CornerstoneViewport extends Component {
149150 } , 300 ) ;
150151 }
151152
153+ /**
154+ * Updates viewportSpecificData only if new image is displayed for the same study and display set (stack scroll, cine play, etc.)
155+ */
156+ updateViewportSpecificData = ( ) => {
157+ if ( ! this . props . setViewportSpecificData ) {
158+ return ;
159+ }
160+
161+ const stateStack = this . state . stack ;
162+ const viewportDataStack = this . props . viewportData . stack ;
163+
164+ // Skip if study or display set was changed
165+ if (
166+ stateStack . displaySetInstanceUid !==
167+ viewportDataStack . displaySetInstanceUid ||
168+ stateStack . studyInstanceUid !== viewportDataStack . studyInstanceUid
169+ ) {
170+ return ;
171+ }
172+
173+ // Skip if image was not changed
174+ if (
175+ stateStack . currentImageIdIndex ===
176+ viewportDataStack . currentImageIdIndex &&
177+ stateStack . sopInstanceUid === viewportDataStack . sopInstanceUid
178+ ) {
179+ return ;
180+ }
181+
182+ const stackData = cornerstoneTools . getToolState ( this . element , 'stack' ) ;
183+ const stack = stackData && stackData . data [ 0 ] ;
184+
185+ const imageId = stack . imageIds [ stack . currentImageIdIndex ] ;
186+ const sopCommonModule = cornerstone . metaData . get (
187+ 'sopCommonModule' ,
188+ imageId
189+ ) ;
190+ if ( ! sopCommonModule ) {
191+ return ;
192+ }
193+
194+ this . props . setViewportSpecificData ( {
195+ displaySetInstanceUid : stack . displaySetInstanceUid ,
196+ studyInstanceUid : stack . studyInstanceUid ,
197+ currentImageIdIndex : stack . currentImageIdIndex ,
198+ sopInstanceUid : sopCommonModule . sopInstanceUID
199+ } ) ;
200+ } ;
201+
152202 render ( ) {
153203 const isLoading = this . state . isLoading ;
154204 // TODO: Check this later
@@ -239,6 +289,8 @@ class CornerstoneViewport extends Component {
239289 if ( this . props . onNewImage ) {
240290 this . props . onNewImage ( event ) ;
241291 }
292+
293+ this . updateViewportSpecificData ( ) ;
242294 } ;
243295
244296 componentDidMount ( ) {
@@ -530,20 +582,12 @@ class CornerstoneViewport extends Component {
530582 const imageId =
531583 currentStack . imageIds [ currentImageIdIndex ] || currentStack . imageIds [ 0 ] ;
532584
533- const viewportSpecificData = {
585+ this . setState ( {
534586 displaySetInstanceUid,
535587 studyInstanceUid,
536588 stack,
537589 imageId
538- } ;
539-
540- this . setState ( viewportSpecificData ) ;
541-
542- /*
543- Temporarily removed because it didn't seem to be performing well
544- if (this.props.setViewportSpecificData) {
545- this.props.setViewportSpecificData(viewportSpecificData);
546- }*/
590+ } ) ;
547591
548592 cornerstoneTools . stackPrefetch . disable ( this . element ) ;
549593 cornerstone . loadAndCacheImage ( imageId ) . then ( image => {
@@ -586,7 +630,8 @@ class CornerstoneViewport extends Component {
586630 const currentImageIdIndex = this . props . viewportData . stack
587631 . currentImageIdIndex ;
588632
589- const stack = this . props . viewportData . stack ;
633+ const viewportDataStack = this . props . viewportData . stack ;
634+ const stack = Object . assign ( { } , viewportDataStack ) ;
590635 const stackData = cornerstoneTools . getToolState ( this . element , 'stack' ) ;
591636 let currentStack = stackData && stackData . data [ 0 ] ;
592637
@@ -608,14 +653,12 @@ class CornerstoneViewport extends Component {
608653
609654 const imageId = currentStack . imageIds [ currentImageIdIndex ] ;
610655
611- const viewportSpecificData = {
656+ this . setState ( {
612657 displaySetInstanceUid,
613658 studyInstanceUid,
614659 stack,
615660 imageId
616- } ;
617-
618- this . setState ( viewportSpecificData ) ;
661+ } ) ;
619662 }
620663
621664 if ( this . props . activeTool !== prevProps . activeTool ) {
@@ -651,13 +694,6 @@ class CornerstoneViewport extends Component {
651694 cornerstoneTools . playClip ( this . element ) ;
652695 } else {
653696 cornerstoneTools . stopClip ( this . element ) ;
654-
655- const stackData = cornerstoneTools . getToolState ( this . element , 'stack' ) ;
656- const stack = stackData . data [ 0 ] ;
657-
658- if ( this . props . setViewportSpecificData ) {
659- this . props . setViewportSpecificData ( { stack } ) ;
660- }
661697 }
662698 }
663699
@@ -738,14 +774,6 @@ class CornerstoneViewport extends Component {
738774 this . setState ( {
739775 stack
740776 } ) ;
741-
742- /*
743- TODO: call this, but debounce it
744-
745- Temporarily removed because it didn't seem to be performing well
746- if (this.props.setViewportSpecificData) {
747- this.props.setViewportSpecificData({ stack });
748- }*/
749777 } ;
750778
751779 onImageLoaded = ( ) => {
@@ -827,11 +855,6 @@ class CornerstoneViewport extends Component {
827855
828856 imageSliderOnInputCallback = value => {
829857 this . setViewportActive ( ) ;
830- /*
831- Temporarily removed because it didn't seem to be performing well
832- if (this.props.setViewportSpecificData) {
833- this.props.setViewportSpecificData({ stack });
834- }*/
835858
836859 scrollToIndex ( this . element , value ) ;
837860
0 commit comments