@@ -1737,10 +1737,11 @@ SIREPO.app.factory('srCache', function(appState, $rootScope) {
1737
1737
} ) ;
1738
1738
1739
1739
1740
- SIREPO . app . factory ( 'frameCache' , function ( appState , panelState , requestSender , srCache , $interval , $rootScope , $timeout ) {
1741
- var self = { } ;
1742
- var frameCountByModelKey = { } ;
1743
- var masterFrameCount = 0 ;
1740
+ SIREPO . app . factory ( 'frameCache' , function ( appState , panelState , requestSender , srCache , $rootScope , $timeout ) {
1741
+ const self = { } ;
1742
+ let frameCountByModelKey = { } ;
1743
+ let masterFrameCount = 0 ;
1744
+ const requestByModelKey = { } ;
1744
1745
self . modelToCurrentFrame = { } ;
1745
1746
1746
1747
function frameId ( frameReport , frameIndex ) {
@@ -1782,92 +1783,105 @@ SIREPO.app.factory('frameCache', function(appState, panelState, requestSender, s
1782
1783
return self . modelToCurrentFrame [ modelName ] || 0 ;
1783
1784
} ;
1784
1785
1785
- self . getFrame = function ( modelName , index , isPlaying , callback ) {
1786
- const isHidden = panelState . isHidden ( modelName ) ;
1787
- let frameRequestTime = now ( ) ;
1788
- let setPanelStateIsLoadingTimer = null ;
1789
-
1790
- function cancelSetPanelStateIsLoadingTimer ( ) {
1791
- if ( setPanelStateIsLoadingTimer ) {
1792
- $timeout . cancel ( setPanelStateIsLoadingTimer ) ;
1793
- setPanelStateIsLoadingTimer = null ;
1794
- }
1795
- }
1786
+ self . getFrame = function ( modelKey , index , isPlaying , callback ) {
1787
+ let loadingTimer = null ;
1796
1788
1797
- function onError ( response ) {
1798
- if ( ! ( response && response . error ) ) {
1799
- panelState . reportNotGenerated ( modelName ) ;
1800
- }
1801
- else if ( ! response . sbatchLoginServiceSRException ) {
1802
- panelState . setLoading ( modelName , false ) ;
1803
- panelState . setError ( modelName , response . error ) ;
1789
+ const callbackData = ( data , frameRequestTime ) => {
1790
+ const t = framePeriod ( ) - ( now ( ) - frameRequestTime ) ;
1791
+ if ( t <= 0 ) {
1792
+ callback ( index , data ) ;
1793
+ return ;
1804
1794
}
1805
- cancelSetPanelStateIsLoadingTimer ( ) ;
1806
- }
1795
+ $timeout ( ( ) => callback ( index , data ) , t ) ;
1796
+ } ;
1807
1797
1808
- function now ( ) {
1809
- return new Date ( ) . getTime ( ) ;
1810
- }
1798
+ const cancelLoadingTimer = ( ) => {
1799
+ panelState . setLoading ( modelKey , false ) ;
1800
+ if ( loadingTimer ) {
1801
+ $timeout . cancel ( loadingTimer ) ;
1802
+ loadingTimer = null ;
1803
+ }
1804
+ } ;
1811
1805
1812
1806
const framePeriod = ( ) => {
1813
- if ( ! isPlaying || isHidden ) {
1807
+ if ( ! isPlaying || panelState . isHidden ( modelKey ) ) {
1814
1808
return 0 ;
1815
1809
}
1816
- const milliseconds = 1000 ;
1817
- var x = appState . models [ modelName ] . framesPerSecond ;
1818
- if ( ! x ) {
1819
- return 0.5 * milliseconds ;
1810
+ const s = appState . models [ modelKey ] . framesPerSecond ;
1811
+ return 1000 / ( s && parseInt ( s ) ? parseInt ( s ) : 2 ) ;
1812
+ } ;
1813
+
1814
+ const now = ( ) => new Date ( ) . getTime ( ) ;
1815
+
1816
+ const onError = ( response ) => {
1817
+ if ( ! ( response && response . error ) ) {
1818
+ panelState . reportNotGenerated ( modelKey ) ;
1819
+ }
1820
+ else if ( ! response . sbatchLoginServiceSRException ) {
1821
+ panelState . setError ( modelKey , response . error ) ;
1820
1822
}
1821
- return milliseconds / parseInt ( x ) ;
1823
+ cancelLoadingTimer ( ) ;
1822
1824
} ;
1823
1825
1824
- const callbackData = ( data ) => {
1825
- let e = framePeriod ( ) - ( now ( ) - frameRequestTime ) ;
1826
- if ( e <= 0 ) {
1827
- callback ( index , data ) ;
1828
- return ;
1826
+ const checkNextRequest = ( ) => {
1827
+ const i = requestByModelKey [ modelKey ] ;
1828
+ delete requestByModelKey [ modelKey ] ;
1829
+ if ( i != index ) {
1830
+ index = i ;
1831
+ // avoid a recursive stack overflow
1832
+ $timeout ( ( ) => requestFunction ( true ) , 0 ) ;
1829
1833
}
1830
- $interval (
1831
- function ( ) {
1832
- callback ( index , data ) ;
1833
- } ,
1834
- e ,
1835
- 1
1836
- ) ;
1837
1834
} ;
1838
1835
1839
- const requestFunction = function ( ) {
1840
- const id = frameId ( modelName , index ) ;
1841
- srCache . getFrame ( id , modelName , ( data ) => {
1836
+ const requestFunction = ( isRetry ) => {
1837
+ const id = frameId ( modelKey , index ) ;
1838
+ const frameRequestTime = now ( ) ;
1839
+ srCache . getFrame ( id , modelKey , ( data ) => {
1840
+ if ( isRetry && modelKey in requestByModelKey ) {
1841
+ // another frame has been requested since the retry was started
1842
+ return ;
1843
+ }
1842
1844
if ( data ) {
1843
- callbackData ( data ) ;
1845
+ callbackData ( data , frameRequestTime ) ;
1846
+ return ;
1847
+ }
1848
+ if ( modelKey in requestByModelKey ) {
1849
+ requestByModelKey [ modelKey ] = index ;
1844
1850
return ;
1845
1851
}
1846
- setPanelStateIsLoadingTimer = $timeout ( ( ) => {
1847
- panelState . setLoading ( modelName , true ) ;
1848
- } , 5000 ) ;
1852
+ if ( ! loadingTimer ) {
1853
+ loadingTimer = $timeout ( ( ) => {
1854
+ panelState . setLoading ( modelKey , true ) ;
1855
+ } , 5000 ) ;
1856
+ }
1857
+ requestByModelKey [ modelKey ] = index ;
1849
1858
requestSender . sendRequest (
1850
1859
{
1851
1860
routeName : 'simulationFrame' ,
1852
1861
frame_id : id ,
1853
1862
} ,
1854
- function ( data ) {
1855
- cancelSetPanelStateIsLoadingTimer ( ) ;
1856
- panelState . setLoading ( modelName , false ) ;
1863
+ ( data ) => {
1864
+ cancelLoadingTimer ( ) ;
1857
1865
if ( 'state' in data && data . state === 'missing' ) {
1858
1866
onError ( ) ;
1859
- return ;
1860
1867
}
1861
- callbackData ( data ) ;
1862
- srCache . saveFrame ( id , modelName , data ) ;
1868
+ else {
1869
+ callbackData ( data , frameRequestTime ) ;
1870
+ srCache . saveFrame ( id , modelKey , data ) ;
1871
+ }
1872
+ checkNextRequest ( ) ;
1863
1873
} ,
1864
1874
null ,
1865
- onError
1875
+ ( response ) => {
1876
+ onError ( response ) ;
1877
+ checkNextRequest ( ) ;
1878
+ }
1866
1879
) ;
1867
1880
} ) ;
1868
1881
} ;
1869
- if ( isHidden ) {
1870
- panelState . addPendingRequest ( modelName , requestFunction ) ;
1882
+
1883
+ if ( panelState . isHidden ( modelKey ) ) {
1884
+ panelState . setPendingRequest ( modelKey , requestFunction ) ;
1871
1885
}
1872
1886
else {
1873
1887
requestFunction ( ) ;
@@ -2145,10 +2159,6 @@ SIREPO.app.factory('panelState', function(appState, uri, simulationQueue, utilit
2145
2159
return uri . format ( route , { ...a , ...args } ) ;
2146
2160
}
2147
2161
2148
- self . addPendingRequest = function ( name , requestFunction ) {
2149
- pendingRequests [ name ] = requestFunction ;
2150
- } ;
2151
-
2152
2162
self . clear = function ( name ) {
2153
2163
if ( name ) {
2154
2164
clearPanel ( name ) ;
@@ -2332,7 +2342,7 @@ SIREPO.app.factory('panelState', function(appState, uri, simulationQueue, utilit
2332
2342
if ( queueItems [ name ] ) {
2333
2343
simulationQueue . cancelItem ( queueItems [ name ] ) ;
2334
2344
}
2335
- self . addPendingRequest ( name , function ( ) {
2345
+ self . setPendingRequest ( name , ( ) => {
2336
2346
queueItems [ name ] = sendRequest ( name , wrappedCallback , errorCallback ) ;
2337
2347
} ) ;
2338
2348
if ( ! self . isHidden ( name ) ) {
@@ -2360,6 +2370,8 @@ SIREPO.app.factory('panelState', function(appState, uri, simulationQueue, utilit
2360
2370
2361
2371
self . setData = ( name , data ) => setPanelValue ( name , 'data' , data ) ;
2362
2372
2373
+ self . setPendingRequest = ( name , requestFunction ) => pendingRequests [ name ] = requestFunction ;
2374
+
2363
2375
self . setWaiting = ( name , isWaiting ) => {
2364
2376
setPanelValue ( name , 'waiting' , isWaiting ) ;
2365
2377
} ;
0 commit comments