@@ -64,7 +64,10 @@ class RootSaga {
6464 const state = yield select ( ) ;
6565 const viewMode = get . viewMode ( state ) ;
6666
67- if ( viewMode === Constants . CONTINUOUS_SCROLL_MODE ) {
67+ // when an outer config is provided, and the continuous scroll isn't default,
68+ // the page reset can start this saga before the manager has been crated.
69+ // Must be fixed at the architectural level, maybe the manager should be created lazily.
70+ if ( viewMode === Constants . CONTINUOUS_SCROLL_MODE && this . continuousScrollManager ) {
6871 yield * this . continuousScrollManager . startDataFetching ( ) ;
6972 } else {
7073 const pageNumber = get . currentPageNumber ( state ) ;
@@ -116,7 +119,8 @@ class RootSaga {
116119 yield put ( Actions . pagesSizesAreGottenAction ( pagesSizes ) ) ;
117120 }
118121
119- * configure ( { pageNumber, pageRotation, pageScale, language, theme, uiOptions } ) {
122+ * configure ( { pageNumber, pageRotation, viewMode, pageScale, language, theme, uiOptions } ) {
123+ if ( viewMode ) yield put ( { type : ActionTypes . SET_VIEW_MODE , payload : viewMode , notSave : true } ) ;
120124 if ( pageNumber ) yield put ( Actions . setNewPageNumberAction ( pageNumber , true ) ) ;
121125 if ( pageRotation ) yield put ( Actions . setPageRotationAction ( pageRotation ) ) ;
122126 if ( pageScale ) yield put ( Actions . setUserScaleAction ( pageScale ) ) ;
@@ -173,27 +177,32 @@ class RootSaga {
173177 isIndirect : ! isBundled ,
174178 } ) ;
175179
180+ yield * this . loadContents ( ) ;
181+
182+ const state = yield select ( ) ;
183+ if ( get . viewMode ( state ) === Constants . CONTINUOUS_SCROLL_MODE ) {
184+ yield * this . prepareForContinuousMode ( ) ;
185+ }
186+
176187 // perhaps it's better to configure the viewer before DOCUMENT_CREATED_ACTION
177188 // (since the promise of the viewer.loadDocument is resolved on this action)
178189 // But currently DOCUMENT_CREATED_ACTION reset the state of the viewer, so the configuration is done after it.
179190 // The optimal variant is to resolve the promise on another action, but I'm not sure is it needed to anybody at all.
191+ // Also, configuration can start some heavy sagas (e.g. viewMode or pageNumber change) which cancel all worker tasks.
192+ // Thus, it's done after all other tasks (including loading of the contents) are done.
180193 if ( config ) {
181194 yield * this . configure ( config ) ;
182195 }
183196
184- const state = yield select ( ) ;
185- this . continuousScrollManager = null ; // we don't have to reset it, since the worker was recreated and all memory was release in any case
186- if ( get . viewMode ( state ) === Constants . CONTINUOUS_SCROLL_MODE ) {
187- yield * this . prepareForContinuousMode ( ) ;
188- }
197+ yield * this . resetCurrentPageNumber ( ) ;
198+ }
189199
200+ * loadContents ( ) {
190201 const contents = yield this . djvuWorker . doc . getContents ( ) . run ( ) ;
191202 yield put ( {
192203 type : Constants . CONTENTS_IS_GOTTEN_ACTION ,
193204 contents : contents
194205 } ) ;
195-
196- yield * this . resetCurrentPageNumber ( ) ;
197206 }
198207
199208 * resetCurrentPageNumber ( ) {
@@ -269,6 +278,8 @@ class RootSaga {
269278 this . pageStorage . reset ( ) ;
270279 this . pagesCache . resetPagesCache ( ) ;
271280 this . djvuWorker . reset ( ) ;
281+ // we don't have to reset it, since the worker was recreated and all memory was released in any case
282+ this . continuousScrollManager = null ;
272283 this . isBundling = false ;
273284 this . documentContructorData = null ;
274285 }
@@ -277,23 +288,23 @@ class RootSaga {
277288 this . callbacks [ action . callbackName ] = action . callback ;
278289 }
279290
280- * switchToContinuousScrollMode ( ) {
291+ * switchToContinuousScrollMode ( notSave = false ) {
281292 this . djvuWorker . cancelAllTasks ( ) ;
282293 if ( ! this . continuousScrollManager ) {
283294 yield * this . prepareForContinuousMode ( ) ;
284295 }
285296 this . pagesCache . resetPagesCache ( ) ;
286297 yield * this . resetCurrentPageNumber ( ) ;
287- yield put ( { type : ActionTypes . UPDATE_OPTIONS , payload : { preferContinuousScroll : true } } ) ;
298+ if ( ! notSave ) yield put ( { type : ActionTypes . UPDATE_OPTIONS , payload : { preferContinuousScroll : true } } ) ;
288299 }
289300
290- * switchToSinglePageMode ( ) {
301+ * switchToSinglePageMode ( notSave = false ) {
291302 this . djvuWorker . cancelAllTasks ( ) ;
292303 if ( this . continuousScrollManager ) {
293304 yield * this . continuousScrollManager . reset ( ) ;
294305 }
295306 yield * this . resetCurrentPageNumber ( ) ;
296- yield put ( { type : ActionTypes . UPDATE_OPTIONS , payload : { preferContinuousScroll : false } } ) ;
307+ if ( ! notSave ) yield put ( { type : ActionTypes . UPDATE_OPTIONS , payload : { preferContinuousScroll : false } } ) ;
297308 }
298309
299310 * switchToTextMode ( ) {
@@ -304,6 +315,23 @@ class RootSaga {
304315 yield * this . fetchPageTextIfRequired ( ) ;
305316 }
306317
318+ * handleViewModeSwitch ( { notSave = false } = { } ) {
319+ const isLoaded = yield select ( get . isDocumentLoaded ) ;
320+ const viewMode = yield select ( get . viewMode ) ;
321+ if ( ! isLoaded ) return ;
322+
323+ switch ( viewMode ) {
324+ case Constants . CONTINUOUS_SCROLL_MODE :
325+ return yield * this . switchToContinuousScrollMode ( notSave ) ;
326+ case Constants . SINGLE_PAGE_MODE :
327+ return yield * this . switchToSinglePageMode ( notSave ) ;
328+ case Constants . TEXT_MODE :
329+ return yield * this . switchToTextMode ( ) ;
330+ default :
331+ throw new Error ( 'Invalid view mode: ' + payload ) ;
332+ }
333+ }
334+
307335 * updateOptions ( action ) {
308336 if ( action . notSave ) return ;
309337
@@ -464,9 +492,7 @@ class RootSaga {
464492 yield takeLatest ( ActionTypes . SAVE_DOCUMENT , this . withErrorHandler ( this . saveDocument ) ) ;
465493 yield takeLatest ( Constants . CLOSE_DOCUMENT_ACTION , this . withErrorHandler ( this . resetWorkerAndStorages ) ) ;
466494 yield takeLatest ( Constants . SET_API_CALLBACK_ACTION , this . withErrorHandler ( this . setCallback ) ) ;
467- yield takeLatest ( Constants . ENABLE_CONTINUOUS_SCROLL_MODE_ACTION , this . withErrorHandler ( this . switchToContinuousScrollMode ) ) ;
468- yield takeLatest ( Constants . ENABLE_SINGLE_PAGE_MODE_ACTION , this . withErrorHandler ( this . switchToSinglePageMode ) ) ;
469- yield takeLatest ( Constants . ENABLE_TEXT_MODE_ACTION , this . withErrorHandler ( this . switchToTextMode ) ) ;
495+ yield takeLatest ( ActionTypes . SET_VIEW_MODE , this . withErrorHandler ( this . handleViewModeSwitch ) ) ;
470496 yield takeLatest ( ActionTypes . UPDATE_OPTIONS , this . withErrorHandler ( this . updateOptions ) ) ;
471497 yield takeLatest ( ActionTypes . CONFIGURE , this . withErrorHandler ( this . configure ) ) ;
472498 yield takeLatest ( ActionTypes . LOAD_DOCUMENT_BY_URL , this . loadDocumentByUrl . bind ( this ) ) ;
0 commit comments