wishthis/node_modules/rxjs/dist/esm/internal/operators/share.js

79 lines
2.8 KiB
JavaScript
Raw Normal View History

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 = {}) {
const { connector = () => new Subject(), resetOnError = true, resetOnComplete = true, resetOnRefCountZero = true } = options;
return (wrapperSource) => {
2022-07-15 07:58:48 +00:00
let connection;
let resetConnection;
let subject;
2022-06-08 10:36:39 +00:00
let refCount = 0;
let hasCompleted = false;
let hasErrored = false;
const cancelReset = () => {
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
};
const reset = () => {
cancelReset();
2022-07-15 07:58:48 +00:00
connection = subject = undefined;
2022-06-08 10:36:39 +00:00
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);
2022-07-15 07:58:48 +00:00
if (!connection &&
refCount > 0) {
2022-06-08 10:36:39 +00:00
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();
},
});
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, ...args) {
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
const onSubscriber = new SafeSubscriber({
next: () => {
onSubscriber.unsubscribe();
reset();
},
});
2023-02-08 12:35:10 +00:00
return innerFrom(on(...args)).subscribe(onSubscriber);
2022-06-08 10:36:39 +00:00
}
//# sourceMappingURL=share.js.map