@@ -22,9 +22,10 @@ angular.module('mm.addons.mod_wiki')
2222 * @name $mmaModWikiPrefetchHandler
2323 */
2424. factory ( '$mmaModWikiPrefetchHandler' , function ( $mmaModWiki , mmaModWikiComponent , $mmSite , $mmFilepool , $q , $mmGroups ,
25- $mmCourseHelper , $mmCourse ) {
25+ $mmCourseHelper , $mmCourse , mmCoreDownloading , mmCoreDownloaded ) {
2626
27- var self = { } ;
27+ var self = { } ,
28+ downloadPromises = { } ; // Store download promises to prevent duplicate requests.
2829
2930 self . component = mmaModWikiComponent ;
3031
@@ -206,10 +207,25 @@ angular.module('mm.addons.mod_wiki')
206207 */
207208 self . prefetch = function ( module , courseId , single ) {
208209 var siteId = $mmSite . getId ( ) ,
209- userid = userid || $mmSite . getUserId ( ) ;
210+ userid = userid || $mmSite . getUserId ( ) ,
211+ prefetchPromise ,
212+ deleted = false ,
213+ component = mmaModWikiComponent ,
214+ revision ,
215+ timemod ;
210216
211- // Get Package timemodified in order to retrieve only updated pages.
212- return $mmFilepool . getPackageTimemodified ( siteId , mmaModWikiComponent , module . id ) . then ( function ( packageModified ) {
217+ if ( downloadPromises [ siteId ] && downloadPromises [ siteId ] [ module . id ] ) {
218+ // There's already a download ongoing for this package, return the promise.
219+ return downloadPromises [ siteId ] [ module . id ] ;
220+ } else if ( ! downloadPromises [ siteId ] ) {
221+ downloadPromises [ siteId ] = { } ;
222+ }
223+
224+ // Mark package as downloading.
225+ prefetchPromise = $mmFilepool . storePackageStatus ( siteId , component , module . id , mmCoreDownloading ) . then ( function ( ) {
226+ // Get Package timemodified in order to retrieve only updated pages.
227+ return $mmFilepool . getPackageTimemodified ( siteId , component , module . id ) ;
228+ } ) . then ( function ( packageModified ) {
213229 // Get Page list to be retrieved. getWiki and getSubwikis done in getAllPages.
214230 return getAllPages ( module , courseId , siteId ) . then ( function ( pages ) {
215231 var promises = [ ] ;
@@ -241,19 +257,42 @@ angular.module('mm.addons.mod_wiki')
241257
242258 // Get related page files and fetch them.
243259 promises . push ( self . getFiles ( module , courseId , siteId ) . then ( function ( files ) {
244- var revision = $mmFilepool . getRevisionFromFileList ( files ) ,
245- pagesTimemodified = getTimemodifiedFromPages ( pages ) ,
246- filesTimemodified = $mmFilepool . getTimemodifiedFromFileList ( files ) ,
247- timemodified = Math . max ( pagesTimemodified , filesTimemodified ) ;
248-
249- // Download related files and update package info.
250- return $mmFilepool . prefetchPackage ( siteId , files , mmaModWikiComponent , module . id , revision ,
251- timemodified ) ;
260+ var filePromises = [ ] ;
261+
262+ revision = $mmFilepool . getRevisionFromFileList ( files ) ;
263+
264+ angular . forEach ( files , function ( file ) {
265+ var url = file . fileurl ;
266+ filePromises . push ( $mmFilepool . addToQueueByUrl ( siteId , url , component , module . id , file . timemodified ) ) ;
267+ } ) ;
268+
269+ return $q . all ( filePromises ) ;
270+ } ) ) ;
271+
272+ // Get timemodified.
273+ promises . push ( self . getTimemodified ( module , courseId , siteId ) . then ( function ( timemodified ) {
274+ timemod = timemodified ;
252275 } ) ) ;
253276
254277 return $q . all ( promises ) ;
255278 } ) ;
279+ } ) . then ( function ( ) {
280+ // Prefetch finished, mark as downloaded.
281+ return $mmFilepool . storePackageStatus ( siteId , component , module . id , mmCoreDownloaded , revision , timemod ) ;
282+ } ) . catch ( function ( error ) {
283+ // Error prefetching, go back to previous status and reject the promise.
284+ return $mmFilepool . setPackagePreviousStatus ( siteId , component , module . id ) . then ( function ( ) {
285+ return $q . reject ( error ) ;
286+ } ) ;
287+ } ) . finally ( function ( ) {
288+ deleted = true ;
289+ delete downloadPromises [ siteId ] [ module . id ] ;
256290 } ) ;
291+
292+ if ( ! deleted ) {
293+ downloadPromises [ siteId ] [ module . id ] = prefetchPromise ;
294+ }
295+ return prefetchPromise ;
257296 } ;
258297
259298 return self ;
0 commit comments