nrgy
Version:
The library for reactive programming using efficient computing and MVC/MVVM patterns
92 lines (74 loc) • 2.44 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true});
var _chunk3WQA6SVUcjs = require('./chunk-3WQA6SVU.cjs');
var _chunkDUWWIS26cjs = require('./chunk-DUWWIS26.cjs');
var _chunk4OYRW4HCcjs = require('./chunk-4OYRW4HC.cjs');
// src/rx-effects/query.ts
var _rxjs = require('rxjs');
function toQuery(source) {
return {
get: () => _chunk4OYRW4HCcjs.RUNTIME.runAsUntracked(() => source()),
value$: _chunk3WQA6SVUcjs.observe.call(void 0, source)
};
}
function fromQuery(query) {
const onDestroyed = _chunk4OYRW4HCcjs.signal.call(void 0, { sync: true });
const scope = _chunkDUWWIS26cjs.createScope.call(void 0, );
scope.onDestroy(() => {
onDestroyed();
_chunk4OYRW4HCcjs.destroySignal.call(void 0, onDestroyed);
});
const state = scope.atom({ type: 0 /* value */ });
const changes$ = query.value$.pipe(_rxjs.skip.call(void 0, 1));
scope.add(
changes$.subscribe({
next: () => state.set({ type: 0 /* value */ }),
error: (error) => state.set({ type: 1 /* error */, error }),
complete: () => scope.destroy()
})
);
const result = _chunk4OYRW4HCcjs.compute.call(void 0, () => {
const current = state();
switch (current.type) {
case 0 /* value */:
return query.get();
case 1 /* error */:
throw current.error;
}
});
const node = _chunk4OYRW4HCcjs.getAtomNode.call(void 0, result);
return _chunk4OYRW4HCcjs.createAtomFromFunction.call(void 0, node, () => result(), {
onDestroyed,
destroy: () => scope.destroy(),
asReadonly: () => result
});
}
// src/rx-effects/action.ts
function toAction(source, options) {
const action = (value) => source(value);
action.event$ = _chunk3WQA6SVUcjs.observe.call(void 0, source, options);
return action;
}
function fromAction(action) {
let subscription;
function unsubscribe() {
subscription == null ? void 0 : subscription.unsubscribe();
subscription = void 0;
}
const s = _chunk4OYRW4HCcjs.signal.call(void 0, {
onSubscribe: () => {
if (!subscription) {
subscription = action.event$.subscribe((value) => s(value));
}
},
onUnsubscribe: (isEmpty) => {
if (isEmpty) {
unsubscribe();
}
},
onDestroy: () => {
unsubscribe();
}
});
return s;
}
exports.fromAction = fromAction; exports.fromQuery = fromQuery; exports.toAction = toAction; exports.toQuery = toQuery;