Skip to content

Instantly share code, notes, and snippets.

@DobrinGanev
Created September 14, 2017 20:00
Show Gist options
  • Save DobrinGanev/6e30bc7c9ba0c575c07053b4cbee6471 to your computer and use it in GitHub Desktop.
Save DobrinGanev/6e30bc7c9ba0c575c07053b4cbee6471 to your computer and use it in GitHub Desktop.
redux createStore returns [$$observable]: observable
/**
this is from the Redux createStore()
/**
* Interoperability point for observable/reactive libraries.
* @returns {observable} A minimal observable of state changes.
* For more information, see the observable proposal:
* https://github.com/tc39/proposal-observable
*/
function observable() {
const outerSubscribe = subscribe
return {
/**
* The minimal observable subscription method.
* @param {Object} observer Any object that can be used as an observer.
* The observer object should have a `next` method.
* @returns {subscription} An object with an `unsubscribe` method that can
* be used to unsubscribe the observable from the store, and prevent further
* emission of values from the observable.
*/
subscribe(observer) {
if (typeof observer !== 'object') {
throw new TypeError('Expected the observer to be an object.')
}
function observeState() {
if (observer.next) {
observer.next(getState())
}
}
observeState()
const unsubscribe = outerSubscribe(observeState)
return {
unsubscribe
}
},
[$$observable]() {
return this
}
}
}
*/
//we can use it like this:
import $$observable from 'symbol-observable'
const obj = {
next: (state) => { // need to provide next method
console.log(state)
}
}
const observerOne = store[$$observable]().subscribe(obj)
observerOne.unsubscribe()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment