Skip to content

Commit

Permalink
Fixed possible edge cases for canceled pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
a-ignatov-parc committed Nov 27, 2017
1 parent 30ad713 commit 35832a7
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,18 @@ function createDocument(template, payload) {
}

export function parseDocument(template) {
return createPipeline().pipe(passthrough(payload => ({
parsedDocument: createDocument(template, payload),
})));
return createPipeline().pipe(passthrough((payload) => {
const { possiblyDismissedByUser } = (payload || {}).renderedDocument || {};

let parsedDocument = null;

// We don't want to process documents in canceled pipeline.
if (!possiblyDismissedByUser) {
parsedDocument = createDocument(template, payload);
}

return { parsedDocument };
}));
}

export function removeModal() {
Expand Down Expand Up @@ -67,16 +76,25 @@ export function render(template) {
route,
redirect,
navigation = {},
} = payload;

let {
parsedDocument: document,
document: renderedDocument,
} = payload;

let { document: renderedDocument } = payload;

const { menuBar, menuItem } = navigation;
const { possiblyDismissedByUser } = renderedDocument || {};

/**
* If we received dismissed document it means that user pressed menu
* button and our pipeline is canceled. Now we must silently succeed
* the rest of the pipeline without rendering anything.
*/
if (possiblyDismissedByUser) {
// eslint-disable-next-line max-len
console.warn('Rendering pipeline was canceled by user. Skipping further renders...');
return null;
}

const prevRouteDocument = renderedDocument
? renderedDocument.prevRouteDocument
: navigationDocument.documents.slice(-1)[0];
Expand All @@ -102,25 +120,13 @@ export function render(template) {
menuBar.setDocument(document, menuItem);
}, RENDERING_ANIMATION);
}
} else if (possiblyDismissedByUser) {
/**
* Because this stage should be noop we need to restore last
* rendered document.
*/
document = payload.renderedDocument;

// eslint-disable-next-line max-len
console.warn('Rendering pipeline was terminated by user. Skipping further renders...');
} else if (renderedDocument) {
navigationDocument.replaceDocument(document, renderedDocument);
} else {
navigationDocument.pushDocument(document);
}

return {
document,
redirect: false,
};
return { document, redirect: false };
}))
.pipe(passthrough(() => promisedTimeout(RENDERING_ANIMATION)));
}

0 comments on commit 35832a7

Please sign in to comment.