2022-06-08 10:36:39 +00:00
|
|
|
import { __read, __spreadArray } from "tslib";
|
2022-07-15 07:58:48 +00:00
|
|
|
import { innerFrom } from '../observable/innerFrom';
|
2022-06-08 10:36:39 +00:00
|
|
|
import { Subject } from '../Subject';
|
|
|
|
import { SafeSubscriber } from '../Subscriber';
|
|
|
|
import { operate } from '../util/lift';
|
|
|
|
export function share(options) {
|
|
|
|
if (options === void 0) { options = {}; }
|
|
|
|
var _a = options.connector, connector = _a === void 0 ? function () { return new Subject(); } : _a, _b = options.resetOnError, resetOnError = _b === void 0 ? true : _b, _c = options.resetOnComplete, resetOnComplete = _c === void 0 ? true : _c, _d = options.resetOnRefCountZero, resetOnRefCountZero = _d === void 0 ? true : _d;
|
|
|
|
return function (wrapperSource) {
|
2022-07-15 07:58:48 +00:00
|
|
|
var connection;
|
|
|
|
var resetConnection;
|
|
|
|
var subject;
|
2022-06-08 10:36:39 +00:00
|
|
|
var refCount = 0;
|
|
|
|
var hasCompleted = false;
|
|
|
|
var hasErrored = false;
|
|
|
|
var cancelReset = function () {
|
|
|
|
resetConnection === null || resetConnection === void 0 ? void 0 : resetConnection.unsubscribe();
|
2022-07-15 07:58:48 +00:00
|
|
|
resetConnection = undefined;
|
2022-06-08 10:36:39 +00:00
|
|
|
};
|
|
|
|
var reset = function () {
|
|
|
|
cancelReset();
|
2022-07-15 07:58:48 +00:00
|
|
|
connection = subject = undefined;
|
2022-06-08 10:36:39 +00:00
|
|
|
hasCompleted = hasErrored = false;
|
|
|
|
};
|
|
|
|
var resetAndUnsubscribe = function () {
|
|
|
|
var conn = connection;
|
|
|
|
reset();
|
|
|
|
conn === null || conn === void 0 ? void 0 : conn.unsubscribe();
|
|
|
|
};
|
|
|
|
return operate(function (source, subscriber) {
|
|
|
|
refCount++;
|
|
|
|
if (!hasErrored && !hasCompleted) {
|
|
|
|
cancelReset();
|
|
|
|
}
|
|
|
|
var dest = (subject = subject !== null && subject !== void 0 ? subject : connector());
|
|
|
|
subscriber.add(function () {
|
|
|
|
refCount--;
|
|
|
|
if (refCount === 0 && !hasErrored && !hasCompleted) {
|
|
|
|
resetConnection = handleReset(resetAndUnsubscribe, resetOnRefCountZero);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
dest.subscribe(subscriber);
|
2022-07-15 07:58:48 +00:00
|
|
|
if (!connection &&
|
|
|
|
refCount > 0) {
|
2022-06-08 10:36:39 +00:00
|
|
|
connection = new SafeSubscriber({
|
|
|
|
next: function (value) { return dest.next(value); },
|
|
|
|
error: function (err) {
|
|
|
|
hasErrored = true;
|
|
|
|
cancelReset();
|
|
|
|
resetConnection = handleReset(reset, resetOnError, err);
|
|
|
|
dest.error(err);
|
|
|
|
},
|
|
|
|
complete: function () {
|
|
|
|
hasCompleted = true;
|
|
|
|
cancelReset();
|
|
|
|
resetConnection = handleReset(reset, resetOnComplete);
|
|
|
|
dest.complete();
|
|
|
|
},
|
|
|
|
});
|
2022-07-15 07:58:48 +00:00
|
|
|
innerFrom(source).subscribe(connection);
|
2022-06-08 10:36:39 +00:00
|
|
|
}
|
|
|
|
})(wrapperSource);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function handleReset(reset, on) {
|
|
|
|
var args = [];
|
|
|
|
for (var _i = 2; _i < arguments.length; _i++) {
|
|
|
|
args[_i - 2] = arguments[_i];
|
|
|
|
}
|
|
|
|
if (on === true) {
|
|
|
|
reset();
|
2022-07-15 07:58:48 +00:00
|
|
|
return;
|
2022-06-08 10:36:39 +00:00
|
|
|
}
|
|
|
|
if (on === false) {
|
2022-07-15 07:58:48 +00:00
|
|
|
return;
|
2022-06-08 10:36:39 +00:00
|
|
|
}
|
2022-07-15 07:58:48 +00:00
|
|
|
var onSubscriber = new SafeSubscriber({
|
|
|
|
next: function () {
|
|
|
|
onSubscriber.unsubscribe();
|
|
|
|
reset();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return on.apply(void 0, __spreadArray([], __read(args))).subscribe(onSubscriber);
|
2022-06-08 10:36:39 +00:00
|
|
|
}
|
|
|
|
//# sourceMappingURL=share.js.map
|