4

I want to convert a function:

static getCurrentPosition(geo_success, geo_error?, geo_options?)

to an observable.

As you can see, the first argument to the function is the success callback.

RxJS 5 bindCallback works on functions where the success callback is the last parameter.

Is there a way to use bindCallback on static getCurrentPosition(geo_success, geo_error?, geo_options?) ?

If not then I will manually convert the function to a Promise like number 2 here

The function in question can be found here

I would like to implement this as an observable:

navigator.geolocation.getCurrentPosition(
  (position) => {
    updateRegion(position)
    store.dispatch(getCurrentLocation(position))
  },
  error => Observable.of(getCurrentPositionRejected(error)),
  { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
)

My attempt at implementing the answer below:

    class Vepo extends Component {
      componentDidMount() {
        const { store } = this.context
        this.unsubscribe = store.subscribe(() => { })
        store.dispatch(fetchCategories())
        store.dispatch(getCurrentPosition())

************implementation starts here******************************
        const bound = Observable.bindCallback((options, cb) => {
          if (typeof options === 'function') {
            cb = options
            options = null
          }

          navigator.geolocation.getCurrentPosition(cb, null, options)
        })
       let x = bound(this.getPosition)

        x.subscribe(
        function (x) {
          console.log('Next: %s', x)
        },
        function (err) {
          console.log('Error: %s', err)
        },
        function () {
          console.log('Completed')
        })

       x.onNext()
      }

      getPosition(position) {
        console.log(position)
        return position
      }

And it console.log()'s only from inside getPosition()

Community
  • 1
  • 1
BeniaminoBaggins
  • 11,202
  • 41
  • 152
  • 287

1 Answers1

6

Create a new function with rearranged arguments

const bound = Rx.Observable.bindCallback((options, cb) => {
  if(typeof options === 'function') {
    cb = options
    options = null
  }

  navigator.geolocation.getCurrentPosition(cb, null, options)
})

Or use bindNodeCallback if you want to handle errors.

Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98
  • Thanks. I've updated my question with my attempt to use. Do you know how to trigger the onNext? It is only `console.log()`ing from inside `getPosition()`. – BeniaminoBaggins May 02 '17 at 09:39
  • @BeniaminoBaggins Not sure I understand your question correctly. You shouldn't mannually trigger `onNext`. You need to call `bound` with correct options. `const currentPosition$ = bound()` and subscribe on `currentPosition$.subscribe(position => console.log(position))` – Yury Tarabanko May 02 '17 at 13:55
  • Ohhhhh so the "callback function" or "success case" goes inside the `subscribe`, not inside the `const currentPosition$ = bound()`? – BeniaminoBaggins May 02 '17 at 20:33
  • I see your example has an `options` param. do my options go inside `currentPosition$ = bound({option1: option1Value})`? And the callback, goes inside the `subscribe`? – BeniaminoBaggins May 02 '17 at 20:39
  • 1
    @BeniaminoBaggins Right `options` go to observable factory. `callback` goes to `subscribe` – Yury Tarabanko May 03 '17 at 06:48