Comparing version 0.0.3 to 0.0.4
@@ -7,810 +7,3 @@ 'use strict'; | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use | ||
this file except in compliance with the License. You may obtain a copy of the | ||
License at http://www.apache.org/licenses/LICENSE-2.0 | ||
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED | ||
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | ||
MERCHANTABLITY OR NON-INFRINGEMENT. | ||
See the Apache Version 2.0 License for specific language governing permissions | ||
and limitations under the License. | ||
***************************************************************************** */ | ||
/* global Reflect, Promise */ | ||
var extendStatics = function(d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
function __extends(d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
} | ||
/** PURE_IMPORTS_START PURE_IMPORTS_END */ | ||
function isFunction(x) { | ||
return typeof x === 'function'; | ||
} | ||
/** PURE_IMPORTS_START PURE_IMPORTS_END */ | ||
var _enable_super_gross_mode_that_will_cause_bad_things = false; | ||
var config = { | ||
Promise: undefined, | ||
set useDeprecatedSynchronousErrorHandling(value) { | ||
if (value) { | ||
var error = /*@__PURE__*/ new Error(); | ||
/*@__PURE__*/ console.warn('DEPRECATED! RxJS was set to use deprecated synchronous error handling behavior by code at: \n' + error.stack); | ||
} | ||
_enable_super_gross_mode_that_will_cause_bad_things = value; | ||
}, | ||
get useDeprecatedSynchronousErrorHandling() { | ||
return _enable_super_gross_mode_that_will_cause_bad_things; | ||
}, | ||
}; | ||
/** PURE_IMPORTS_START PURE_IMPORTS_END */ | ||
function hostReportError(err) { | ||
setTimeout(function () { throw err; }, 0); | ||
} | ||
/** PURE_IMPORTS_START _config,_util_hostReportError PURE_IMPORTS_END */ | ||
var empty = { | ||
closed: true, | ||
next: function (value) { }, | ||
error: function (err) { | ||
if (config.useDeprecatedSynchronousErrorHandling) { | ||
throw err; | ||
} | ||
else { | ||
hostReportError(err); | ||
} | ||
}, | ||
complete: function () { } | ||
}; | ||
/** PURE_IMPORTS_START PURE_IMPORTS_END */ | ||
var isArray = /*@__PURE__*/ (function () { return Array.isArray || (function (x) { return x && typeof x.length === 'number'; }); })(); | ||
/** PURE_IMPORTS_START PURE_IMPORTS_END */ | ||
function isObject(x) { | ||
return x !== null && typeof x === 'object'; | ||
} | ||
/** PURE_IMPORTS_START PURE_IMPORTS_END */ | ||
var UnsubscriptionErrorImpl = /*@__PURE__*/ (function () { | ||
function UnsubscriptionErrorImpl(errors) { | ||
Error.call(this); | ||
this.message = errors ? | ||
errors.length + " errors occurred during unsubscription:\n" + errors.map(function (err, i) { return i + 1 + ") " + err.toString(); }).join('\n ') : ''; | ||
this.name = 'UnsubscriptionError'; | ||
this.errors = errors; | ||
return this; | ||
} | ||
UnsubscriptionErrorImpl.prototype = /*@__PURE__*/ Object.create(Error.prototype); | ||
return UnsubscriptionErrorImpl; | ||
})(); | ||
var UnsubscriptionError = UnsubscriptionErrorImpl; | ||
/** PURE_IMPORTS_START _util_isArray,_util_isObject,_util_isFunction,_util_UnsubscriptionError PURE_IMPORTS_END */ | ||
var Subscription = /*@__PURE__*/ (function () { | ||
function Subscription(unsubscribe) { | ||
this.closed = false; | ||
this._parentOrParents = null; | ||
this._subscriptions = null; | ||
if (unsubscribe) { | ||
this._unsubscribe = unsubscribe; | ||
} | ||
} | ||
Subscription.prototype.unsubscribe = function () { | ||
var errors; | ||
if (this.closed) { | ||
return; | ||
} | ||
var _a = this, _parentOrParents = _a._parentOrParents, _unsubscribe = _a._unsubscribe, _subscriptions = _a._subscriptions; | ||
this.closed = true; | ||
this._parentOrParents = null; | ||
this._subscriptions = null; | ||
if (_parentOrParents instanceof Subscription) { | ||
_parentOrParents.remove(this); | ||
} | ||
else if (_parentOrParents !== null) { | ||
for (var index = 0; index < _parentOrParents.length; ++index) { | ||
var parent_1 = _parentOrParents[index]; | ||
parent_1.remove(this); | ||
} | ||
} | ||
if (isFunction(_unsubscribe)) { | ||
try { | ||
_unsubscribe.call(this); | ||
} | ||
catch (e) { | ||
errors = e instanceof UnsubscriptionError ? flattenUnsubscriptionErrors(e.errors) : [e]; | ||
} | ||
} | ||
if (isArray(_subscriptions)) { | ||
var index = -1; | ||
var len = _subscriptions.length; | ||
while (++index < len) { | ||
var sub = _subscriptions[index]; | ||
if (isObject(sub)) { | ||
try { | ||
sub.unsubscribe(); | ||
} | ||
catch (e) { | ||
errors = errors || []; | ||
if (e instanceof UnsubscriptionError) { | ||
errors = errors.concat(flattenUnsubscriptionErrors(e.errors)); | ||
} | ||
else { | ||
errors.push(e); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if (errors) { | ||
throw new UnsubscriptionError(errors); | ||
} | ||
}; | ||
Subscription.prototype.add = function (teardown) { | ||
var subscription = teardown; | ||
if (!teardown) { | ||
return Subscription.EMPTY; | ||
} | ||
switch (typeof teardown) { | ||
case 'function': | ||
subscription = new Subscription(teardown); | ||
case 'object': | ||
if (subscription === this || subscription.closed || typeof subscription.unsubscribe !== 'function') { | ||
return subscription; | ||
} | ||
else if (this.closed) { | ||
subscription.unsubscribe(); | ||
return subscription; | ||
} | ||
else if (!(subscription instanceof Subscription)) { | ||
var tmp = subscription; | ||
subscription = new Subscription(); | ||
subscription._subscriptions = [tmp]; | ||
} | ||
break; | ||
default: { | ||
throw new Error('unrecognized teardown ' + teardown + ' added to Subscription.'); | ||
} | ||
} | ||
var _parentOrParents = subscription._parentOrParents; | ||
if (_parentOrParents === null) { | ||
subscription._parentOrParents = this; | ||
} | ||
else if (_parentOrParents instanceof Subscription) { | ||
if (_parentOrParents === this) { | ||
return subscription; | ||
} | ||
subscription._parentOrParents = [_parentOrParents, this]; | ||
} | ||
else if (_parentOrParents.indexOf(this) === -1) { | ||
_parentOrParents.push(this); | ||
} | ||
else { | ||
return subscription; | ||
} | ||
var subscriptions = this._subscriptions; | ||
if (subscriptions === null) { | ||
this._subscriptions = [subscription]; | ||
} | ||
else { | ||
subscriptions.push(subscription); | ||
} | ||
return subscription; | ||
}; | ||
Subscription.prototype.remove = function (subscription) { | ||
var subscriptions = this._subscriptions; | ||
if (subscriptions) { | ||
var subscriptionIndex = subscriptions.indexOf(subscription); | ||
if (subscriptionIndex !== -1) { | ||
subscriptions.splice(subscriptionIndex, 1); | ||
} | ||
} | ||
}; | ||
Subscription.EMPTY = (function (empty) { | ||
empty.closed = true; | ||
return empty; | ||
}(new Subscription())); | ||
return Subscription; | ||
}()); | ||
function flattenUnsubscriptionErrors(errors) { | ||
return errors.reduce(function (errs, err) { return errs.concat((err instanceof UnsubscriptionError) ? err.errors : err); }, []); | ||
} | ||
/** PURE_IMPORTS_START PURE_IMPORTS_END */ | ||
var rxSubscriber = /*@__PURE__*/ (function () { | ||
return typeof Symbol === 'function' | ||
? /*@__PURE__*/ Symbol('rxSubscriber') | ||
: '@@rxSubscriber_' + /*@__PURE__*/ Math.random(); | ||
})(); | ||
/** PURE_IMPORTS_START tslib,_util_isFunction,_Observer,_Subscription,_internal_symbol_rxSubscriber,_config,_util_hostReportError PURE_IMPORTS_END */ | ||
var Subscriber = /*@__PURE__*/ (function (_super) { | ||
__extends(Subscriber, _super); | ||
function Subscriber(destinationOrNext, error, complete) { | ||
var _this = _super.call(this) || this; | ||
_this.syncErrorValue = null; | ||
_this.syncErrorThrown = false; | ||
_this.syncErrorThrowable = false; | ||
_this.isStopped = false; | ||
switch (arguments.length) { | ||
case 0: | ||
_this.destination = empty; | ||
break; | ||
case 1: | ||
if (!destinationOrNext) { | ||
_this.destination = empty; | ||
break; | ||
} | ||
if (typeof destinationOrNext === 'object') { | ||
if (destinationOrNext instanceof Subscriber) { | ||
_this.syncErrorThrowable = destinationOrNext.syncErrorThrowable; | ||
_this.destination = destinationOrNext; | ||
destinationOrNext.add(_this); | ||
} | ||
else { | ||
_this.syncErrorThrowable = true; | ||
_this.destination = new SafeSubscriber(_this, destinationOrNext); | ||
} | ||
break; | ||
} | ||
default: | ||
_this.syncErrorThrowable = true; | ||
_this.destination = new SafeSubscriber(_this, destinationOrNext, error, complete); | ||
break; | ||
} | ||
return _this; | ||
} | ||
Subscriber.prototype[rxSubscriber] = function () { return this; }; | ||
Subscriber.create = function (next, error, complete) { | ||
var subscriber = new Subscriber(next, error, complete); | ||
subscriber.syncErrorThrowable = false; | ||
return subscriber; | ||
}; | ||
Subscriber.prototype.next = function (value) { | ||
if (!this.isStopped) { | ||
this._next(value); | ||
} | ||
}; | ||
Subscriber.prototype.error = function (err) { | ||
if (!this.isStopped) { | ||
this.isStopped = true; | ||
this._error(err); | ||
} | ||
}; | ||
Subscriber.prototype.complete = function () { | ||
if (!this.isStopped) { | ||
this.isStopped = true; | ||
this._complete(); | ||
} | ||
}; | ||
Subscriber.prototype.unsubscribe = function () { | ||
if (this.closed) { | ||
return; | ||
} | ||
this.isStopped = true; | ||
_super.prototype.unsubscribe.call(this); | ||
}; | ||
Subscriber.prototype._next = function (value) { | ||
this.destination.next(value); | ||
}; | ||
Subscriber.prototype._error = function (err) { | ||
this.destination.error(err); | ||
this.unsubscribe(); | ||
}; | ||
Subscriber.prototype._complete = function () { | ||
this.destination.complete(); | ||
this.unsubscribe(); | ||
}; | ||
Subscriber.prototype._unsubscribeAndRecycle = function () { | ||
var _parentOrParents = this._parentOrParents; | ||
this._parentOrParents = null; | ||
this.unsubscribe(); | ||
this.closed = false; | ||
this.isStopped = false; | ||
this._parentOrParents = _parentOrParents; | ||
return this; | ||
}; | ||
return Subscriber; | ||
}(Subscription)); | ||
var SafeSubscriber = /*@__PURE__*/ (function (_super) { | ||
__extends(SafeSubscriber, _super); | ||
function SafeSubscriber(_parentSubscriber, observerOrNext, error, complete) { | ||
var _this = _super.call(this) || this; | ||
_this._parentSubscriber = _parentSubscriber; | ||
var next; | ||
var context = _this; | ||
if (isFunction(observerOrNext)) { | ||
next = observerOrNext; | ||
} | ||
else if (observerOrNext) { | ||
next = observerOrNext.next; | ||
error = observerOrNext.error; | ||
complete = observerOrNext.complete; | ||
if (observerOrNext !== empty) { | ||
context = Object.create(observerOrNext); | ||
if (isFunction(context.unsubscribe)) { | ||
_this.add(context.unsubscribe.bind(context)); | ||
} | ||
context.unsubscribe = _this.unsubscribe.bind(_this); | ||
} | ||
} | ||
_this._context = context; | ||
_this._next = next; | ||
_this._error = error; | ||
_this._complete = complete; | ||
return _this; | ||
} | ||
SafeSubscriber.prototype.next = function (value) { | ||
if (!this.isStopped && this._next) { | ||
var _parentSubscriber = this._parentSubscriber; | ||
if (!config.useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) { | ||
this.__tryOrUnsub(this._next, value); | ||
} | ||
else if (this.__tryOrSetError(_parentSubscriber, this._next, value)) { | ||
this.unsubscribe(); | ||
} | ||
} | ||
}; | ||
SafeSubscriber.prototype.error = function (err) { | ||
if (!this.isStopped) { | ||
var _parentSubscriber = this._parentSubscriber; | ||
var useDeprecatedSynchronousErrorHandling = config.useDeprecatedSynchronousErrorHandling; | ||
if (this._error) { | ||
if (!useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) { | ||
this.__tryOrUnsub(this._error, err); | ||
this.unsubscribe(); | ||
} | ||
else { | ||
this.__tryOrSetError(_parentSubscriber, this._error, err); | ||
this.unsubscribe(); | ||
} | ||
} | ||
else if (!_parentSubscriber.syncErrorThrowable) { | ||
this.unsubscribe(); | ||
if (useDeprecatedSynchronousErrorHandling) { | ||
throw err; | ||
} | ||
hostReportError(err); | ||
} | ||
else { | ||
if (useDeprecatedSynchronousErrorHandling) { | ||
_parentSubscriber.syncErrorValue = err; | ||
_parentSubscriber.syncErrorThrown = true; | ||
} | ||
else { | ||
hostReportError(err); | ||
} | ||
this.unsubscribe(); | ||
} | ||
} | ||
}; | ||
SafeSubscriber.prototype.complete = function () { | ||
var _this = this; | ||
if (!this.isStopped) { | ||
var _parentSubscriber = this._parentSubscriber; | ||
if (this._complete) { | ||
var wrappedComplete = function () { return _this._complete.call(_this._context); }; | ||
if (!config.useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) { | ||
this.__tryOrUnsub(wrappedComplete); | ||
this.unsubscribe(); | ||
} | ||
else { | ||
this.__tryOrSetError(_parentSubscriber, wrappedComplete); | ||
this.unsubscribe(); | ||
} | ||
} | ||
else { | ||
this.unsubscribe(); | ||
} | ||
} | ||
}; | ||
SafeSubscriber.prototype.__tryOrUnsub = function (fn, value) { | ||
try { | ||
fn.call(this._context, value); | ||
} | ||
catch (err) { | ||
this.unsubscribe(); | ||
if (config.useDeprecatedSynchronousErrorHandling) { | ||
throw err; | ||
} | ||
else { | ||
hostReportError(err); | ||
} | ||
} | ||
}; | ||
SafeSubscriber.prototype.__tryOrSetError = function (parent, fn, value) { | ||
if (!config.useDeprecatedSynchronousErrorHandling) { | ||
throw new Error('bad call'); | ||
} | ||
try { | ||
fn.call(this._context, value); | ||
} | ||
catch (err) { | ||
if (config.useDeprecatedSynchronousErrorHandling) { | ||
parent.syncErrorValue = err; | ||
parent.syncErrorThrown = true; | ||
return true; | ||
} | ||
else { | ||
hostReportError(err); | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
SafeSubscriber.prototype._unsubscribe = function () { | ||
var _parentSubscriber = this._parentSubscriber; | ||
this._context = null; | ||
this._parentSubscriber = null; | ||
_parentSubscriber.unsubscribe(); | ||
}; | ||
return SafeSubscriber; | ||
}(Subscriber)); | ||
/** PURE_IMPORTS_START _Subscriber PURE_IMPORTS_END */ | ||
function canReportError(observer) { | ||
while (observer) { | ||
var _a = observer, closed_1 = _a.closed, destination = _a.destination, isStopped = _a.isStopped; | ||
if (closed_1 || isStopped) { | ||
return false; | ||
} | ||
else if (destination && destination instanceof Subscriber) { | ||
observer = destination; | ||
} | ||
else { | ||
observer = null; | ||
} | ||
} | ||
return true; | ||
} | ||
/** PURE_IMPORTS_START _Subscriber,_symbol_rxSubscriber,_Observer PURE_IMPORTS_END */ | ||
function toSubscriber(nextOrObserver, error, complete) { | ||
if (nextOrObserver) { | ||
if (nextOrObserver instanceof Subscriber) { | ||
return nextOrObserver; | ||
} | ||
if (nextOrObserver[rxSubscriber]) { | ||
return nextOrObserver[rxSubscriber](); | ||
} | ||
} | ||
if (!nextOrObserver && !error && !complete) { | ||
return new Subscriber(empty); | ||
} | ||
return new Subscriber(nextOrObserver, error, complete); | ||
} | ||
/** PURE_IMPORTS_START PURE_IMPORTS_END */ | ||
var observable = /*@__PURE__*/ (function () { return typeof Symbol === 'function' && Symbol.observable || '@@observable'; })(); | ||
/** PURE_IMPORTS_START PURE_IMPORTS_END */ | ||
function noop() { } | ||
/** PURE_IMPORTS_START _noop PURE_IMPORTS_END */ | ||
function pipeFromArray(fns) { | ||
if (!fns) { | ||
return noop; | ||
} | ||
if (fns.length === 1) { | ||
return fns[0]; | ||
} | ||
return function piped(input) { | ||
return fns.reduce(function (prev, fn) { return fn(prev); }, input); | ||
}; | ||
} | ||
/** PURE_IMPORTS_START _util_canReportError,_util_toSubscriber,_symbol_observable,_util_pipe,_config PURE_IMPORTS_END */ | ||
var Observable = /*@__PURE__*/ (function () { | ||
function Observable(subscribe) { | ||
this._isScalar = false; | ||
if (subscribe) { | ||
this._subscribe = subscribe; | ||
} | ||
} | ||
Observable.prototype.lift = function (operator) { | ||
var observable = new Observable(); | ||
observable.source = this; | ||
observable.operator = operator; | ||
return observable; | ||
}; | ||
Observable.prototype.subscribe = function (observerOrNext, error, complete) { | ||
var operator = this.operator; | ||
var sink = toSubscriber(observerOrNext, error, complete); | ||
if (operator) { | ||
sink.add(operator.call(sink, this.source)); | ||
} | ||
else { | ||
sink.add(this.source || (config.useDeprecatedSynchronousErrorHandling && !sink.syncErrorThrowable) ? | ||
this._subscribe(sink) : | ||
this._trySubscribe(sink)); | ||
} | ||
if (config.useDeprecatedSynchronousErrorHandling) { | ||
if (sink.syncErrorThrowable) { | ||
sink.syncErrorThrowable = false; | ||
if (sink.syncErrorThrown) { | ||
throw sink.syncErrorValue; | ||
} | ||
} | ||
} | ||
return sink; | ||
}; | ||
Observable.prototype._trySubscribe = function (sink) { | ||
try { | ||
return this._subscribe(sink); | ||
} | ||
catch (err) { | ||
if (config.useDeprecatedSynchronousErrorHandling) { | ||
sink.syncErrorThrown = true; | ||
sink.syncErrorValue = err; | ||
} | ||
if (canReportError(sink)) { | ||
sink.error(err); | ||
} | ||
else { | ||
console.warn(err); | ||
} | ||
} | ||
}; | ||
Observable.prototype.forEach = function (next, promiseCtor) { | ||
var _this = this; | ||
promiseCtor = getPromiseCtor(promiseCtor); | ||
return new promiseCtor(function (resolve, reject) { | ||
var subscription; | ||
subscription = _this.subscribe(function (value) { | ||
try { | ||
next(value); | ||
} | ||
catch (err) { | ||
reject(err); | ||
if (subscription) { | ||
subscription.unsubscribe(); | ||
} | ||
} | ||
}, reject, resolve); | ||
}); | ||
}; | ||
Observable.prototype._subscribe = function (subscriber) { | ||
var source = this.source; | ||
return source && source.subscribe(subscriber); | ||
}; | ||
Observable.prototype[observable] = function () { | ||
return this; | ||
}; | ||
Observable.prototype.pipe = function () { | ||
var operations = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
operations[_i] = arguments[_i]; | ||
} | ||
if (operations.length === 0) { | ||
return this; | ||
} | ||
return pipeFromArray(operations)(this); | ||
}; | ||
Observable.prototype.toPromise = function (promiseCtor) { | ||
var _this = this; | ||
promiseCtor = getPromiseCtor(promiseCtor); | ||
return new promiseCtor(function (resolve, reject) { | ||
var value; | ||
_this.subscribe(function (x) { return value = x; }, function (err) { return reject(err); }, function () { return resolve(value); }); | ||
}); | ||
}; | ||
Observable.create = function (subscribe) { | ||
return new Observable(subscribe); | ||
}; | ||
return Observable; | ||
}()); | ||
function getPromiseCtor(promiseCtor) { | ||
if (!promiseCtor) { | ||
promiseCtor = Promise; | ||
} | ||
if (!promiseCtor) { | ||
throw new Error('no Promise impl found'); | ||
} | ||
return promiseCtor; | ||
} | ||
/** PURE_IMPORTS_START PURE_IMPORTS_END */ | ||
var ObjectUnsubscribedErrorImpl = /*@__PURE__*/ (function () { | ||
function ObjectUnsubscribedErrorImpl() { | ||
Error.call(this); | ||
this.message = 'object unsubscribed'; | ||
this.name = 'ObjectUnsubscribedError'; | ||
return this; | ||
} | ||
ObjectUnsubscribedErrorImpl.prototype = /*@__PURE__*/ Object.create(Error.prototype); | ||
return ObjectUnsubscribedErrorImpl; | ||
})(); | ||
var ObjectUnsubscribedError = ObjectUnsubscribedErrorImpl; | ||
/** PURE_IMPORTS_START tslib,_Subscription PURE_IMPORTS_END */ | ||
var SubjectSubscription = /*@__PURE__*/ (function (_super) { | ||
__extends(SubjectSubscription, _super); | ||
function SubjectSubscription(subject, subscriber) { | ||
var _this = _super.call(this) || this; | ||
_this.subject = subject; | ||
_this.subscriber = subscriber; | ||
_this.closed = false; | ||
return _this; | ||
} | ||
SubjectSubscription.prototype.unsubscribe = function () { | ||
if (this.closed) { | ||
return; | ||
} | ||
this.closed = true; | ||
var subject = this.subject; | ||
var observers = subject.observers; | ||
this.subject = null; | ||
if (!observers || observers.length === 0 || subject.isStopped || subject.closed) { | ||
return; | ||
} | ||
var subscriberIndex = observers.indexOf(this.subscriber); | ||
if (subscriberIndex !== -1) { | ||
observers.splice(subscriberIndex, 1); | ||
} | ||
}; | ||
return SubjectSubscription; | ||
}(Subscription)); | ||
/** PURE_IMPORTS_START tslib,_Observable,_Subscriber,_Subscription,_util_ObjectUnsubscribedError,_SubjectSubscription,_internal_symbol_rxSubscriber PURE_IMPORTS_END */ | ||
var SubjectSubscriber = /*@__PURE__*/ (function (_super) { | ||
__extends(SubjectSubscriber, _super); | ||
function SubjectSubscriber(destination) { | ||
var _this = _super.call(this, destination) || this; | ||
_this.destination = destination; | ||
return _this; | ||
} | ||
return SubjectSubscriber; | ||
}(Subscriber)); | ||
var Subject = /*@__PURE__*/ (function (_super) { | ||
__extends(Subject, _super); | ||
function Subject() { | ||
var _this = _super.call(this) || this; | ||
_this.observers = []; | ||
_this.closed = false; | ||
_this.isStopped = false; | ||
_this.hasError = false; | ||
_this.thrownError = null; | ||
return _this; | ||
} | ||
Subject.prototype[rxSubscriber] = function () { | ||
return new SubjectSubscriber(this); | ||
}; | ||
Subject.prototype.lift = function (operator) { | ||
var subject = new AnonymousSubject(this, this); | ||
subject.operator = operator; | ||
return subject; | ||
}; | ||
Subject.prototype.next = function (value) { | ||
if (this.closed) { | ||
throw new ObjectUnsubscribedError(); | ||
} | ||
if (!this.isStopped) { | ||
var observers = this.observers; | ||
var len = observers.length; | ||
var copy = observers.slice(); | ||
for (var i = 0; i < len; i++) { | ||
copy[i].next(value); | ||
} | ||
} | ||
}; | ||
Subject.prototype.error = function (err) { | ||
if (this.closed) { | ||
throw new ObjectUnsubscribedError(); | ||
} | ||
this.hasError = true; | ||
this.thrownError = err; | ||
this.isStopped = true; | ||
var observers = this.observers; | ||
var len = observers.length; | ||
var copy = observers.slice(); | ||
for (var i = 0; i < len; i++) { | ||
copy[i].error(err); | ||
} | ||
this.observers.length = 0; | ||
}; | ||
Subject.prototype.complete = function () { | ||
if (this.closed) { | ||
throw new ObjectUnsubscribedError(); | ||
} | ||
this.isStopped = true; | ||
var observers = this.observers; | ||
var len = observers.length; | ||
var copy = observers.slice(); | ||
for (var i = 0; i < len; i++) { | ||
copy[i].complete(); | ||
} | ||
this.observers.length = 0; | ||
}; | ||
Subject.prototype.unsubscribe = function () { | ||
this.isStopped = true; | ||
this.closed = true; | ||
this.observers = null; | ||
}; | ||
Subject.prototype._trySubscribe = function (subscriber) { | ||
if (this.closed) { | ||
throw new ObjectUnsubscribedError(); | ||
} | ||
else { | ||
return _super.prototype._trySubscribe.call(this, subscriber); | ||
} | ||
}; | ||
Subject.prototype._subscribe = function (subscriber) { | ||
if (this.closed) { | ||
throw new ObjectUnsubscribedError(); | ||
} | ||
else if (this.hasError) { | ||
subscriber.error(this.thrownError); | ||
return Subscription.EMPTY; | ||
} | ||
else if (this.isStopped) { | ||
subscriber.complete(); | ||
return Subscription.EMPTY; | ||
} | ||
else { | ||
this.observers.push(subscriber); | ||
return new SubjectSubscription(this, subscriber); | ||
} | ||
}; | ||
Subject.prototype.asObservable = function () { | ||
var observable = new Observable(); | ||
observable.source = this; | ||
return observable; | ||
}; | ||
Subject.create = function (destination, source) { | ||
return new AnonymousSubject(destination, source); | ||
}; | ||
return Subject; | ||
}(Observable)); | ||
var AnonymousSubject = /*@__PURE__*/ (function (_super) { | ||
__extends(AnonymousSubject, _super); | ||
function AnonymousSubject(destination, source) { | ||
var _this = _super.call(this) || this; | ||
_this.destination = destination; | ||
_this.source = source; | ||
return _this; | ||
} | ||
AnonymousSubject.prototype.next = function (value) { | ||
var destination = this.destination; | ||
if (destination && destination.next) { | ||
destination.next(value); | ||
} | ||
}; | ||
AnonymousSubject.prototype.error = function (err) { | ||
var destination = this.destination; | ||
if (destination && destination.error) { | ||
this.destination.error(err); | ||
} | ||
}; | ||
AnonymousSubject.prototype.complete = function () { | ||
var destination = this.destination; | ||
if (destination && destination.complete) { | ||
this.destination.complete(); | ||
} | ||
}; | ||
AnonymousSubject.prototype._subscribe = function (subscriber) { | ||
var source = this.source; | ||
if (source) { | ||
return this.source.subscribe(subscriber); | ||
} | ||
else { | ||
return Subscription.EMPTY; | ||
} | ||
}; | ||
return AnonymousSubject; | ||
}(Subject)); | ||
function isObject$1(obj) { | ||
function isObject(obj) { | ||
return typeof obj === 'object' && obj !== null; | ||
@@ -828,2 +21,3 @@ } | ||
this.__listenerMap = new Map(); | ||
this.__eventMap = new Map(); | ||
this.__index = 0; | ||
@@ -834,4 +28,6 @@ | ||
if (this.__actions[action]) { | ||
this.__actions[action](payload).then((payloadResponse) => { | ||
e.reply(mainToRendererResponse, id, payloadResponse); | ||
this.__actions[action](payload).then((res) => { | ||
e.reply(mainToRendererResponse, id, 'resolve', res); | ||
}, (err) => { | ||
e.reply(mainToRendererResponse, id, 'reject', err); | ||
}); | ||
@@ -841,6 +37,6 @@ } | ||
electron.ipcMain.on(rendererToMainResponse, (e, id, payload) => { | ||
electron.ipcMain.on(rendererToMainResponse, (e, id, promiseAction, payload) => { | ||
const listener = this.__listenerMap.get(id); | ||
if (listener.obs) { | ||
listener.obs.next(payload); | ||
if (listener.promise) { | ||
listener.promise[promiseAction](payload); | ||
const emit = listener.emit + 1; | ||
@@ -851,3 +47,3 @@ if (emit === listener.winCount) { | ||
this.__listenerMap.set(id, { | ||
obs: listener.obs, | ||
promise: listener.promise, | ||
action: listener.action, | ||
@@ -864,3 +60,3 @@ emit, | ||
register(actions) { | ||
if (isObject$1(actions)) { | ||
if (isObject(actions)) { | ||
this.__actions = { | ||
@@ -876,10 +72,24 @@ ...this.__actions, | ||
talk(action, payload, window) { | ||
const obs = new Subject(); | ||
const id = this.__index; | ||
let resolve, reject; | ||
const promise = new Promise((_resolve, _reject) => { | ||
resolve = _resolve; | ||
reject = _reject; | ||
}); | ||
if (window) { | ||
this.__listenerMap.set(id, { obs, action, emit: 0, winCount: 1 }); | ||
this.__listenerMap.set(id, { | ||
promise: { resolve, reject }, | ||
action, | ||
emit: 0, | ||
winCount: 1, | ||
}); | ||
window.send(mainToRenderer, id, action, payload); | ||
} else { | ||
const windows = electron.BrowserWindow.getAllWindows(); | ||
this.__listenerMap.set(id, { obs, action, emit: 0, winCount: windows.length }); | ||
this.__listenerMap.set(id, { | ||
promise: { resolve, reject }, | ||
action, | ||
emit: 0, | ||
winCount: windows.length, | ||
}); | ||
windows.forEach(win => { | ||
@@ -890,3 +100,3 @@ win.send(mainToRenderer, id, action, payload); | ||
this.__index++; | ||
return obs; | ||
return promise; | ||
} | ||
@@ -901,2 +111,3 @@ } | ||
this.__listenerMap = new Map(); | ||
this.__eventMap = new Map(); | ||
this.__index = 0; | ||
@@ -908,3 +119,8 @@ | ||
this.__actions[action](payload).then((res) => { | ||
electron.ipcRenderer.send(rendererToMainResponse, id, res); | ||
electron.ipcRenderer.send(rendererToMainResponse, id, 'resolve', res); | ||
}, (err) => { | ||
electron.ipcRenderer.send(rendererToMainResponse, id, 'reject', err); | ||
}).catch(err => { | ||
const callback = this.__eventMap.get('error'); | ||
if (callback) callback(err); | ||
}); | ||
@@ -914,6 +130,6 @@ } | ||
electron.ipcRenderer.on(mainToRendererResponse, (e, id, payload) => { | ||
const { obs, action } = this.__listenerMap.get(id); | ||
if (obs) { | ||
obs.next(payload); | ||
electron.ipcRenderer.on(mainToRendererResponse, (e, id, promiseAction, payload) => { | ||
const { promise, action } = this.__listenerMap.get(id); | ||
if (promise) { | ||
promise[promiseAction](payload); | ||
this.__listenerMap.delete(id); | ||
@@ -926,3 +142,3 @@ } | ||
register(actions) { | ||
if (isObject$1(actions)) { | ||
if (isObject(actions)) { | ||
this.__actions = { | ||
@@ -939,7 +155,14 @@ ...this.__actions, | ||
const id = this.__index; | ||
const obs = new Subject(); | ||
this.__listenerMap.set(id, { obs, action }); | ||
let resolve, reject; | ||
const promise = new Promise((_resolve, _reject) => { | ||
resolve = _resolve; | ||
reject = _reject; | ||
}); | ||
this.__listenerMap.set(id, { | ||
promise: { resolve, reject }, | ||
action, | ||
}); | ||
electron.ipcRenderer.send(rendererToMain, id, action, payload); | ||
this.__index++; | ||
return obs; | ||
return promise; | ||
} | ||
@@ -946,0 +169,0 @@ } |
{ | ||
"name": "brillo", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"main": "build/brillo.js", | ||
@@ -8,2 +8,3 @@ "license": "MIT", | ||
"scripts": { | ||
"prepublish": "yarn build", | ||
"build": "rollup --config rollup.config.js", | ||
@@ -10,0 +11,0 @@ "test": "jest", |
# Brillo | ||
### Simple IPC communication within Electron using rxjs | ||
### Simple electron IPC using promises | ||
@@ -23,3 +23,3 @@ Here's a simple example - register some main actions: | ||
brilloRenderer.talk('mainAction').subscribe(response => { | ||
brilloRenderer.talk('mainAction').then(response => { | ||
console.log(response); // outputs '10' | ||
@@ -70,3 +70,3 @@ }); | ||
Returns `rxjs` observable. | ||
Returns a promise. | ||
@@ -80,3 +80,3 @@ ```javascript | ||
brilloMain.talk('rendererAction', 'someData', win) | ||
.subscribe(response => console.log(response)); | ||
.then(response => console.log(response)); | ||
``` | ||
@@ -106,3 +106,3 @@ | ||
Returns `rxjs` observable. | ||
Returns a promise. | ||
@@ -113,3 +113,3 @@ ```javascript | ||
brilloRenderer.talk('mainAction', 'someData') | ||
.subscribe(response => console.log(response)); | ||
.then(response => console.log(response)); | ||
``` | ||
@@ -143,3 +143,3 @@ | ||
brilloRenderer.talk('getUser', { id: 15 }) | ||
.subscribe(setUser); | ||
.then(setUser); | ||
}, []); | ||
@@ -146,0 +146,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
0
10053
7
158
1