wishthis/node_modules/rxjs/dist/esm/internal/operators/share.js
2022-04-07 09:06:43 +02:00

75 lines
No EOL
2.7 KiB
JavaScript

import { from } from '../observable/from';
import { take } from '../operators/take';
import { Subject } from '../Subject';
import { SafeSubscriber } from '../Subscriber';
import { operate } from '../util/lift';
export function share(options = {}) {
const { connector = () => new Subject(), resetOnError = true, resetOnComplete = true, resetOnRefCountZero = true } = options;
return (wrapperSource) => {
let connection = null;
let resetConnection = null;
let subject = null;
let refCount = 0;
let hasCompleted = false;
let hasErrored = false;
const cancelReset = () => {
resetConnection === null || resetConnection === void 0 ? void 0 : resetConnection.unsubscribe();
resetConnection = null;
};
const reset = () => {
cancelReset();
connection = subject = null;
hasCompleted = hasErrored = false;
};
const resetAndUnsubscribe = () => {
const conn = connection;
reset();
conn === null || conn === void 0 ? void 0 : conn.unsubscribe();
};
return operate((source, subscriber) => {
refCount++;
if (!hasErrored && !hasCompleted) {
cancelReset();
}
const dest = (subject = subject !== null && subject !== void 0 ? subject : connector());
subscriber.add(() => {
refCount--;
if (refCount === 0 && !hasErrored && !hasCompleted) {
resetConnection = handleReset(resetAndUnsubscribe, resetOnRefCountZero);
}
});
dest.subscribe(subscriber);
if (!connection) {
connection = new SafeSubscriber({
next: (value) => dest.next(value),
error: (err) => {
hasErrored = true;
cancelReset();
resetConnection = handleReset(reset, resetOnError, err);
dest.error(err);
},
complete: () => {
hasCompleted = true;
cancelReset();
resetConnection = handleReset(reset, resetOnComplete);
dest.complete();
},
});
from(source).subscribe(connection);
}
})(wrapperSource);
};
}
function handleReset(reset, on, ...args) {
if (on === true) {
reset();
return null;
}
if (on === false) {
return null;
}
return on(...args)
.pipe(take(1))
.subscribe(() => reset());
}
//# sourceMappingURL=share.js.map