123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561 |
- ;(function (factory) {
- var objectTypes = {
- 'function': true,
- 'object': true
- };
- function checkGlobal(value) {
- return (value && value.Object === Object) ? value : null;
- }
- var freeExports = (objectTypes[typeof exports] && exports && !exports.nodeType) ? exports : null;
- var freeModule = (objectTypes[typeof module] && module && !module.nodeType) ? module : null;
- var freeGlobal = checkGlobal(freeExports && freeModule && typeof global === 'object' && global);
- var freeSelf = checkGlobal(objectTypes[typeof self] && self);
- var freeWindow = checkGlobal(objectTypes[typeof window] && window);
- var moduleExports = (freeModule && freeModule.exports === freeExports) ? freeExports : null;
- var thisGlobal = checkGlobal(objectTypes[typeof this] && this);
- var root = freeGlobal || ((freeWindow !== (thisGlobal && thisGlobal.window)) && freeWindow) || freeSelf || thisGlobal || Function('return this')();
-
- if (typeof define === 'function' && define.amd) {
- define(['./rx'], function (Rx, exports) {
- return factory(root, exports, Rx);
- });
- } else if (typeof module === 'object' && module && module.exports === freeExports) {
- module.exports = factory(root, module.exports, require('./rx'));
- } else {
- root.Rx = factory(root, {}, root.Rx);
- }
- }.call(this, function (root, exp, Rx, undefined) {
- var Observable = Rx.Observable,
- observableProto = Observable.prototype,
- AnonymousObservable = Rx.AnonymousObservable,
- ObservableBase = Rx.ObservableBase,
- Subject = Rx.Subject,
- AsyncSubject = Rx.AsyncSubject,
- Observer = Rx.Observer,
- ScheduledObserver = Rx.internals.ScheduledObserver,
- disposableCreate = Rx.Disposable.create,
- disposableEmpty = Rx.Disposable.empty,
- BinaryDisposable = Rx.BinaryDisposable,
- currentThreadScheduler = Rx.Scheduler.currentThread,
- isFunction = Rx.helpers.isFunction,
- inherits = Rx.internals.inherits,
- addProperties = Rx.internals.addProperties,
- checkDisposed = Rx.Disposable.checkDisposed;
-
- function cloneArray(arr) {
- var len = arr.length, a = new Array(len);
- for(var i = 0; i < len; i++) { a[i] = arr[i]; }
- return a;
- }
- var MulticastObservable = (function (__super__) {
- inherits(MulticastObservable, __super__);
- function MulticastObservable(source, fn1, fn2) {
- this.source = source;
- this._fn1 = fn1;
- this._fn2 = fn2;
- __super__.call(this);
- }
- MulticastObservable.prototype.subscribeCore = function (o) {
- var connectable = this.source.multicast(this._fn1());
- return new BinaryDisposable(this._fn2(connectable).subscribe(o), connectable.connect());
- };
- return MulticastObservable;
- }(ObservableBase));
-
- observableProto.multicast = function (subjectOrSubjectSelector, selector) {
- return isFunction(subjectOrSubjectSelector) ?
- new MulticastObservable(this, subjectOrSubjectSelector, selector) :
- new ConnectableObservable(this, subjectOrSubjectSelector);
- };
-
- observableProto.publish = function (selector) {
- return selector && isFunction(selector) ?
- this.multicast(function () { return new Subject(); }, selector) :
- this.multicast(new Subject());
- };
-
- observableProto.share = function () {
- return this.publish().refCount();
- };
-
- observableProto.publishLast = function (selector) {
- return selector && isFunction(selector) ?
- this.multicast(function () { return new AsyncSubject(); }, selector) :
- this.multicast(new AsyncSubject());
- };
-
- observableProto.publishValue = function (initialValueOrSelector, initialValue) {
- return arguments.length === 2 ?
- this.multicast(function () {
- return new BehaviorSubject(initialValue);
- }, initialValueOrSelector) :
- this.multicast(new BehaviorSubject(initialValueOrSelector));
- };
-
- observableProto.shareValue = function (initialValue) {
- return this.publishValue(initialValue).refCount();
- };
-
- observableProto.replay = function (selector, bufferSize, windowSize, scheduler) {
- return selector && isFunction(selector) ?
- this.multicast(function () { return new ReplaySubject(bufferSize, windowSize, scheduler); }, selector) :
- this.multicast(new ReplaySubject(bufferSize, windowSize, scheduler));
- };
-
- observableProto.shareReplay = function (bufferSize, windowSize, scheduler) {
- return this.replay(null, bufferSize, windowSize, scheduler).refCount();
- };
- var InnerSubscription = function (s, o) {
- this._s = s;
- this._o = o;
- };
- InnerSubscription.prototype.dispose = function () {
- if (!this._s.isDisposed && this._o !== null) {
- var idx = this._s.observers.indexOf(this._o);
- this._s.observers.splice(idx, 1);
- this._o = null;
- }
- };
-
- var BehaviorSubject = Rx.BehaviorSubject = (function (__super__) {
- inherits(BehaviorSubject, __super__);
- function BehaviorSubject(value) {
- __super__.call(this);
- this.value = value;
- this.observers = [];
- this.isDisposed = false;
- this.isStopped = false;
- this.hasError = false;
- }
- addProperties(BehaviorSubject.prototype, Observer.prototype, {
- _subscribe: function (o) {
- checkDisposed(this);
- if (!this.isStopped) {
- this.observers.push(o);
- o.onNext(this.value);
- return new InnerSubscription(this, o);
- }
- if (this.hasError) {
- o.onError(this.error);
- } else {
- o.onCompleted();
- }
- return disposableEmpty;
- },
-
- getValue: function () {
- checkDisposed(this);
- if (this.hasError) { thrower(this.error); }
- return this.value;
- },
-
- hasObservers: function () { checkDisposed(this); return this.observers.length > 0; },
-
- onCompleted: function () {
- checkDisposed(this);
- if (this.isStopped) { return; }
- this.isStopped = true;
- for (var i = 0, os = cloneArray(this.observers), len = os.length; i < len; i++) {
- os[i].onCompleted();
- }
- this.observers.length = 0;
- },
-
- onError: function (error) {
- checkDisposed(this);
- if (this.isStopped) { return; }
- this.isStopped = true;
- this.hasError = true;
- this.error = error;
- for (var i = 0, os = cloneArray(this.observers), len = os.length; i < len; i++) {
- os[i].onError(error);
- }
- this.observers.length = 0;
- },
-
- onNext: function (value) {
- checkDisposed(this);
- if (this.isStopped) { return; }
- this.value = value;
- for (var i = 0, os = cloneArray(this.observers), len = os.length; i < len; i++) {
- os[i].onNext(value);
- }
- },
-
- dispose: function () {
- this.isDisposed = true;
- this.observers = null;
- this.value = null;
- this.error = null;
- }
- });
- return BehaviorSubject;
- }(Observable));
-
- var ReplaySubject = Rx.ReplaySubject = (function (__super__) {
- var maxSafeInteger = Math.pow(2, 53) - 1;
- function createRemovableDisposable(subject, observer) {
- return disposableCreate(function () {
- observer.dispose();
- !subject.isDisposed && subject.observers.splice(subject.observers.indexOf(observer), 1);
- });
- }
- inherits(ReplaySubject, __super__);
-
- function ReplaySubject(bufferSize, windowSize, scheduler) {
- this.bufferSize = bufferSize == null ? maxSafeInteger : bufferSize;
- this.windowSize = windowSize == null ? maxSafeInteger : windowSize;
- this.scheduler = scheduler || currentThreadScheduler;
- this.q = [];
- this.observers = [];
- this.isStopped = false;
- this.isDisposed = false;
- this.hasError = false;
- this.error = null;
- __super__.call(this);
- }
- addProperties(ReplaySubject.prototype, Observer.prototype, {
- _subscribe: function (o) {
- checkDisposed(this);
- var so = new ScheduledObserver(this.scheduler, o), subscription = createRemovableDisposable(this, so);
- this._trim(this.scheduler.now());
- this.observers.push(so);
- for (var i = 0, len = this.q.length; i < len; i++) {
- so.onNext(this.q[i].value);
- }
- if (this.hasError) {
- so.onError(this.error);
- } else if (this.isStopped) {
- so.onCompleted();
- }
- so.ensureActive();
- return subscription;
- },
-
- hasObservers: function () { checkDisposed(this); return this.observers.length > 0; },
- _trim: function (now) {
- while (this.q.length > this.bufferSize) {
- this.q.shift();
- }
- while (this.q.length > 0 && (now - this.q[0].interval) > this.windowSize) {
- this.q.shift();
- }
- },
-
- onNext: function (value) {
- checkDisposed(this);
- if (this.isStopped) { return; }
- var now = this.scheduler.now();
- this.q.push({ interval: now, value: value });
- this._trim(now);
- for (var i = 0, os = cloneArray(this.observers), len = os.length; i < len; i++) {
- var observer = os[i];
- observer.onNext(value);
- observer.ensureActive();
- }
- },
-
- onError: function (error) {
- checkDisposed(this);
- if (this.isStopped) { return; }
- this.isStopped = true;
- this.error = error;
- this.hasError = true;
- var now = this.scheduler.now();
- this._trim(now);
- for (var i = 0, os = cloneArray(this.observers), len = os.length; i < len; i++) {
- var observer = os[i];
- observer.onError(error);
- observer.ensureActive();
- }
- this.observers.length = 0;
- },
-
- onCompleted: function () {
- checkDisposed(this);
- if (this.isStopped) { return; }
- this.isStopped = true;
- var now = this.scheduler.now();
- this._trim(now);
- for (var i = 0, os = cloneArray(this.observers), len = os.length; i < len; i++) {
- var observer = os[i];
- observer.onCompleted();
- observer.ensureActive();
- }
- this.observers.length = 0;
- },
-
- dispose: function () {
- this.isDisposed = true;
- this.observers = null;
- }
- });
- return ReplaySubject;
- }(Observable));
- var RefCountObservable = (function (__super__) {
- inherits(RefCountObservable, __super__);
- function RefCountObservable(source) {
- this.source = source;
- this._count = 0;
- this._connectableSubscription = null;
- __super__.call(this);
- }
- RefCountObservable.prototype.subscribeCore = function (o) {
- var subscription = this.source.subscribe(o);
- ++this._count === 1 && (this._connectableSubscription = this.source.connect());
- return new RefCountDisposable(this, subscription);
- };
- function RefCountDisposable(p, s) {
- this._p = p;
- this._s = s;
- this.isDisposed = false;
- }
- RefCountDisposable.prototype.dispose = function () {
- if (!this.isDisposed) {
- this.isDisposed = true;
- this._s.dispose();
- --this._p._count === 0 && this._p._connectableSubscription.dispose();
- }
- };
- return RefCountObservable;
- }(ObservableBase));
- var ConnectableObservable = Rx.ConnectableObservable = (function (__super__) {
- inherits(ConnectableObservable, __super__);
- function ConnectableObservable(source, subject) {
- this.source = source;
- this._connection = null;
- this._source = source.asObservable();
- this._subject = subject;
- __super__.call(this);
- }
- function ConnectDisposable(parent, subscription) {
- this._p = parent;
- this._s = subscription;
- }
- ConnectDisposable.prototype.dispose = function () {
- if (this._s) {
- this._s.dispose();
- this._s = null;
- this._p._connection = null;
- }
- };
- ConnectableObservable.prototype.connect = function () {
- if (!this._connection) {
- if (this._subject.isStopped) {
- return disposableEmpty;
- }
- var subscription = this._source.subscribe(this._subject);
- this._connection = new ConnectDisposable(this, subscription);
- }
- return this._connection;
- };
- ConnectableObservable.prototype._subscribe = function (o) {
- return this._subject.subscribe(o);
- };
- ConnectableObservable.prototype.refCount = function () {
- return new RefCountObservable(this);
- };
- return ConnectableObservable;
- }(Observable));
-
- observableProto.singleInstance = function() {
- var source = this, hasObservable = false, observable;
- function getObservable() {
- if (!hasObservable) {
- hasObservable = true;
- observable = source['finally'](function() { hasObservable = false; }).publish().refCount();
- }
- return observable;
- }
- return new AnonymousObservable(function(o) {
- return getObservable().subscribe(o);
- });
- };
- return Rx;
- }));
|