Skip to content

Instantly share code, notes, and snippets.

@kayhadrin
Created July 19, 2025 19:58
Show Gist options
  • Save kayhadrin/06ed1f26dd0c92fbbf1b1f7a12de4788 to your computer and use it in GitHub Desktop.
Save kayhadrin/06ed1f26dd0c92fbbf1b1f7a12de4788 to your computer and use it in GitHub Desktop.
Tip: detecting an Observable JS object
/*
See https://github.com/ReactiveX/rxjs/blob/5.0.2/src/util/subscribeToResult.ts#L70-L77
And also:
- https://github.com/ReactiveX/rxjs/blob/5.0.2/src/Observable.ts
- https://github.com/ReactiveX/rxjs/blob/5.0.2/src/symbol/observable.ts
*/
if (result instanceof Observable) {
if (result._isScalar) {
destination.next((<any>result).value);
destination.complete();
return null;
} else {
return result.subscribe(destination);
}
}
// ...
if (result && typeof result[$$observable] === 'function') {
const obs = result[$$observable]();
if (typeof obs.subscribe !== 'function') {
destination.error(new TypeError('Provided object does not correctly implement Symbol.observable'));
} else {
return obs.subscribe(new InnerSubscriber(outerSubscriber, outerValue, outerIndex));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment