How to use the @sanity/observable function in @sanity/observable

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 / src / datastores / document / observableUtils.js View on Github external
export function timer(initialDelay, period) {
  return new Observable(observer => {
    let n = 0
    let intervalId = -1

    const timeoutId = setTimeout(() => {
      emitNext()
      if (period !== undefined) {
        intervalId = setInterval(emitNext, period)
      }
    }, initialDelay)

    function emitNext() {
      observer.next(n++)
    }
    return function dispose() {
      clearTimeout(timeoutId)
      clearInterval(intervalId)
github sanity-io / sanity / packages / @sanity / base / src / preview / observeWithPaths.js View on Github external
function listen(id) {
  return new Observable(observer => {
    observer.next({type: 'welcome', documentId: id})
    return globalListener
      .filter(event => event.documentId === id)
      .debounceTime(2000)
      .subscribe(observer)
  })
}
github sanity-io / sanity / packages / @sanity / base / src / preview / scrollPosition.js View on Github external
import Observable from '@sanity/observable'

function getWindowDimensions() {
  return {
    height: window.innerHeight,
    width: window.innerWidth
  }
}

const EVENTS = ['scroll', 'resize']

const scrollObserver = new Observable(observer => {
  const listener = () => observer.next(getWindowDimensions())
  EVENTS.forEach(eventType => {
    document.body.addEventListener(eventType, listener, true)
  })

  return () => {
    EVENTS.forEach(eventType => {
      document.body.removeEventListener(eventType, listener, true)
    })
  }
})
  .debounceTime(100)
  .share()

export default new Observable(observer => {
  observer.next(getWindowDimensions())

@sanity/observable

[deprecated] This package exists for legacy reasons only

MIT
Latest version published 4 years ago

Package Health Score

78 / 100
Full package analysis