nrgy
Version:
The library for reactive programming using efficient computing and MVC/MVVM patterns
61 lines (57 loc) • 1.71 kB
JavaScript
import {
createAtomSubject,
createScope
} from "./chunk-JYWBIFMU.js";
import {
RUNTIME,
isAtom,
isSignal,
syncEffect
} from "./chunk-6EESSQRU.js";
// src/rxjs/observe.ts
import { Observable, share, shareReplay, skip } from "rxjs";
function observe(source, options) {
const observable = new Observable((subscriber) => {
const scope = createScope();
scope.onDestroy(() => subscriber.complete());
const effectFn = (options == null ? void 0 : options.sync) ? scope.syncEffect : scope.effect;
const fx = effectFn(source, (value) => {
RUNTIME.runAsUntracked(() => subscriber.next(value));
});
syncEffect(
fx.onError,
(error) => RUNTIME.runAsUntracked(() => subscriber.error(error))
);
syncEffect(fx.onDestroy, () => scope.destroy());
if (isAtom(source)) {
const sourceFx = scope.syncEffect(source, () => {
});
scope.syncEffect(sourceFx.onDestroy, () => scope.destroy());
}
return () => scope.destroy();
});
if (isSignal(source)) {
return observable.pipe(share({ resetOnRefCountZero: true }));
} else {
if (options == null ? void 0 : options.onlyChanges) {
return observable.pipe(skip(1), share({ resetOnRefCountZero: true }));
} else {
return observable.pipe(shareReplay({ bufferSize: 1, refCount: true }));
}
}
}
// src/rxjs/fromObservable.ts
function fromObservable(source, initialValue) {
const scope = createScope();
const atomSubject = createAtomSubject(initialValue, {
onDestroy: scope.destroy
});
scope.add(
source.subscribe({ next: atomSubject.next, error: atomSubject.error })
);
return atomSubject.asDestroyable();
}
export {
observe,
fromObservable
};