@vueuse/rxjs
Version:
Enables RxJS reactive functions in Vue
140 lines (130 loc) • 4.49 kB
JavaScript
(function (exports, rxjs, vue, shared) {
'use strict';
// @__NO_SIDE_EFFECTS__
function from(value, watchOptions) {
if (vue.isRef(value))
return new rxjs.Observable((subscriber) => vue.watch(value, (val) => subscriber.next(val), watchOptions));
return rxjs.from(value);
}
function fromEvent(value, event) {
if (vue.isRef(value)) {
return new rxjs.Observable((subscriber) => {
let innerSub;
return vue.watch(value, (element) => {
innerSub == null ? void 0 : innerSub.unsubscribe();
if (element instanceof HTMLElement) {
innerSub = rxjs.fromEvent(element, event).subscribe(subscriber);
subscriber.add(innerSub);
}
}, { immediate: true });
});
}
if (value === null) {
throw new Error("The value is `null`, and it should be an HTMLElement.");
}
return rxjs.fromEvent(value, event);
}
// @__NO_SIDE_EFFECTS__
function toObserver(value) {
return {
next: (val) => {
value.value = val;
}
};
}
function useExtractedObservable(source, extractor, options, watchOptions) {
let subscription;
shared.tryOnScopeDispose(() => {
subscription == null ? void 0 : subscription.unsubscribe();
subscription = void 0;
});
const obsRef = vue.shallowRef(options == null ? void 0 : options.initialValue);
vue.watch(source, (value, oldValue, onCleanup) => {
subscription == null ? void 0 : subscription.unsubscribe();
if (typeof value !== "undefined" && value !== null) {
const observable = extractor(value, oldValue, onCleanup);
subscription = observable.subscribe({
error: (err) => {
var _a;
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(options, err);
},
complete: () => {
var _a;
(_a = options == null ? void 0 : options.onComplete) == null ? void 0 : _a.call(options);
},
next: (val) => {
obsRef.value = val;
}
});
} else {
subscription = void 0;
}
}, {
immediate: true,
...watchOptions
});
return vue.readonly(obsRef);
}
function useObservable(observable, options) {
const value = vue.ref(options == null ? void 0 : options.initialValue);
const subscription = observable.subscribe({
next: (val) => value.value = val,
error: options == null ? void 0 : options.onError
});
shared.tryOnScopeDispose(() => {
subscription.unsubscribe();
});
return value;
}
function useSubject(subject, options) {
const value = vue.ref(
subject instanceof rxjs.BehaviorSubject ? subject.value : void 0
);
const subscription = subject.subscribe({
next(val) {
value.value = val;
},
error: options == null ? void 0 : options.onError
});
vue.watch(value, (nextValue) => {
subject.next(nextValue);
});
shared.tryOnScopeDispose(() => {
subscription.unsubscribe();
});
return value;
}
function useSubscription(subscription) {
shared.tryOnScopeDispose(() => {
subscription.unsubscribe();
});
}
function watchExtractedObservable(source, extractor, callback, subscriptionOptions, watchOptions) {
let subscription;
shared.tryOnScopeDispose(() => {
subscription == null ? void 0 : subscription.unsubscribe();
subscription = void 0;
});
return vue.watch(source, (value, oldValue, onCleanup) => {
subscription == null ? void 0 : subscription.unsubscribe();
if (typeof value !== "undefined" && value !== null) {
const observable = extractor(value, oldValue, onCleanup);
subscription = observable.subscribe({
next: callback,
error: subscriptionOptions == null ? void 0 : subscriptionOptions.onError,
complete: subscriptionOptions == null ? void 0 : subscriptionOptions.onComplete
});
} else {
subscription = void 0;
}
}, watchOptions);
}
exports.from = from;
exports.fromEvent = fromEvent;
exports.toObserver = toObserver;
exports.useExtractedObservable = useExtractedObservable;
exports.useObservable = useObservable;
exports.useSubject = useSubject;
exports.useSubscription = useSubscription;
exports.watchExtractedObservable = watchExtractedObservable;
})(this.VueUse = this.VueUse || {}, rxjs, Vue, VueUse);