3

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

What I'm looking for is something like

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

What is the most reliable way of doing this?

Tero Tolonen
  • 4,144
  • 4
  • 27
  • 32
  • What do you need this for? This seems to be more a [XY problem](http://meta.stackexchange.com/q/66377) – Bergi May 31 '15 at 12:59
  • When you are creating UI which is reactive it can get values in different formats, like a reactive UI which can get text, object or stream like this http://jsfiddle.net/8j6edrp9/ – Tero Tolonen May 31 '15 at 13:26
  • 1
    Just let it always take a stream. Even if it's a constant stream. Also you mostly don't want arbitrary objects anyway. Try to distinguish between stream objects, arrays of other items, and primitive strings (text). – Bergi May 31 '15 at 13:37
  • No, the problem is how to detect if it is stream or not, it may be a promise, text, HTML or some other object - how to detect if it is a stream is important here. I'm looking for a generic answer to the question, not specific answer to some other problem – Tero Tolonen May 31 '15 at 13:40
  • Have you already tried the instanceof operator? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof – smendola May 31 '15 at 14:28
  • @smendola sort of, the question is, what should you be asking with "instanceof", the Bacon in the global scope is an object, so you can't use that, I have not yet found what is the constructor to be used – Tero Tolonen May 31 '15 at 14:34
  • It could be instanceof Bacon.Observable or instanceof Bacon.Property, but I'm not sure which is the right way to go here, here is something related http://stackoverflow.com/questions/29090514/bacon-js-bus-plug-uncaught-error-not-an-observable-object-object – Tero Tolonen May 31 '15 at 14:38
  • In my case I use it so https://github.com/xgrommx/rxfy-react/blob/master/app/app.js#L14 – xgrommx May 31 '15 at 17:21
  • 1
    Observable is the base class that both EventStream and Property objects inherit from. So if you want to detect anything bacon, you could use Observable. – OlliM Jun 01 '15 at 06:16
  • @OlliM Thanks! That should be the right way to go. – Tero Tolonen Jun 01 '15 at 06:22
  • I still doubt that detecting bacon observables is the right architectural solution for your code. I usually try to keep everything as a observables, using `Bacon.fromPromise` to convert promises and sometimes even `Bacon.once` or `Bacon.constant` for plain values. – OlliM Jun 01 '15 at 06:36
  • Yes, well that's another topic :) In short, for the usability point of view, it is important to be able to say o.h1().text("Hello"); or o.attr({ fill : "red" } ) or to change the text with object like o.attr( { fill : stream } ). Forcing to use something specific is a bit scary – Tero Tolonen Jun 01 '15 at 06:50
  • As for Rx, you can't check easily, as some observables don't share same interfance: e.g `Rx.ConnectableObservable instanceof Rx.Observable === false`. – avetisk Jan 22 '16 at 10:07

2 Answers2

2

Observable is the base class that both EventStream and Property objects inherit from. So if you want to detect anything bacon, you could 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

OlliM
  • 7,023
  • 1
  • 36
  • 47
  • I copied my comment as an answer. – OlliM Jun 01 '15 at 13:08
  • Good answer, RxJS detect is still missing - because I asked that too can't accept it as the right answer. The other thing is that the test only works if Bacon is defined in the environment, but that's a minor detail – Tero Tolonen Jun 03 '15 at 10:00
2

There might be better ways in each framework, e.g. a native isStream equivalent, but checking instanceof is the next best solution and works for both bacon and rxjs.

const isStream = x => x instanceof Bacon.Observable || x instanceof Rx.Observable;
low_ghost
  • 570
  • 1
  • 5
  • 13