@cdellacqua/signals
Version:
A simple signal pattern implementation that enables reactive programming
82 lines (81 loc) • 1.59 kB
JavaScript
//#region src/lib/composition.ts
function e(e, t) {
let r = n(), i = (e) => {
r.emit(t(e));
}, a = () => {
o && r.nOfSubscriptions() === 0 && (o(), o = null);
}, o = null;
return {
nOfSubscriptions: r.nOfSubscriptions,
subscribe: (t) => {
let n = r.subscribe(t);
return o ||= e.subscribe(i), () => {
n(), a();
};
},
subscribeOnce: (t) => {
let n = r.subscribeOnce((e) => {
t(e), a();
});
return o ||= e.subscribe(i), () => {
n(), a();
};
}
};
}
function t(e) {
let t = n(), r = (e) => {
t.emit(e);
}, i = () => {
a && t.nOfSubscriptions() === 0 && (a.forEach((e) => e()), a = null);
}, a = null;
return {
nOfSubscriptions: t.nOfSubscriptions,
subscribe: (n) => {
let o = t.subscribe(n);
return a ||= e.map((e) => e.subscribe(r)), () => {
o(), i();
};
},
subscribeOnce: (n) => {
let o = t.subscribeOnce((e) => {
n(e), i();
});
return a ||= e.map((e) => e.subscribe(r)), () => {
o(), i();
};
}
};
}
//#endregion
//#region src/lib/index.ts
function n() {
let e = [];
function t(t) {
if (e.length !== 0) for (let n of e.slice()) n(t);
}
function n(t) {
let n = e.indexOf(t);
n !== -1 && e.splice(n, 1);
}
function r(t) {
return e.indexOf(t) === -1 && e.push(t), () => n(t);
}
function i(e) {
let t = r((n) => {
t(), e(n);
});
return t;
}
return {
emit: t,
subscribe: r,
subscribeOnce: i,
nOfSubscriptions() {
return e.length;
}
};
}
//#endregion
export { t as coalesceSignals, e as deriveSignal, n as makeSignal };
//# sourceMappingURL=index.js.map