nrgy
Version:
The library for reactive programming using efficient computing and MVC/MVVM patterns
234 lines (200 loc) • 6.66 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true});
var _chunk4OYRW4HCcjs = require('./chunk-4OYRW4HC.cjs');
// src/core/scope/createScope.ts
function createScope() {
const scope = new (0, _chunk4OYRW4HCcjs.BaseScope)();
return {
onDestroy: scope.onDestroy.bind(scope),
add: scope.add.bind(scope),
destroy: scope.destroy.bind(scope),
createScope: () => {
return scope.add(createScope());
},
signal: (...args) => {
const emitter = _chunk4OYRW4HCcjs.signal.call(void 0, ...args);
scope.onDestroy(() => _chunk4OYRW4HCcjs.destroySignal.call(void 0, emitter));
return emitter;
},
atom: (...args) => {
return scope.add(_chunk4OYRW4HCcjs.atom.call(void 0, ...args));
},
effect: (...args) => {
return scope.add(_chunk4OYRW4HCcjs.effect.call(void 0, ...args));
},
syncEffect: (...args) => {
return scope.add(_chunk4OYRW4HCcjs.syncEffect.call(void 0, ...args));
}
};
}
// src/core/utils/atomSubject.ts
function createAtomSubject(initialValue, options) {
let readonlyAtom = void 0;
const state = _chunk4OYRW4HCcjs.atom.call(void 0,
{
type: 0 /* value */,
value: initialValue
},
{
name: options == null ? void 0 : options.name,
onDestroy: options == null ? void 0 : options.onDestroy
}
);
const result = _chunk4OYRW4HCcjs.compute.call(void 0,
() => {
const current = state();
switch (current.type) {
case 0 /* value */:
return current.value;
case 1 /* error */:
throw current.error;
}
},
{
equal: options == null ? void 0 : options.equal
}
);
const onDestroyed = state.onDestroyed;
const destroy = () => state.destroy();
const node = _chunk4OYRW4HCcjs.getAtomNode.call(void 0, result);
let observableAtom;
const asReadonly = () => {
if (readonlyAtom === void 0) {
readonlyAtom = _chunk4OYRW4HCcjs.createAtomFromFunction.call(void 0, node, () => result());
}
return readonlyAtom;
};
const resultAtom = _chunk4OYRW4HCcjs.createAtomFromFunction.call(void 0, node, () => result(), {
next: (value) => state.set({ type: 0 /* value */, value }),
error: (error) => state.set({ type: 1 /* error */, error }),
onDestroyed,
destroy,
asDestroyable: () => {
if (!observableAtom) {
const node2 = _chunk4OYRW4HCcjs.getAtomNode.call(void 0, result);
observableAtom = _chunk4OYRW4HCcjs.createAtomFromFunction.call(void 0, node2, () => result(), {
onDestroyed,
destroy,
asReadonly
});
}
return observableAtom;
},
asReadonly
});
return resultAtom;
}
// src/core/utils/batch.ts
function batch(action) {
_chunk4OYRW4HCcjs.RUNTIME.batch(action);
}
// src/core/utils/keepLastValue.ts
function keepLastValue(source, initialValue) {
const scope = new (0, _chunk4OYRW4HCcjs.BaseScope)();
const subject = createAtomSubject(initialValue, {
onDestroy: scope.destroy
});
const sub = scope.add(_chunk4OYRW4HCcjs.syncEffect.call(void 0, source, subject.next));
scope.add(_chunk4OYRW4HCcjs.syncEffect.call(void 0, sub.onError, subject.error));
scope.add(_chunk4OYRW4HCcjs.syncEffect.call(void 0, sub.onDestroy, subject.destroy));
return subject.asDestroyable();
}
// src/core/utils/mapAtom.ts
function mapAtom(source, computation, options) {
return _chunk4OYRW4HCcjs.compute.call(void 0, () => computation(source()), options);
}
// src/core/utils/mergeAtoms.ts
function mergeAtoms(sources, computation, options) {
return _chunk4OYRW4HCcjs.compute.call(void 0, () => {
const values = sources.map((source) => source());
return computation(...values);
}, options);
}
// src/core/utils/mixSignals.ts
function mixSignals(sources, options) {
let subscriptionList;
function subscribeSources() {
if (subscriptionList === void 0) {
for (const source of sources) {
const fx = _chunk4OYRW4HCcjs.effect.call(void 0, source, resultSignal, { sync: options == null ? void 0 : options.sync });
subscriptionList = { subscription: fx, next: subscriptionList };
}
}
}
function unsubscribeSources() {
for (let item = subscriptionList; item; item = item.next) {
item.subscription.destroy();
}
subscriptionList = void 0;
}
const resultSignal = _chunk4OYRW4HCcjs.signal.call(void 0, {
...options,
onSubscribe: () => {
var _a;
subscribeSources();
(_a = options == null ? void 0 : options.onSubscribe) == null ? void 0 : _a.call(options);
},
onUnsubscribe: (isEmpty) => {
var _a;
if (isEmpty) {
unsubscribeSources();
}
(_a = options == null ? void 0 : options.onUnsubscribe) == null ? void 0 : _a.call(options, isEmpty);
},
onDestroy: () => {
var _a;
unsubscribeSources();
(_a = options == null ? void 0 : options.onDestroy) == null ? void 0 : _a.call(options);
}
});
return resultSignal;
}
// src/core/utils/objectEquals.ts
var hasOwnProperty = Object.prototype.hasOwnProperty;
var objectEquals = (objA, objB) => {
if (objA === objB) {
return true;
}
const keysA = Object.keys(objA);
const keysB = Object.keys(objB);
if (keysA.length !== keysB.length) {
return false;
}
for (let i = 0; i < keysA.length; i++) {
const key = keysA[i];
if (!hasOwnProperty.call(objB, key) || !_chunk4OYRW4HCcjs.defaultEquals.call(void 0, objA[key], objB[key])) {
return false;
}
}
return true;
};
// src/core/utils/runEffects.ts
function runEffects() {
_chunk4OYRW4HCcjs.RUNTIME.asyncScheduler.execute();
}
// src/core/utils/signalChanges.ts
function signalChanges(source, options) {
const scope = new (0, _chunk4OYRW4HCcjs.BaseScope)();
if (options == null ? void 0 : options.onDestroy) {
scope.onDestroy(options.onDestroy);
}
const s = _chunk4OYRW4HCcjs.signal.call(void 0, {
...options,
onDestroy: () => scope.destroy()
});
let first = true;
scope.add(
_chunk4OYRW4HCcjs.effect.call(void 0,
source,
(value) => {
if (first) {
first = false;
} else {
s(value);
}
},
{ sync: options == null ? void 0 : options.sync }
)
);
return s;
}
exports.createScope = createScope; exports.createAtomSubject = createAtomSubject; exports.batch = batch; exports.keepLastValue = keepLastValue; exports.mapAtom = mapAtom; exports.mergeAtoms = mergeAtoms; exports.mixSignals = mixSignals; exports.objectEquals = objectEquals; exports.runEffects = runEffects; exports.signalChanges = signalChanges;