From c1e0ca30322adcb9a60662c06756d994a964f9b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rge=20N=C3=A6ss?= Date: Thu, 15 Feb 2018 23:26:15 +0700 Subject: [PATCH] [preview] Optimize preview fetching --- .../@sanity/preview/src/observeWithPaths.js | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/@sanity/preview/src/observeWithPaths.js b/packages/@sanity/preview/src/observeWithPaths.js index d09efaea592..8f3a76aee3f 100644 --- a/packages/@sanity/preview/src/observeWithPaths.js +++ b/packages/@sanity/preview/src/observeWithPaths.js @@ -6,37 +6,35 @@ import {combineSelections, reassemble, toGradientQuery} from './utils/optimizeQu let _globalListener const getGlobalListener = () => { if (!_globalListener) { - _globalListener = Observable - .from(client.listen('*[!(_id in path("_.**"))]', {}, {includeResult: false})) - .share() + _globalListener = Observable.from( + client.listen('*[!(_id in path("_.**"))]', {}, {includeResult: false}) + ).share() } return _globalListener } function listen(id) { - return Observable.of({type: 'welcome', documentId: id}) - .concat(getGlobalListener()) - .filter(event => event.documentId === id) + return getGlobalListener().filter(event => event.documentId === id) } -function fetchAllDocumentSnapshots(selections) { +function fetchAllDocumentPaths(selections) { const combinedSelections = combineSelections(selections) return client.observable .fetch(toGradientQuery(combinedSelections)) .map(result => reassemble(result, combinedSelections)) } -const debouncedFetchDocumentSnapshot = debounceCollect(fetchAllDocumentSnapshots, 50) +const fetchDocumentPathsFast = debounceCollect(fetchAllDocumentPaths, 0) +const fetchDocumentPathsSlow = debounceCollect(fetchAllDocumentPaths, 1000) // todo: keep for debugging purposes for now -// function fetchDocumentSnapshot(id, selection) { +// function fetchDocumentPaths(id, selection) { // return client.observable.fetch(`*[_id==$id]{_id,_type,${selection.join(',')}}`, {id}) // .map(result => result[0]) // } export default function observeWithPaths(id, paths) { - return debouncedFetchDocumentSnapshot(id, paths) - .concat(listen(id) - .debounceTime(1000) - .switchMap(event => debouncedFetchDocumentSnapshot(id, paths))) + return fetchDocumentPathsFast(id, paths).concat( + listen(id).switchMap(event => fetchDocumentPathsSlow(id, paths)) + ) }