@@ -2,18 +2,42 @@ var reportUrl = 'http://powerbiembedapiv2.azurewebsites.net/api/Reports/SampleRe
22var  datasetUrl  =  'http://powerbiembedapiv2.azurewebsites.net/api/Reports/SampleCreate' ; 
33var  dashboardUrl  =  'http://powerbiembedapiv2.azurewebsites.net/api/Dashboards/SampleDashboard' ; 
44
5+ var  LastReportSampleUrl  =  null ; 
6+ var  ReportRefreshTokenTimer  =  0 ; 
7+ var  DashboardRefreshTokenTimer  =  0 ; 
8+ 
59const  EntityType  =  { 
610    Report  : "Report" , 
711    Dashboard  : "Dashboard" , 
812} ; 
913
10- function  FetchUrlIntoSession ( url )  { 
14+ function  FetchUrlIntoSession ( url ,   updateCurrentToken )  { 
1115    return  fetch ( url ) . then ( function  ( response )  { 
1216        if  ( response . ok )  { 
1317            return  response . json ( ) 
1418            . then ( function  ( embedConfig )  { 
1519                setSession ( embedConfig . embedToken . token ,  embedConfig . embedUrl ,  embedConfig . id ) ; 
1620                SetSession ( SessionKeys . SampleId ,  embedConfig . id ) ; 
21+ 
22+                 if  ( updateCurrentToken ) 
23+                 { 
24+                     var  embedContainerId  =  ( embedConfig . type  ===  "report" )  ? "embedContainer"  : "dashboardContainer" ; 
25+                     var  embedContainer  =  powerbi . embeds . find ( function ( embedElement )  { return  ( embedElement . element . id  ==  embedContainerId ) } ) ; 
26+                     if  ( embedContainer ) 
27+                     { 
28+                         embedContainer . setAccessToken ( embedConfig . embedToken . token ) ; 
29+                     } 
30+                 } 
31+ 
32+                 if  ( embedConfig . type  ===  "report" ) 
33+                 { 
34+                     LastReportSampleUrl  =  url ; 
35+                     TokenExpirationRefreshListener ( embedConfig . minutesToExpiration ,  EntityType . Report ) ; 
36+                 } 
37+                 else 
38+                 { 
39+                     TokenExpirationRefreshListener ( embedConfig . minutesToExpiration ,  EntityType . Dashboard ) ; 
40+                 } 
1741            } ) ; 
1842        } 
1943        else  { 
@@ -25,19 +49,53 @@ function FetchUrlIntoSession(url) {
2549    } ) ; 
2650} 
2751
52+ function  TokenExpirationRefreshListener ( minutesToExpiration ,  entityType )  { 
53+     var  updateAfterMilliSeconds  =  ( minutesToExpiration  -  2 )  *  60  *  1000 ; 
54+ 
55+     if  ( entityType  ==  EntityType . Report ) 
56+     { 
57+         if  ( ReportRefreshTokenTimer ) 
58+         { 
59+             console . log ( "step current Report Embed Token update threads." ) ; 
60+             clearTimeout ( ReportRefreshTokenTimer ) ; 
61+         } 
62+ 
63+         console . log ( "Report Embed Token will be updated in "  +  updateAfterMilliSeconds  +  " milliseconds." ) ; 
64+         ReportRefreshTokenTimer  =  setTimeout ( function ( )  { 
65+             if  ( LastReportSampleUrl ) 
66+             { 
67+                 FetchUrlIntoSession ( LastReportSampleUrl ,  true  /* updateCurrentToken */ ) ; 
68+             } 
69+         } ,  updateAfterMilliSeconds ) ; 
70+     } 
71+     else  if  ( entityType  ==  EntityType . Dashboard ) 
72+     { 
73+         if  ( DashboardRefreshTokenTimer ) 
74+         { 
75+             console . log ( "step current Dashboard Embed Token update threads." ) ; 
76+             clearTimeout ( DashboardRefreshTokenTimer ) ; 
77+         } 
78+ 
79+         console . log ( "Dashboard Embed Token will be updated in "  +  updateAfterMilliSeconds  +  " milliseconds." ) ; 
80+         DashboardRefreshTokenTimer  =  setTimeout ( function ( )  { 
81+             FetchUrlIntoSession ( dashboardUrl ,  true  /* updateCurrentToken */ ) ; 
82+         } ,  updateAfterMilliSeconds ) ; 
83+     } 
84+ } 
85+ 
2886function  LoadSampleReportIntoSession ( )  { 
2987    SetSession ( SessionKeys . EntityType ,  EntityType . Report ) ; 
30-     return  FetchUrlIntoSession ( reportUrl ) ; 
88+     return  FetchUrlIntoSession ( reportUrl ,   false   /* updateCurrentToken */ ) ; 
3189} 
3290
3391function  LoadSampleDatasetIntoSession ( )  { 
3492    SetSession ( SessionKeys . EntityType ,  EntityType . Report ) ; 
35-     return  FetchUrlIntoSession ( datasetUrl ) ; 
93+     return  FetchUrlIntoSession ( datasetUrl ,   false   /* updateCurrentToken */ ) ; 
3694} 
3795
3896function  LoadSampleDashboardIntoSession ( )  { 
3997    SetSession ( SessionKeys . EntityType ,  EntityType . Dashboard ) ; 
40-     return  FetchUrlIntoSession ( dashboardUrl ) ; 
98+     return  FetchUrlIntoSession ( dashboardUrl ,   false   /* updateCurrentToken */ ) ; 
4199} 
42100
43101function  OpenEmbedStepWithSample ( entityType )  { 
0 commit comments