How to use @sanity/observable - 10 common examples

To help you get started, we’ve selected a few @sanity/observable examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sanity-io / sanity / packages / @sanity / client / src / data / dataMethods.js View on Github external
const uri = this.getDataUrl(endpoint, stringQuery)

    const reqOptions = {
      method: useGet ? 'GET' : 'POST',
      uri: uri,
      json: true,
      body: useGet ? undefined : body,
      query: isMutation && getMutationQuery(options),
      timeout,
      token
    }

    return this._requestObservable(reqOptions).pipe(
      filter(isResponse),
      map(getBody),
      map(res => {
        if (!isMutation) {
          return res
        }

        // Should we return documents?
        const results = res.results || []
        if (options.returnDocuments) {
          return returnFirst ? results[0] && results[0].document : results.map(mut => mut.document)
        }

        // Return a reduced subset
        const key = returnFirst ? 'documentId' : 'documentIds'
        const ids = returnFirst ? results[0] && results[0].id : results.map(mut => mut.id)
        return {
          transactionId: res.transactionId,
          results: results,
github sanity-io / sanity / packages / @sanity / client / src / data / dataMethods.js View on Github external
const uri = this.getDataUrl(endpoint, stringQuery)

    const reqOptions = {
      method: useGet ? 'GET' : 'POST',
      uri: uri,
      json: true,
      body: useGet ? undefined : body,
      query: isMutation && getMutationQuery(options),
      timeout,
      token
    }

    return this._requestObservable(reqOptions).pipe(
      filter(isResponse),
      map(getBody),
      map(res => {
        if (!isMutation) {
          return res
        }

        // Should we return documents?
        const results = res.results || []
        if (options.returnDocuments) {
          return returnFirst ? results[0] && results[0].document : results.map(mut => mut.document)
        }

        // Return a reduced subset
        const key = returnFirst ? 'documentId' : 'documentIds'
        const ids = returnFirst ? results[0] && results[0].id : results.map(mut => mut.id)
        return {
          transactionId: res.transactionId,
github sanity-io / sanity / packages / @sanity / form-builder / src / sanity / uploads / uploadQueue.js View on Github external
// @flow
import Multicast from '@sanity/observable/multicast'
import Observable from '@sanity/observable'

import {uploadImageAsset} from '../inputs/client-adapters/assets'

const uploadRequests$ = new Multicast

const CONCURRENCY = 4

const registry = []
export function createUploadId(file: File): number {
  const id = registry.length
  const events = new Multicast
  registry[id] = {
    file,
    events$: events.asObservable().do({
      complete: () => {
        registry[id] = null
      }}),
    _multicast: events
  }
  return id
github sanity-io / sanity / packages / @sanity / desk-tool / src / pane / EditorWrapper.js View on Github external
handleUnpublish = () => {
    const {documentId} = this.props
    const {published} = this.state

    let tx = client.observable.transaction().delete(getPublishedId(documentId))

    if (published.snapshot) {
      tx = tx.createIfNotExists({
        ...omit(published.snapshot, '_updatedAt'),
        _id: getDraftId(documentId)
      })
    }

    Observable.from(tx.commit())
      .map(result => ({
        type: 'success',
        result: result
      }))
      .catch(error =>
        Observable.of({
          type: 'error',
          message: `An error occurred while attempting to unpublish document.
        This usually means that you attempted to unpublish a document that other documents
        refers to.`,
          error
        })
      )
      .subscribe(result => {
        this.setStateIfMounted({transactionResult: result})
      })
github sanity-io / sanity / packages / @sanity / desk-tool / src / pane / EditorWrapper.js View on Github external
.catch(error =>
        Observable.of({
          type: 'error',
          message: `An error occurred while attempting to delete document.
        This usually means that you attempted to delete a document that other documents
        refers to.`,
          error
        })
      )
github sanity-io / sanity / packages / @sanity / base / src / preview / PreviewSubscriber.js View on Github external
subscribe(value, type, fields) {
    this.unsubscribe()

    const viewOptions = this.props.ordering
      ? {ordering: this.props.ordering}
      : {}

    const visibilityOn$ = Observable.of(!document.hidden)
      .merge(visibilityChange$.map(event => !event.target.hidden))

    const inViewport$ = intersectionObservableFor(this._element)
      .map(event => event.isIntersecting)

    this.subscription = visibilityOn$
      .distinctUntilChanged()
      .switchMap(isVisible => (isVisible ? inViewport$ : Observable.of(false)))
      .distinctUntilChanged()
      .switchMap(isInViewport => {
        return isInViewport
          ? observeForPreview(value, type, fields, viewOptions)
          : Observable.of(null)
      })
      .subscribe(result => {
        if (result) {
github sanity-io / sanity / packages / @sanity / base / src / preview / PreviewSubscriber.js View on Github external
      .switchMap(isVisible => (isVisible ? inViewport$ : Observable.of(false)))
      .distinctUntilChanged()
github sanity-io / sanity / packages / @sanity / base / src / preview / PreviewSubscriber.js View on Github external
.switchMap(isInViewport => {
        return isInViewport
          ? observeForPreview(value, type, fields, viewOptions)
          : Observable.of(null)
      })
      .subscribe(result => {
github sanity-io / sanity / packages / @sanity / client / src / data / dataMethods.js View on Github external
fetch(query, params, options = {}) {
    const mapResponse = options.filterResponse === false ? res => res : res => res.result

    const observable = this._dataRequest('query', {query, params}, options).pipe(map(mapResponse))
    return this.isPromiseAPI() ? toPromise(observable) : observable
  },
github sanity-io / sanity / packages / @sanity / client / src / sanityClient.js View on Github external
request(options) {
    const observable = this._requestObservable(options).pipe(
      filter(event => event.type === 'response'),
      map(event => event.body)
    )

    return this.isPromiseAPI() ? toPromise(observable) : observable
  }
})

@sanity/observable

[deprecated] This package exists for legacy reasons only

MIT
Latest version published 3 years ago

Package Health Score

78 / 100
Full package analysis