wallet-core/node_modules/core-js/modules/es7.observable.js

200 lines
5.4 KiB
JavaScript
Raw Normal View History

2017-05-28 00:38:50 +02:00
'use strict';
// https://github.com/zenparsing/es-observable
2017-08-14 05:01:11 +02:00
var $export = require('./_export');
var global = require('./_global');
var core = require('./_core');
var microtask = require('./_microtask')();
var OBSERVABLE = require('./_wks')('observable');
var aFunction = require('./_a-function');
var anObject = require('./_an-object');
var anInstance = require('./_an-instance');
var redefineAll = require('./_redefine-all');
var hide = require('./_hide');
var forOf = require('./_for-of');
var RETURN = forOf.RETURN;
2017-05-28 00:38:50 +02:00
2017-08-14 05:01:11 +02:00
var getMethod = function (fn) {
2017-05-28 00:38:50 +02:00
return fn == null ? undefined : aFunction(fn);
};
2017-08-14 05:01:11 +02:00
var cleanupSubscription = function (subscription) {
2017-05-28 00:38:50 +02:00
var cleanup = subscription._c;
2017-08-14 05:01:11 +02:00
if (cleanup) {
2017-05-28 00:38:50 +02:00
subscription._c = undefined;
cleanup();
}
};
2017-08-14 05:01:11 +02:00
var subscriptionClosed = function (subscription) {
2017-05-28 00:38:50 +02:00
return subscription._o === undefined;
};
2017-08-14 05:01:11 +02:00
var closeSubscription = function (subscription) {
if (!subscriptionClosed(subscription)) {
2017-05-28 00:38:50 +02:00
subscription._o = undefined;
cleanupSubscription(subscription);
}
};
2017-08-14 05:01:11 +02:00
var Subscription = function (observer, subscriber) {
2017-05-28 00:38:50 +02:00
anObject(observer);
this._c = undefined;
this._o = observer;
observer = new SubscriptionObserver(this);
try {
2017-08-14 05:01:11 +02:00
var cleanup = subscriber(observer);
var subscription = cleanup;
if (cleanup != null) {
if (typeof cleanup.unsubscribe === 'function') cleanup = function () { subscription.unsubscribe(); };
2017-05-28 00:38:50 +02:00
else aFunction(cleanup);
this._c = cleanup;
}
2017-08-14 05:01:11 +02:00
} catch (e) {
2017-05-28 00:38:50 +02:00
observer.error(e);
return;
2017-08-14 05:01:11 +02:00
} if (subscriptionClosed(this)) cleanupSubscription(this);
2017-05-28 00:38:50 +02:00
};
Subscription.prototype = redefineAll({}, {
2017-08-14 05:01:11 +02:00
unsubscribe: function unsubscribe() { closeSubscription(this); }
2017-05-28 00:38:50 +02:00
});
2017-08-14 05:01:11 +02:00
var SubscriptionObserver = function (subscription) {
2017-05-28 00:38:50 +02:00
this._s = subscription;
};
SubscriptionObserver.prototype = redefineAll({}, {
2017-08-14 05:01:11 +02:00
next: function next(value) {
2017-05-28 00:38:50 +02:00
var subscription = this._s;
2017-08-14 05:01:11 +02:00
if (!subscriptionClosed(subscription)) {
2017-05-28 00:38:50 +02:00
var observer = subscription._o;
try {
var m = getMethod(observer.next);
2017-08-14 05:01:11 +02:00
if (m) return m.call(observer, value);
} catch (e) {
2017-05-28 00:38:50 +02:00
try {
closeSubscription(subscription);
} finally {
throw e;
}
}
}
},
2017-08-14 05:01:11 +02:00
error: function error(value) {
2017-05-28 00:38:50 +02:00
var subscription = this._s;
2017-08-14 05:01:11 +02:00
if (subscriptionClosed(subscription)) throw value;
2017-05-28 00:38:50 +02:00
var observer = subscription._o;
subscription._o = undefined;
try {
var m = getMethod(observer.error);
2017-08-14 05:01:11 +02:00
if (!m) throw value;
2017-05-28 00:38:50 +02:00
value = m.call(observer, value);
2017-08-14 05:01:11 +02:00
} catch (e) {
2017-05-28 00:38:50 +02:00
try {
cleanupSubscription(subscription);
} finally {
throw e;
}
} cleanupSubscription(subscription);
return value;
},
2017-08-14 05:01:11 +02:00
complete: function complete(value) {
2017-05-28 00:38:50 +02:00
var subscription = this._s;
2017-08-14 05:01:11 +02:00
if (!subscriptionClosed(subscription)) {
2017-05-28 00:38:50 +02:00
var observer = subscription._o;
subscription._o = undefined;
try {
var m = getMethod(observer.complete);
value = m ? m.call(observer, value) : undefined;
2017-08-14 05:01:11 +02:00
} catch (e) {
2017-05-28 00:38:50 +02:00
try {
cleanupSubscription(subscription);
} finally {
throw e;
}
} cleanupSubscription(subscription);
return value;
}
}
});
2017-08-14 05:01:11 +02:00
var $Observable = function Observable(subscriber) {
2017-05-28 00:38:50 +02:00
anInstance(this, $Observable, 'Observable', '_f')._f = aFunction(subscriber);
};
redefineAll($Observable.prototype, {
2017-08-14 05:01:11 +02:00
subscribe: function subscribe(observer) {
2017-05-28 00:38:50 +02:00
return new Subscription(observer, this._f);
},
2017-08-14 05:01:11 +02:00
forEach: function forEach(fn) {
2017-05-28 00:38:50 +02:00
var that = this;
2017-08-14 05:01:11 +02:00
return new (core.Promise || global.Promise)(function (resolve, reject) {
2017-05-28 00:38:50 +02:00
aFunction(fn);
var subscription = that.subscribe({
2017-08-14 05:01:11 +02:00
next: function (value) {
2017-05-28 00:38:50 +02:00
try {
return fn(value);
2017-08-14 05:01:11 +02:00
} catch (e) {
2017-05-28 00:38:50 +02:00
reject(e);
subscription.unsubscribe();
}
},
error: reject,
complete: resolve
});
});
}
});
redefineAll($Observable, {
2017-08-14 05:01:11 +02:00
from: function from(x) {
2017-05-28 00:38:50 +02:00
var C = typeof this === 'function' ? this : $Observable;
var method = getMethod(anObject(x)[OBSERVABLE]);
2017-08-14 05:01:11 +02:00
if (method) {
2017-05-28 00:38:50 +02:00
var observable = anObject(method.call(x));
2017-08-14 05:01:11 +02:00
return observable.constructor === C ? observable : new C(function (observer) {
2017-05-28 00:38:50 +02:00
return observable.subscribe(observer);
});
}
2017-08-14 05:01:11 +02:00
return new C(function (observer) {
2017-05-28 00:38:50 +02:00
var done = false;
2017-08-14 05:01:11 +02:00
microtask(function () {
if (!done) {
2017-05-28 00:38:50 +02:00
try {
2017-08-14 05:01:11 +02:00
if (forOf(x, false, function (it) {
2017-05-28 00:38:50 +02:00
observer.next(it);
2017-08-14 05:01:11 +02:00
if (done) return RETURN;
}) === RETURN) return;
} catch (e) {
if (done) throw e;
2017-05-28 00:38:50 +02:00
observer.error(e);
return;
} observer.complete();
}
});
2017-08-14 05:01:11 +02:00
return function () { done = true; };
2017-05-28 00:38:50 +02:00
});
},
2017-08-14 05:01:11 +02:00
of: function of() {
2017-12-27 19:33:54 +01:00
for (var i = 0, l = arguments.length, items = new Array(l); i < l;) items[i] = arguments[i++];
2017-08-14 05:01:11 +02:00
return new (typeof this === 'function' ? this : $Observable)(function (observer) {
2017-05-28 00:38:50 +02:00
var done = false;
2017-08-14 05:01:11 +02:00
microtask(function () {
if (!done) {
for (var j = 0; j < items.length; ++j) {
observer.next(items[j]);
if (done) return;
2017-05-28 00:38:50 +02:00
} observer.complete();
}
});
2017-08-14 05:01:11 +02:00
return function () { done = true; };
2017-05-28 00:38:50 +02:00
});
}
});
2017-08-14 05:01:11 +02:00
hide($Observable.prototype, OBSERVABLE, function () { return this; });
2017-05-28 00:38:50 +02:00
2017-08-14 05:01:11 +02:00
$export($export.G, { Observable: $Observable });
2017-05-28 00:38:50 +02:00
2017-08-14 05:01:11 +02:00
require('./_set-species')('Observable');