Skip to content

Commit

Permalink
[preview] Expose a default materializePaths function (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Jan 30, 2018
1 parent 003a049 commit 4eb8590
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/@sanity/base/src/preview/index.js
@@ -1,2 +1,2 @@
export {SanityPreview as default, PreviewSubscriber, PreviewFields} from '@sanity/preview/components'
export {observeForPreview, observeWithPaths} from '@sanity/preview'
export {observeForPreview, observeWithPaths, materializePaths} from '@sanity/preview'
1 change: 1 addition & 0 deletions packages/@sanity/preview/index.js
@@ -1,2 +1,3 @@
exports.observeForPreview = require('./lib/observeForPreview').default
exports.observeWithPaths = require('./lib/observeWithPaths').default
exports.materializePaths = require('./lib/materializePaths').default
@@ -1,13 +1,9 @@
import {uniq, isObject, isArray} from 'lodash'
import {isArray, isObject, uniq} from 'lodash'
import Observable from '@sanity/observable'
import {configure} from 'observable-props'

const props = configure({Observable})

function resolveMissingHeads(value, paths) {
return paths.filter(path => !(path[0] in value))
}

function isReference(value) {
return '_ref' in value
}
Expand All @@ -23,9 +19,12 @@ function createEmpty(keys) {
}, {})
}

export default function createPreviewObserver(observeWithPaths) {
function resolveMissingHeads(value, paths) {
return paths.filter(path => !(path[0] in value))
}

function follow(value, paths) {
export default function createPathMaterializer(observeWithPaths) {
return function materializePaths(value, paths) {
if (!isArray(value) && !isObject(value)) {
// Reached a leaf. Don't blow up
return Observable.of(value)
Expand All @@ -41,7 +40,7 @@ export default function createPreviewObserver(observeWithPaths) {
const id = isReference(value) ? value._ref : value._id
return observeWithPaths(id, nextHeads)
.switchMap(snapshot => {
return follow({...createEmpty(nextHeads), ...value, ...snapshot}, paths)
return materializePaths({...createEmpty(nextHeads), ...value, ...snapshot}, paths)
})
}
}
Expand All @@ -60,13 +59,11 @@ export default function createPreviewObserver(observeWithPaths) {
if (tails.every(tail => tail.length === 0)) {
res[head] = value[head]
} else {
res[head] = follow(value[head], tails)
res[head] = materializePaths(value[head], tails)
}
return res
}, {...value})

return props(Observable.of(next), {wait: true})
}

return follow
}
4 changes: 4 additions & 0 deletions packages/@sanity/preview/src/materializePaths.js
@@ -0,0 +1,4 @@
import observeWithPaths from './observeWithPaths'
import createPathMaterializer from './createPathMaterializer'

export default createPathMaterializer(observeWithPaths)
7 changes: 2 additions & 5 deletions packages/@sanity/preview/src/observeForPreview.js
@@ -1,10 +1,7 @@
import createPreviewObserver from './createPreviewObserver'
import observeWithPaths from './observeWithPaths'
import resolveRefType from './resolveRefType'
import prepareForPreview, {invokePrepare} from './prepareForPreview'
import Observable from '@sanity/observable'

const observe = createPreviewObserver(observeWithPaths)
import materializePaths from './materializePaths'

function is(typeName, type) {
return type.name === typeName || (type.type && is(typeName, type.type))
Expand Down Expand Up @@ -34,7 +31,7 @@ export default function observeForPreview(value, type, fields, viewOptions) {
const configFields = Object.keys(selection)
const targetFields = fields ? configFields.filter(fieldName => fields.includes(fieldName)) : configFields
const paths = targetFields.map(key => selection[key].split('.'))
return observe(value, paths)
return materializePaths(value, paths)
.map(snapshot => ({
type: type,
snapshot: prepareForPreview(snapshot, type, viewOptions)
Expand Down

0 comments on commit 4eb8590

Please sign in to comment.