-
Notifications
You must be signed in to change notification settings - Fork 8
asObservable
richardszalay edited this page May 20, 2011
·
7 revisions
Hides the source sequence so it cannot be cast back to it’s concrete implementation. Often used with Subjects to avoid the consumer being able to call on*() on them.
function asObservable() : IObservable.<T>
Useful for Subjects and custom implementations of IObservable, where you would not want calling code casting the sequence back to the original implementation
The returned sequence completes if the source sequence completes
The returned sequence errors if the source sequence errors or if func throws an error
xs ──o─────o─────/
│ │ │
zs ──o─────o─────/
xs ──o─────o─────x
│ │ │
zs ──o─────o─────x
IObservable.<T>
var subject : Subject = new Subject();
// source cannot be cast back to Subject
var source : IObservable = subject.asObservable();
source.subscribe(
function(x:int) : void { trace(x); },
function() : void { trace("Completed"); }
);
subject.onNext(1);
subject.onNext(2);
subject.onNext(3);
subject.onCompleted();
// Trace output is:
// 1
// 2
// 3
// Completed