What is the best way to determine if an object is an instance of the Stream class?

Is there a way to determine if an object is an instance of the stream class? For example, RxJS stream or Bacon.js.

What I am looking for is something like

function isStream(obj) {
   // if obj is RxJS or Bacon Stream return true, otherwise false
}

      

What's the safest way to do this?

+3


source to share


2 answers


Observable

is the base class from which objects EventStream

and Property

. Therefore, if you want to discover something bacon, you can use Observable

.

function isStream(v) {
  return v instanceof Bacon.Observable
}

function test(v) {
  console.log(isStream(v))
}

test(Bacon.constant(1)) // true
test(Bacon.once(1))     // true
test(1)                 // false

      



http://jsbin.com/qugihobalu/2/edit

+2


source


There may be better ways in each structure, for example. isStream's own equivalent, but instanceof check is the next best solution and works for both bacon and rxjs.



const isStream = x => x instanceof Bacon.Observable || x instanceof Rx.Observable;

      

+2


source







All Articles