promise-logic 2.8.4 → 2.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- "use strict";class e extends Error{constructor(e,t,l){super(t),this.name="PromiseLogicError",this.type=e,this.results=l}}function t(t,l,r,n,s){const i={XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${l} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${r} promises fulfilled (expected at least one rejection).`,NOT_ERROR:"NOT condition failed: promise resolved (expected rejection).",NOR_ERROR:`NOR condition failed: ${l} promises fulfilled (expected all rejected).`,MAJORITY_ERROR:`Majority condition failed: ${l}/${r} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${l}/${r} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${r-l}/${r} promises rejected (expected all to fail).`}[t],a=s.length>0?`\n失败原因:${s.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,i+a,n)}class l{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}countRejected(e){return e.filter(e=>"rejected"===e.status).length}}class r extends l{async execute(e,l={}){try{return await Promise.all(e)}catch(l){throw t("AND_ERROR",0,e.length,[...e],l)}}}class n extends l{async execute(e,l={}){try{return await Promise.any(e)}catch(l){throw t("OR_ERROR",0,e.length,[],l)}}}class s extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=this.filterRejectedResults(r),i=n.length,a=r.length;if(1===i)return n[0];throw t("XOR_ERROR",i,a,r,s)}}class i extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(s===i)throw t("NAND_ERROR",s,i,r,n);return n}}class a extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(0===s)return[];throw t("NOR_ERROR",s,i,r,n)}}class o extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(0===s||s===i)return n;throw t("XNOR_ERROR",s,i,r,n)}}class c extends l{async execute(e,l={max:.5}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(s>Math.floor(i*l.max))return n;throw t("MAJORITY_ERROR",s,i,r)}}class u extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let r=0;0!==([...e]?.length??0)?e.forEach((e,n)=>{e.then(e=>{r++,l[n]=e}).catch(()=>{l[n]=void 0}).finally(()=>{r>0&&t(l.filter(e=>void 0!==e))})}):t(l)})}}class d extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let r=0;0!==([...e]?.length??0)?e.forEach((e,n)=>{e.then(()=>{l[n]=void 0}).catch(e=>{r++,l[n]=e}).finally(()=>{r>0&&t(l.filter(e=>void 0!==e))})}):t(l)})}}class f{constructor(e){this.promise=e}maxTimer(e,t=`Promise timed out after ${e}ms`){let l;return Promise.race([this.promise,new Promise((r,n)=>{l=setTimeout(()=>{n(new Error(t))},e)})]).finally(()=>clearTimeout(l))}then(e,t){return new f(this.promise.then(e,t))}catch(e){return new f(this.promise.catch(e))}finally(e){return new f(this.promise.finally(e))}toPromise(){return this.promise}}class h{static get gates(){return{and:new r,or:new n,xor:new s,nand:new i,nor:new a,xnor:new o,majority:new c,allFulfilled:new u,allRejected:new d}}static and(e){return new f(this.gates.and.execute(e))}static or(e){return new f(this.gates.or.execute(e))}static not(e){return new f(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new f(Promise.race(e).then(e=>Promise.resolve(e)))}static allSettled(e){return new f(Promise.allSettled(e))}static xor(e){return new f(this.gates.xor.execute(e))}static nand(e){return new f(this.gates.nand.execute(e))}static nor(e){return new f(this.gates.nor.execute(e))}static xnor(e){return new f(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new f(this.gates.majority.execute(e,t))}static allFulfilled(e){return new f(this.gates.allFulfilled.execute(e))}static allRejected(e){return new f(this.gates.allRejected.execute(e))}static createFlipFlop(e=!1){let t=e,l=null,r=Promise.resolve(t);return{getState:()=>t,set(e){return t=e,l&&(l(t),l=null),r=Promise.resolve(t),this},toggle(){return this.set(!t)},waitForChange:()=>(l||(r=new Promise(e=>{l=e})),r),waitFor(e){return t===e?Promise.resolve(t):new Promise(l=>{const r=()=>{t===e?l(t):this.waitForChange().then(r)};r()})}}}}exports.createPromiseLogic=function(e={}){const{prefix:t="",suffix:l="",rename:r={}}=e,n={and:h.and.bind(h),or:h.or.bind(h),race:h.race.bind(h),allSettled:h.allSettled.bind(h),xor:h.xor.bind(h),not:h.not.bind(h),nand:h.nand.bind(h),nor:h.nor.bind(h),xnor:h.xnor.bind(h),majority:h.majority.bind(h),allFulfilled:h.allFulfilled.bind(h),allRejected:h.allRejected.bind(h)},s={};return Object.entries(n).forEach(([e,n])=>{const i=r[e]||e;s[`${t}${i}${l}`]=n}),s};
1
+ "use strict";class e extends Error{constructor(e,t,r){super(t),this.name="PromiseLogicError",this.type=e,this.results=r}}function t(t,r,l,n,s){const i={XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${r} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${l} promises fulfilled (expected at least one rejection).`,NOT_ERROR:"NOT condition failed: promise resolved (expected rejection).",NOR_ERROR:`NOR condition failed: ${r} promises fulfilled (expected all rejected).`,MAJORITY_ERROR:`Majority condition failed: ${r}/${l} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${r}/${l} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${l-r}/${l} promises rejected (expected all to fail).`}[t],a=s.length>0?`\n失败原因:${s.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,i+a,n)}class r{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}countRejected(e){return e.filter(e=>"rejected"===e.status).length}}class l extends r{async execute(e,r={}){try{return await Promise.all(e)}catch(r){throw t("AND_ERROR",0,e.length,[...e],r)}}}class n extends r{async execute(e,r={}){try{return await Promise.any(e)}catch(r){throw t("OR_ERROR",0,e.length,[],r)}}}class s extends r{async execute(e,r={}){const l=await Promise.allSettled(e),n=this.filterFulfilledResults(l),s=this.filterRejectedResults(l),i=n.length,a=l.length;if(1===i)return n[0];throw t("XOR_ERROR",i,a,l,s)}}class i extends r{async execute(e,r={}){const l=await Promise.allSettled(e),n=this.filterFulfilledResults(l),s=n.length,i=l.length;if(s===i)throw t("NAND_ERROR",s,i,l,n);return n}}class a extends r{async execute(e,r={}){const l=await Promise.allSettled(e),n=this.filterFulfilledResults(l),s=n.length,i=l.length;if(0===s)return[];throw t("NOR_ERROR",s,i,l,n)}}class o extends r{async execute(e,r={}){const l=await Promise.allSettled(e),n=this.filterFulfilledResults(l),s=n.length,i=l.length;if(0===s||s===i)return n;throw t("XNOR_ERROR",s,i,l,n)}}class c extends r{async execute(e,r={max:.5}){const l=await Promise.allSettled(e),n=this.filterFulfilledResults(l),s=n.length,i=l.length;if(s>Math.floor(i*r.max))return n;throw t("MAJORITY_ERROR",s,i,l)}}class u extends r{async execute(e,t={}){return new Promise(t=>{const r=[];let l=0,n=0;const s=[...e],i=s?.length??0;0!==i?s.forEach((e,s)=>{Promise.resolve(e).then(e=>{n++,r[s]=e}).catch(()=>{r[s]=void 0}).finally(()=>{l++,n>0?t(r.filter(e=>void 0!==e)):l===i&&t([])})}):t(r)})}}class d extends r{async execute(e,t={}){return new Promise(t=>{const r=[];let l=0,n=0;const s=[...e],i=s?.length??0;0!==i?s.forEach((e,s)=>{Promise.resolve(e).then(()=>{r[s]=void 0}).catch(e=>{n++,r[s]=e}).finally(()=>{l++,n>0?t(r.filter(e=>void 0!==e)):l===i&&t([])})}):t(r)})}}class f{constructor(e){this.promise=e}maxTimer(e,t=`Promise timed out after ${e}ms`){let r;return Promise.race([this.promise,new Promise((l,n)=>{r=setTimeout(()=>{n(new Error(t))},e)})]).finally(()=>clearTimeout(r))}then(e,t){return new f(this.promise.then(e,t))}catch(e){return new f(this.promise.catch(e))}finally(e){return new f(this.promise.finally(e))}toPromise(){return this.promise}}class h{static get gates(){return{and:new l,or:new n,xor:new s,nand:new i,nor:new a,xnor:new o,majority:new c,allFulfilled:new u,allRejected:new d}}static and(e){return new f(this.gates.and.execute(e))}static or(e){return new f(this.gates.or.execute(e))}static not(e){return new f(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new f(Promise.race(e).then(e=>Promise.resolve(e)))}static allSettled(e){return new f(Promise.allSettled(e))}static xor(e){return new f(this.gates.xor.execute(e))}static nand(e){return new f(this.gates.nand.execute(e))}static nor(e){return new f(this.gates.nor.execute(e))}static xnor(e){return new f(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new f(this.gates.majority.execute(e,t))}static allFulfilled(e){return new f(this.gates.allFulfilled.execute(e))}static allRejected(e){return new f(this.gates.allRejected.execute(e))}static createFlipFlop(e=!1){let t=e,r=null,l=Promise.resolve(t);return{getState:()=>t,set(e){return t=e,r&&(r(t),r=null),l=Promise.resolve(t),this},toggle(){return this.set(!t)},waitForChange:()=>(r||(l=new Promise(e=>{r=e})),l),waitFor(e){return t===e?Promise.resolve(t):new Promise(r=>{const l=()=>{t===e?r(t):this.waitForChange().then(l)};l()})}}}}exports.createPromiseLogic=function(e={}){const{prefix:t="",suffix:r="",rename:l={}}=e,n={and:h.and.bind(h),or:h.or.bind(h),race:h.race.bind(h),allSettled:h.allSettled.bind(h),xor:h.xor.bind(h),not:h.not.bind(h),nand:h.nand.bind(h),nor:h.nor.bind(h),xnor:h.xnor.bind(h),majority:h.majority.bind(h),allFulfilled:h.allFulfilled.bind(h),allRejected:h.allRejected.bind(h)},s={};return Object.entries(n).forEach(([e,n])=>{const i=l[e]||e;s[`${t}${i}${r}`]=n}),s};
@@ -1 +1 @@
1
- class e extends Error{constructor(e,t,l){super(t),this.name="PromiseLogicError",this.type=e,this.results=l}}function t(t,l,r,n,s){const i={XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${l} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${r} promises fulfilled (expected at least one rejection).`,NOT_ERROR:"NOT condition failed: promise resolved (expected rejection).",NOR_ERROR:`NOR condition failed: ${l} promises fulfilled (expected all rejected).`,MAJORITY_ERROR:`Majority condition failed: ${l}/${r} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${l}/${r} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${r-l}/${r} promises rejected (expected all to fail).`}[t],a=s.length>0?`\n失败原因:${s.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,i+a,n)}class l{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}countRejected(e){return e.filter(e=>"rejected"===e.status).length}}class r extends l{async execute(e,l={}){try{return await Promise.all(e)}catch(l){throw t("AND_ERROR",0,e.length,[...e],l)}}}class n extends l{async execute(e,l={}){try{return await Promise.any(e)}catch(l){throw t("OR_ERROR",0,e.length,[],l)}}}class s extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=this.filterRejectedResults(r),i=n.length,a=r.length;if(1===i)return n[0];throw t("XOR_ERROR",i,a,r,s)}}class i extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(s===i)throw t("NAND_ERROR",s,i,r,n);return n}}class a extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(0===s)return[];throw t("NOR_ERROR",s,i,r,n)}}class o extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(0===s||s===i)return n;throw t("XNOR_ERROR",s,i,r,n)}}class c extends l{async execute(e,l={max:.5}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(s>Math.floor(i*l.max))return n;throw t("MAJORITY_ERROR",s,i,r)}}class u extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let r=0;0!==([...e]?.length??0)?e.forEach((e,n)=>{e.then(e=>{r++,l[n]=e}).catch(()=>{l[n]=void 0}).finally(()=>{r>0&&t(l.filter(e=>void 0!==e))})}):t(l)})}}class d extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let r=0;0!==([...e]?.length??0)?e.forEach((e,n)=>{e.then(()=>{l[n]=void 0}).catch(e=>{r++,l[n]=e}).finally(()=>{r>0&&t(l.filter(e=>void 0!==e))})}):t(l)})}}class f{constructor(e){this.promise=e}maxTimer(e,t=`Promise timed out after ${e}ms`){let l;return Promise.race([this.promise,new Promise((r,n)=>{l=setTimeout(()=>{n(new Error(t))},e)})]).finally(()=>clearTimeout(l))}then(e,t){return new f(this.promise.then(e,t))}catch(e){return new f(this.promise.catch(e))}finally(e){return new f(this.promise.finally(e))}toPromise(){return this.promise}}class h{static get gates(){return{and:new r,or:new n,xor:new s,nand:new i,nor:new a,xnor:new o,majority:new c,allFulfilled:new u,allRejected:new d}}static and(e){return new f(this.gates.and.execute(e))}static or(e){return new f(this.gates.or.execute(e))}static not(e){return new f(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new f(Promise.race(e).then(e=>Promise.resolve(e)))}static allSettled(e){return new f(Promise.allSettled(e))}static xor(e){return new f(this.gates.xor.execute(e))}static nand(e){return new f(this.gates.nand.execute(e))}static nor(e){return new f(this.gates.nor.execute(e))}static xnor(e){return new f(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new f(this.gates.majority.execute(e,t))}static allFulfilled(e){return new f(this.gates.allFulfilled.execute(e))}static allRejected(e){return new f(this.gates.allRejected.execute(e))}static createFlipFlop(e=!1){let t=e,l=null,r=Promise.resolve(t);return{getState:()=>t,set(e){return t=e,l&&(l(t),l=null),r=Promise.resolve(t),this},toggle(){return this.set(!t)},waitForChange:()=>(l||(r=new Promise(e=>{l=e})),r),waitFor(e){return t===e?Promise.resolve(t):new Promise(l=>{const r=()=>{t===e?l(t):this.waitForChange().then(r)};r()})}}}}function R(e={}){const{prefix:t="",suffix:l="",rename:r={}}=e,n={and:h.and.bind(h),or:h.or.bind(h),race:h.race.bind(h),allSettled:h.allSettled.bind(h),xor:h.xor.bind(h),not:h.not.bind(h),nand:h.nand.bind(h),nor:h.nor.bind(h),xnor:h.xnor.bind(h),majority:h.majority.bind(h),allFulfilled:h.allFulfilled.bind(h),allRejected:h.allRejected.bind(h)},s={};return Object.entries(n).forEach(([e,n])=>{const i=r[e]||e;s[`${t}${i}${l}`]=n}),s}export{R as createPromiseLogic};
1
+ class e extends Error{constructor(e,t,l){super(t),this.name="PromiseLogicError",this.type=e,this.results=l}}function t(t,l,r,n,s){const i={XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${l} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${r} promises fulfilled (expected at least one rejection).`,NOT_ERROR:"NOT condition failed: promise resolved (expected rejection).",NOR_ERROR:`NOR condition failed: ${l} promises fulfilled (expected all rejected).`,MAJORITY_ERROR:`Majority condition failed: ${l}/${r} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${l}/${r} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${r-l}/${r} promises rejected (expected all to fail).`}[t],a=s.length>0?`\n失败原因:${s.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,i+a,n)}class l{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}countRejected(e){return e.filter(e=>"rejected"===e.status).length}}class r extends l{async execute(e,l={}){try{return await Promise.all(e)}catch(l){throw t("AND_ERROR",0,e.length,[...e],l)}}}class n extends l{async execute(e,l={}){try{return await Promise.any(e)}catch(l){throw t("OR_ERROR",0,e.length,[],l)}}}class s extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=this.filterRejectedResults(r),i=n.length,a=r.length;if(1===i)return n[0];throw t("XOR_ERROR",i,a,r,s)}}class i extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(s===i)throw t("NAND_ERROR",s,i,r,n);return n}}class a extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(0===s)return[];throw t("NOR_ERROR",s,i,r,n)}}class o extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(0===s||s===i)return n;throw t("XNOR_ERROR",s,i,r,n)}}class c extends l{async execute(e,l={max:.5}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(s>Math.floor(i*l.max))return n;throw t("MAJORITY_ERROR",s,i,r)}}class u extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let r=0,n=0;const s=[...e],i=s?.length??0;0!==i?s.forEach((e,s)=>{Promise.resolve(e).then(e=>{n++,l[s]=e}).catch(()=>{l[s]=void 0}).finally(()=>{r++,n>0?t(l.filter(e=>void 0!==e)):r===i&&t([])})}):t(l)})}}class d extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let r=0,n=0;const s=[...e],i=s?.length??0;0!==i?s.forEach((e,s)=>{Promise.resolve(e).then(()=>{l[s]=void 0}).catch(e=>{n++,l[s]=e}).finally(()=>{r++,n>0?t(l.filter(e=>void 0!==e)):r===i&&t([])})}):t(l)})}}class f{constructor(e){this.promise=e}maxTimer(e,t=`Promise timed out after ${e}ms`){let l;return Promise.race([this.promise,new Promise((r,n)=>{l=setTimeout(()=>{n(new Error(t))},e)})]).finally(()=>clearTimeout(l))}then(e,t){return new f(this.promise.then(e,t))}catch(e){return new f(this.promise.catch(e))}finally(e){return new f(this.promise.finally(e))}toPromise(){return this.promise}}class h{static get gates(){return{and:new r,or:new n,xor:new s,nand:new i,nor:new a,xnor:new o,majority:new c,allFulfilled:new u,allRejected:new d}}static and(e){return new f(this.gates.and.execute(e))}static or(e){return new f(this.gates.or.execute(e))}static not(e){return new f(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new f(Promise.race(e).then(e=>Promise.resolve(e)))}static allSettled(e){return new f(Promise.allSettled(e))}static xor(e){return new f(this.gates.xor.execute(e))}static nand(e){return new f(this.gates.nand.execute(e))}static nor(e){return new f(this.gates.nor.execute(e))}static xnor(e){return new f(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new f(this.gates.majority.execute(e,t))}static allFulfilled(e){return new f(this.gates.allFulfilled.execute(e))}static allRejected(e){return new f(this.gates.allRejected.execute(e))}static createFlipFlop(e=!1){let t=e,l=null,r=Promise.resolve(t);return{getState:()=>t,set(e){return t=e,l&&(l(t),l=null),r=Promise.resolve(t),this},toggle(){return this.set(!t)},waitForChange:()=>(l||(r=new Promise(e=>{l=e})),r),waitFor(e){return t===e?Promise.resolve(t):new Promise(l=>{const r=()=>{t===e?l(t):this.waitForChange().then(r)};r()})}}}}function R(e={}){const{prefix:t="",suffix:l="",rename:r={}}=e,n={and:h.and.bind(h),or:h.or.bind(h),race:h.race.bind(h),allSettled:h.allSettled.bind(h),xor:h.xor.bind(h),not:h.not.bind(h),nand:h.nand.bind(h),nor:h.nor.bind(h),xnor:h.xnor.bind(h),majority:h.majority.bind(h),allFulfilled:h.allFulfilled.bind(h),allRejected:h.allRejected.bind(h)},s={};return Object.entries(n).forEach(([e,n])=>{const i=r[e]||e;s[`${t}${i}${l}`]=n}),s}export{R as createPromiseLogic};
package/dist/index.cjs.js CHANGED
@@ -1 +1 @@
1
- "use strict";class e extends Error{constructor(e,t,r){super(t),this.name="PromiseLogicError",this.type=e,this.results=r}}function t(t,r,l,s,n){const i={XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${r} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${l} promises fulfilled (expected at least one rejection).`,NOT_ERROR:"NOT condition failed: promise resolved (expected rejection).",NOR_ERROR:`NOR condition failed: ${r} promises fulfilled (expected all rejected).`,MAJORITY_ERROR:`Majority condition failed: ${r}/${l} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${r}/${l} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${l-r}/${l} promises rejected (expected all to fail).`}[t],a=n.length>0?`\n失败原因:${n.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,i+a,s)}class r{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}countRejected(e){return e.filter(e=>"rejected"===e.status).length}}class l extends r{async execute(e,r={}){try{return await Promise.all(e)}catch(r){throw t("AND_ERROR",0,e.length,[...e],r)}}}class s extends r{async execute(e,r={}){try{return await Promise.any(e)}catch(r){throw t("OR_ERROR",0,e.length,[],r)}}}class n extends r{async execute(e,r={}){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),n=this.filterRejectedResults(l),i=s.length,a=l.length;if(1===i)return s[0];throw t("XOR_ERROR",i,a,l,n)}}class i extends r{async execute(e,r={}){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),n=s.length,i=l.length;if(n===i)throw t("NAND_ERROR",n,i,l,s);return s}}class a extends r{async execute(e,r={}){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),n=s.length,i=l.length;if(0===n)return[];throw t("NOR_ERROR",n,i,l,s)}}class o extends r{async execute(e,r={}){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),n=s.length,i=l.length;if(0===n||n===i)return s;throw t("XNOR_ERROR",n,i,l,s)}}class c extends r{async execute(e,r={max:.5}){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),n=s.length,i=l.length;if(n>Math.floor(i*r.max))return s;throw t("MAJORITY_ERROR",n,i,l)}}class u extends r{async execute(e,t={}){return new Promise(t=>{const r=[];let l=0;0!==([...e]?.length??0)?e.forEach((e,s)=>{e.then(e=>{l++,r[s]=e}).catch(()=>{r[s]=void 0}).finally(()=>{l>0&&t(r.filter(e=>void 0!==e))})}):t(r)})}}class d extends r{async execute(e,t={}){return new Promise(t=>{const r=[];let l=0;0!==([...e]?.length??0)?e.forEach((e,s)=>{e.then(()=>{r[s]=void 0}).catch(e=>{l++,r[s]=e}).finally(()=>{l>0&&t(r.filter(e=>void 0!==e))})}):t(r)})}}class f{constructor(e){this.promise=e}maxTimer(e,t=`Promise timed out after ${e}ms`){let r;return Promise.race([this.promise,new Promise((l,s)=>{r=setTimeout(()=>{s(new Error(t))},e)})]).finally(()=>clearTimeout(r))}then(e,t){return new f(this.promise.then(e,t))}catch(e){return new f(this.promise.catch(e))}finally(e){return new f(this.promise.finally(e))}toPromise(){return this.promise}}class h{static get gates(){return{and:new l,or:new s,xor:new n,nand:new i,nor:new a,xnor:new o,majority:new c,allFulfilled:new u,allRejected:new d}}static and(e){return new f(this.gates.and.execute(e))}static or(e){return new f(this.gates.or.execute(e))}static not(e){return new f(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new f(Promise.race(e).then(e=>Promise.resolve(e)))}static allSettled(e){return new f(Promise.allSettled(e))}static xor(e){return new f(this.gates.xor.execute(e))}static nand(e){return new f(this.gates.nand.execute(e))}static nor(e){return new f(this.gates.nor.execute(e))}static xnor(e){return new f(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new f(this.gates.majority.execute(e,t))}static allFulfilled(e){return new f(this.gates.allFulfilled.execute(e))}static allRejected(e){return new f(this.gates.allRejected.execute(e))}static createFlipFlop(e=!1){let t=e,r=null,l=Promise.resolve(t);return{getState:()=>t,set(e){return t=e,r&&(r(t),r=null),l=Promise.resolve(t),this},toggle(){return this.set(!t)},waitForChange:()=>(r||(l=new Promise(e=>{r=e})),l),waitFor(e){return t===e?Promise.resolve(t):new Promise(r=>{const l=()=>{t===e?r(t):this.waitForChange().then(l)};l()})}}}}exports.PromiseLogic=h,exports.PromiseLogicError=e,exports.PromiseWithTimer=f,exports.createLogicError=t,exports.createPromiseLogic=function(e={}){const{prefix:t="",suffix:r="",rename:l={}}=e,s={and:h.and.bind(h),or:h.or.bind(h),race:h.race.bind(h),allSettled:h.allSettled.bind(h),xor:h.xor.bind(h),not:h.not.bind(h),nand:h.nand.bind(h),nor:h.nor.bind(h),xnor:h.xnor.bind(h),majority:h.majority.bind(h),allFulfilled:h.allFulfilled.bind(h),allRejected:h.allRejected.bind(h)},n={};return Object.entries(s).forEach(([e,s])=>{const i=l[e]||e;n[`${t}${i}${r}`]=s}),n};
1
+ "use strict";class e extends Error{constructor(e,t,r){super(t),this.name="PromiseLogicError",this.type=e,this.results=r}}function t(t,r,l,s,n){const i={XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${r} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${l} promises fulfilled (expected at least one rejection).`,NOT_ERROR:"NOT condition failed: promise resolved (expected rejection).",NOR_ERROR:`NOR condition failed: ${r} promises fulfilled (expected all rejected).`,MAJORITY_ERROR:`Majority condition failed: ${r}/${l} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${r}/${l} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${l-r}/${l} promises rejected (expected all to fail).`}[t],o=n.length>0?`\n失败原因:${n.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,i+o,s)}class r{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}countRejected(e){return e.filter(e=>"rejected"===e.status).length}}class l extends r{async execute(e,r={}){try{return await Promise.all(e)}catch(r){throw t("AND_ERROR",0,e.length,[...e],r)}}}class s extends r{async execute(e,r={}){try{return await Promise.any(e)}catch(r){throw t("OR_ERROR",0,e.length,[],r)}}}class n extends r{async execute(e,r={}){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),n=this.filterRejectedResults(l),i=s.length,o=l.length;if(1===i)return s[0];throw t("XOR_ERROR",i,o,l,n)}}class i extends r{async execute(e,r={}){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),n=s.length,i=l.length;if(n===i)throw t("NAND_ERROR",n,i,l,s);return s}}class o extends r{async execute(e,r={}){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),n=s.length,i=l.length;if(0===n)return[];throw t("NOR_ERROR",n,i,l,s)}}class a extends r{async execute(e,r={}){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),n=s.length,i=l.length;if(0===n||n===i)return s;throw t("XNOR_ERROR",n,i,l,s)}}class c extends r{async execute(e,r={max:.5}){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),n=s.length,i=l.length;if(n>Math.floor(i*r.max))return s;throw t("MAJORITY_ERROR",n,i,l)}}class u extends r{async execute(e,t={}){return new Promise(t=>{const r=[];let l=0,s=0;const n=[...e],i=n?.length??0;0!==i?n.forEach((e,n)=>{Promise.resolve(e).then(e=>{s++,r[n]=e}).catch(()=>{r[n]=void 0}).finally(()=>{l++,s>0?t(r.filter(e=>void 0!==e)):l===i&&t([])})}):t(r)})}}class d extends r{async execute(e,t={}){return new Promise(t=>{const r=[];let l=0,s=0;const n=[...e],i=n?.length??0;0!==i?n.forEach((e,n)=>{Promise.resolve(e).then(()=>{r[n]=void 0}).catch(e=>{s++,r[n]=e}).finally(()=>{l++,s>0?t(r.filter(e=>void 0!==e)):l===i&&t([])})}):t(r)})}}class f{constructor(e){this.promise=e}maxTimer(e,t=`Promise timed out after ${e}ms`){let r;return Promise.race([this.promise,new Promise((l,s)=>{r=setTimeout(()=>{s(new Error(t))},e)})]).finally(()=>clearTimeout(r))}then(e,t){return new f(this.promise.then(e,t))}catch(e){return new f(this.promise.catch(e))}finally(e){return new f(this.promise.finally(e))}toPromise(){return this.promise}}class h{static get gates(){return{and:new l,or:new s,xor:new n,nand:new i,nor:new o,xnor:new a,majority:new c,allFulfilled:new u,allRejected:new d}}static and(e){return new f(this.gates.and.execute(e))}static or(e){return new f(this.gates.or.execute(e))}static not(e){return new f(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new f(Promise.race(e).then(e=>Promise.resolve(e)))}static allSettled(e){return new f(Promise.allSettled(e))}static xor(e){return new f(this.gates.xor.execute(e))}static nand(e){return new f(this.gates.nand.execute(e))}static nor(e){return new f(this.gates.nor.execute(e))}static xnor(e){return new f(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new f(this.gates.majority.execute(e,t))}static allFulfilled(e){return new f(this.gates.allFulfilled.execute(e))}static allRejected(e){return new f(this.gates.allRejected.execute(e))}static createFlipFlop(e=!1){let t=e,r=null,l=Promise.resolve(t);return{getState:()=>t,set(e){return t=e,r&&(r(t),r=null),l=Promise.resolve(t),this},toggle(){return this.set(!t)},waitForChange:()=>(r||(l=new Promise(e=>{r=e})),l),waitFor(e){return t===e?Promise.resolve(t):new Promise(r=>{const l=()=>{t===e?r(t):this.waitForChange().then(l)};l()})}}}}exports.PromiseLogic=h,exports.PromiseLogicError=e,exports.PromiseWithTimer=f,exports.createLogicError=t,exports.createPromiseLogic=function(e={}){const{prefix:t="",suffix:r="",rename:l={}}=e,s={and:h.and.bind(h),or:h.or.bind(h),race:h.race.bind(h),allSettled:h.allSettled.bind(h),xor:h.xor.bind(h),not:h.not.bind(h),nand:h.nand.bind(h),nor:h.nor.bind(h),xnor:h.xnor.bind(h),majority:h.majority.bind(h),allFulfilled:h.allFulfilled.bind(h),allRejected:h.allRejected.bind(h)},n={};return Object.entries(s).forEach(([e,s])=>{const i=l[e]||e;n[`${t}${i}${r}`]=s}),n};
package/dist/index.esm.js CHANGED
@@ -1 +1 @@
1
- class e extends Error{constructor(e,t,l){super(t),this.name="PromiseLogicError",this.type=e,this.results=l}}function t(t,l,r,n,s){const i={XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${l} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${r} promises fulfilled (expected at least one rejection).`,NOT_ERROR:"NOT condition failed: promise resolved (expected rejection).",NOR_ERROR:`NOR condition failed: ${l} promises fulfilled (expected all rejected).`,MAJORITY_ERROR:`Majority condition failed: ${l}/${r} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${l}/${r} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${r-l}/${r} promises rejected (expected all to fail).`}[t],a=s.length>0?`\n失败原因:${s.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,i+a,n)}class l{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}countRejected(e){return e.filter(e=>"rejected"===e.status).length}}class r extends l{async execute(e,l={}){try{return await Promise.all(e)}catch(l){throw t("AND_ERROR",0,e.length,[...e],l)}}}class n extends l{async execute(e,l={}){try{return await Promise.any(e)}catch(l){throw t("OR_ERROR",0,e.length,[],l)}}}class s extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=this.filterRejectedResults(r),i=n.length,a=r.length;if(1===i)return n[0];throw t("XOR_ERROR",i,a,r,s)}}class i extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(s===i)throw t("NAND_ERROR",s,i,r,n);return n}}class a extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(0===s)return[];throw t("NOR_ERROR",s,i,r,n)}}class o extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(0===s||s===i)return n;throw t("XNOR_ERROR",s,i,r,n)}}class c extends l{async execute(e,l={max:.5}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(s>Math.floor(i*l.max))return n;throw t("MAJORITY_ERROR",s,i,r)}}class u extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let r=0;0!==([...e]?.length??0)?e.forEach((e,n)=>{e.then(e=>{r++,l[n]=e}).catch(()=>{l[n]=void 0}).finally(()=>{r>0&&t(l.filter(e=>void 0!==e))})}):t(l)})}}class d extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let r=0;0!==([...e]?.length??0)?e.forEach((e,n)=>{e.then(()=>{l[n]=void 0}).catch(e=>{r++,l[n]=e}).finally(()=>{r>0&&t(l.filter(e=>void 0!==e))})}):t(l)})}}class f{constructor(e){this.promise=e}maxTimer(e,t=`Promise timed out after ${e}ms`){let l;return Promise.race([this.promise,new Promise((r,n)=>{l=setTimeout(()=>{n(new Error(t))},e)})]).finally(()=>clearTimeout(l))}then(e,t){return new f(this.promise.then(e,t))}catch(e){return new f(this.promise.catch(e))}finally(e){return new f(this.promise.finally(e))}toPromise(){return this.promise}}class h{static get gates(){return{and:new r,or:new n,xor:new s,nand:new i,nor:new a,xnor:new o,majority:new c,allFulfilled:new u,allRejected:new d}}static and(e){return new f(this.gates.and.execute(e))}static or(e){return new f(this.gates.or.execute(e))}static not(e){return new f(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new f(Promise.race(e).then(e=>Promise.resolve(e)))}static allSettled(e){return new f(Promise.allSettled(e))}static xor(e){return new f(this.gates.xor.execute(e))}static nand(e){return new f(this.gates.nand.execute(e))}static nor(e){return new f(this.gates.nor.execute(e))}static xnor(e){return new f(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new f(this.gates.majority.execute(e,t))}static allFulfilled(e){return new f(this.gates.allFulfilled.execute(e))}static allRejected(e){return new f(this.gates.allRejected.execute(e))}static createFlipFlop(e=!1){let t=e,l=null,r=Promise.resolve(t);return{getState:()=>t,set(e){return t=e,l&&(l(t),l=null),r=Promise.resolve(t),this},toggle(){return this.set(!t)},waitForChange:()=>(l||(r=new Promise(e=>{l=e})),r),waitFor(e){return t===e?Promise.resolve(t):new Promise(l=>{const r=()=>{t===e?l(t):this.waitForChange().then(r)};r()})}}}}function R(e={}){const{prefix:t="",suffix:l="",rename:r={}}=e,n={and:h.and.bind(h),or:h.or.bind(h),race:h.race.bind(h),allSettled:h.allSettled.bind(h),xor:h.xor.bind(h),not:h.not.bind(h),nand:h.nand.bind(h),nor:h.nor.bind(h),xnor:h.xnor.bind(h),majority:h.majority.bind(h),allFulfilled:h.allFulfilled.bind(h),allRejected:h.allRejected.bind(h)},s={};return Object.entries(n).forEach(([e,n])=>{const i=r[e]||e;s[`${t}${i}${l}`]=n}),s}export{h as PromiseLogic,e as PromiseLogicError,f as PromiseWithTimer,t as createLogicError,R as createPromiseLogic};
1
+ class e extends Error{constructor(e,t,l){super(t),this.name="PromiseLogicError",this.type=e,this.results=l}}function t(t,l,r,n,s){const i={XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${l} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${r} promises fulfilled (expected at least one rejection).`,NOT_ERROR:"NOT condition failed: promise resolved (expected rejection).",NOR_ERROR:`NOR condition failed: ${l} promises fulfilled (expected all rejected).`,MAJORITY_ERROR:`Majority condition failed: ${l}/${r} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${l}/${r} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${r-l}/${r} promises rejected (expected all to fail).`}[t],a=s.length>0?`\n失败原因:${s.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,i+a,n)}class l{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}countRejected(e){return e.filter(e=>"rejected"===e.status).length}}class r extends l{async execute(e,l={}){try{return await Promise.all(e)}catch(l){throw t("AND_ERROR",0,e.length,[...e],l)}}}class n extends l{async execute(e,l={}){try{return await Promise.any(e)}catch(l){throw t("OR_ERROR",0,e.length,[],l)}}}class s extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=this.filterRejectedResults(r),i=n.length,a=r.length;if(1===i)return n[0];throw t("XOR_ERROR",i,a,r,s)}}class i extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(s===i)throw t("NAND_ERROR",s,i,r,n);return n}}class a extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(0===s)return[];throw t("NOR_ERROR",s,i,r,n)}}class o extends l{async execute(e,l={}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(0===s||s===i)return n;throw t("XNOR_ERROR",s,i,r,n)}}class c extends l{async execute(e,l={max:.5}){const r=await Promise.allSettled(e),n=this.filterFulfilledResults(r),s=n.length,i=r.length;if(s>Math.floor(i*l.max))return n;throw t("MAJORITY_ERROR",s,i,r)}}class u extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let r=0,n=0;const s=[...e],i=s?.length??0;0!==i?s.forEach((e,s)=>{Promise.resolve(e).then(e=>{n++,l[s]=e}).catch(()=>{l[s]=void 0}).finally(()=>{r++,n>0?t(l.filter(e=>void 0!==e)):r===i&&t([])})}):t(l)})}}class d extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let r=0,n=0;const s=[...e],i=s?.length??0;0!==i?s.forEach((e,s)=>{Promise.resolve(e).then(()=>{l[s]=void 0}).catch(e=>{n++,l[s]=e}).finally(()=>{r++,n>0?t(l.filter(e=>void 0!==e)):r===i&&t([])})}):t(l)})}}class f{constructor(e){this.promise=e}maxTimer(e,t=`Promise timed out after ${e}ms`){let l;return Promise.race([this.promise,new Promise((r,n)=>{l=setTimeout(()=>{n(new Error(t))},e)})]).finally(()=>clearTimeout(l))}then(e,t){return new f(this.promise.then(e,t))}catch(e){return new f(this.promise.catch(e))}finally(e){return new f(this.promise.finally(e))}toPromise(){return this.promise}}class h{static get gates(){return{and:new r,or:new n,xor:new s,nand:new i,nor:new a,xnor:new o,majority:new c,allFulfilled:new u,allRejected:new d}}static and(e){return new f(this.gates.and.execute(e))}static or(e){return new f(this.gates.or.execute(e))}static not(e){return new f(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new f(Promise.race(e).then(e=>Promise.resolve(e)))}static allSettled(e){return new f(Promise.allSettled(e))}static xor(e){return new f(this.gates.xor.execute(e))}static nand(e){return new f(this.gates.nand.execute(e))}static nor(e){return new f(this.gates.nor.execute(e))}static xnor(e){return new f(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new f(this.gates.majority.execute(e,t))}static allFulfilled(e){return new f(this.gates.allFulfilled.execute(e))}static allRejected(e){return new f(this.gates.allRejected.execute(e))}static createFlipFlop(e=!1){let t=e,l=null,r=Promise.resolve(t);return{getState:()=>t,set(e){return t=e,l&&(l(t),l=null),r=Promise.resolve(t),this},toggle(){return this.set(!t)},waitForChange:()=>(l||(r=new Promise(e=>{l=e})),r),waitFor(e){return t===e?Promise.resolve(t):new Promise(l=>{const r=()=>{t===e?l(t):this.waitForChange().then(r)};r()})}}}}function R(e={}){const{prefix:t="",suffix:l="",rename:r={}}=e,n={and:h.and.bind(h),or:h.or.bind(h),race:h.race.bind(h),allSettled:h.allSettled.bind(h),xor:h.xor.bind(h),not:h.not.bind(h),nand:h.nand.bind(h),nor:h.nor.bind(h),xnor:h.xnor.bind(h),majority:h.majority.bind(h),allFulfilled:h.allFulfilled.bind(h),allRejected:h.allRejected.bind(h)},s={};return Object.entries(n).forEach(([e,n])=>{const i=r[e]||e;s[`${t}${i}${l}`]=n}),s}export{h as PromiseLogic,e as PromiseLogicError,f as PromiseWithTimer,t as createLogicError,R as createPromiseLogic};
@@ -1 +1 @@
1
- "use strict";class e extends Error{constructor(e,t,l){super(t),this.type=e,this.results=l,this.name="PromiseLogicError"}}function t(t,l,s,r,n){const i={XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${l} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${s} promises fulfilled (expected at least one rejection).`,NOR_ERROR:`NOR condition failed: ${l} promises fulfilled (expected all rejected).`,XNOR_ERROR:`XNOR condition failed: ${l}/${s} promises fulfilled (expected all or none).`,MAJORITY_ERROR:`Majority condition failed: ${l}/${s} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${l}/${s} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${s-l}/${s} promises rejected (expected all to fail).`}[t]||"Logic condition failed",a=n&&n?.length>0?`\n失败原因:${n.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,i+a,r)}class l{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}}class s extends l{async execute(e){try{return Promise.any(e)}catch(e){throw t("OR_ERROR",0,0,[],[e])}}}class r extends l{async execute(e){try{return Promise.all(e)}catch(e){throw t("AND_ERROR",0,0,[],[e])}}}class n extends l{async execute(e,l){const s=await Promise.allSettled(e),r=this.filterFulfilledResults(s),n=r.length,i=s.length,a=this.filterRejectedResults(s);if(n>Math.floor(i*l.max))return r;throw t("MAJORITY_ERROR",n,i,s,a)}}class i extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),r=s.length,n=l.length,i=this.filterRejectedResults(l);if(r===n)throw t("NAND_ERROR",r,n,l,i);return s}}class a extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l).length,r=l.length,n=this.filterRejectedResults(l);if(0===s)return[];throw t("NOR_ERROR",s,r,l,n)}}class o extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),r=s.length,n=l.length,i=this.filterRejectedResults(l);if(0===r||r===n)return s;throw t("XNOR_ERROR",r,n,l,i)}}class c extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),r=s.length,n=l.length,i=this.filterRejectedResults(l);if(1===r)return s[0];throw t("XOR_ERROR",r,n,l,i)}}class u extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let s=0;const r=[...e];0!==(r?.length??0)?r.forEach((e,r)=>{Promise.resolve(e).then(e=>{s++,l[r]=e}).catch(()=>{l[r]=void 0}).finally(()=>{s>0&&t(l.filter(e=>void 0!==e))})}):t(l)})}}class d extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let s=0;const r=[...e];0!==(r?.length??0)?r.forEach((e,r)=>{Promise.resolve(e).then(()=>{l[r]=void 0}).catch(e=>{s++,l[r]=e}).finally(()=>{s>0&&t(l.filter(e=>void 0!==e))})}):t(l)})}}class f{constructor(e){this.promise=e}maxTimer(e,t=`Promise timed out after ${e}ms`){let l;return Promise.race([this.promise,new Promise((s,r)=>{l=setTimeout(()=>{r(new Error(t))},e)})]).finally(()=>clearTimeout(l))}then(e,t){return new f(this.promise.then(e,t))}catch(e){return new f(this.promise.catch(e))}finally(e){return new f(this.promise.finally(e))}toPromise(){return this.promise}}class R{static get gates(){return{and:new r,or:new s,xor:new c,nand:new i,nor:new a,xnor:new o,majority:new n,allFulfilled:new u,allRejected:new d}}static and(e){return new f(this.gates.and.execute(e))}static or(e){return new f(this.gates.or.execute(e))}static xor(e){return new f(this.gates.xor.execute(e))}static nand(e){return new f(this.gates.nand.execute(e))}static nor(e){return new f(this.gates.nor.execute(e))}static xnor(e){return new f(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new f(this.gates.majority.execute(e,t))}static allFulfilled(e){return new f(this.gates.allFulfilled.execute(e))}static allRejected(e){return new f(this.gates.allRejected.execute(e))}static not(e){return new f(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new f(Promise.race(e))}static allSettled(e){return new f(Promise.allSettled(e))}static createFlipFlop(e=!1){let t=e,l=null,s=Promise.resolve(t);const r=()=>(l||(s=new Promise(e=>{l=e})),s);return{getState:()=>t,set:async e=>(t=e,l&&(l(t),l=null),s=Promise.resolve(t),t),async toggle(){return this.set(!t)},waitForChange:r,waitFor:e=>t===e?Promise.resolve(t):new Promise(l=>{const s=()=>{t===e?l(t):r().then(s)};s()})}}}exports.createPromiseLogic=function(e={}){const{prefix:t="",suffix:l="",rename:s={}}=e,r={and:R.and.bind(R),or:R.or.bind(R),not:R.not.bind(R),race:R.race.bind(R),allSettled:R.allSettled.bind(R),xor:R.xor.bind(R),nand:R.nand.bind(R),nor:R.nor.bind(R),xnor:R.xnor.bind(R),majority:R.majority.bind(R),allFulfilled:R.allFulfilled.bind(R),allRejected:R.allRejected.bind(R)},n={};return Object.entries(r).forEach(([e,r])=>{const i=s[e]||e;n[`${t}${i}${l}`]=r}),n};
1
+ "use strict";class e extends Error{constructor(e,t,l){super(t),this.type=e,this.results=l,this.name="PromiseLogicError"}}function t(t,l,s,r,n){const i={XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${l} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${s} promises fulfilled (expected at least one rejection).`,NOR_ERROR:`NOR condition failed: ${l} promises fulfilled (expected all rejected).`,XNOR_ERROR:`XNOR condition failed: ${l}/${s} promises fulfilled (expected all or none).`,MAJORITY_ERROR:`Majority condition failed: ${l}/${s} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${l}/${s} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${s-l}/${s} promises rejected (expected all to fail).`}[t]||"Logic condition failed",a=n&&n?.length>0?`\n失败原因:${n.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,i+a,r)}class l{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}}class s extends l{async execute(e){try{return Promise.any(e)}catch(e){throw t("OR_ERROR",0,0,[],[e])}}}class r extends l{async execute(e){try{return Promise.all(e)}catch(e){throw t("AND_ERROR",0,0,[],[e])}}}class n extends l{async execute(e,l){const s=await Promise.allSettled(e),r=this.filterFulfilledResults(s),n=r.length,i=s.length,a=this.filterRejectedResults(s);if(n>Math.floor(i*l.max))return r;throw t("MAJORITY_ERROR",n,i,s,a)}}class i extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),r=s.length,n=l.length,i=this.filterRejectedResults(l);if(r===n)throw t("NAND_ERROR",r,n,l,i);return s}}class a extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l).length,r=l.length,n=this.filterRejectedResults(l);if(0===s)return[];throw t("NOR_ERROR",s,r,l,n)}}class o extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),r=s.length,n=l.length,i=this.filterRejectedResults(l);if(0===r||r===n)return s;throw t("XNOR_ERROR",r,n,l,i)}}class c extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),r=s.length,n=l.length,i=this.filterRejectedResults(l);if(1===r)return s[0];throw t("XOR_ERROR",r,n,l,i)}}class u extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let s=0,r=0;const n=[...e],i=n?.length??0;0!==i?n.forEach((e,n)=>{Promise.resolve(e).then(e=>{r++,l[n]=e}).catch(()=>{l[n]=void 0}).finally(()=>{s++,r>0?t(l.filter(e=>void 0!==e)):s===i&&t([])})}):t(l)})}}class d extends l{async execute(e){return new Promise(t=>{const l=[];let s=0,r=0;const n=[...e],i=n?.length??0;0!==i?n.forEach((e,n)=>{Promise.resolve(e).then(()=>{l[n]=void 0}).catch(e=>{r++,l[n]=e}).finally(()=>{s++,r>0?t(l.filter(e=>void 0!==e)):s===i&&t([])})}):t(l)})}}class f{constructor(e){this.promise=e}maxTimer(e,t=`Promise timed out after ${e}ms`){let l;return Promise.race([this.promise,new Promise((s,r)=>{l=setTimeout(()=>{r(new Error(t))},e)})]).finally(()=>clearTimeout(l))}then(e,t){return new f(this.promise.then(e,t))}catch(e){return new f(this.promise.catch(e))}finally(e){return new f(this.promise.finally(e))}toPromise(){return this.promise}}class R{static get gates(){return{and:new r,or:new s,xor:new c,nand:new i,nor:new a,xnor:new o,majority:new n,allFulfilled:new u,allRejected:new d}}static and(e){return new f(this.gates.and.execute(e))}static or(e){return new f(this.gates.or.execute(e))}static xor(e){return new f(this.gates.xor.execute(e))}static nand(e){return new f(this.gates.nand.execute(e))}static nor(e){return new f(this.gates.nor.execute(e))}static xnor(e){return new f(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new f(this.gates.majority.execute(e,t))}static allFulfilled(e){return new f(this.gates.allFulfilled.execute(e))}static allRejected(e){return new f(this.gates.allRejected.execute(e))}static not(e){return new f(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new f(Promise.race(e))}static allSettled(e){return new f(Promise.allSettled(e))}static createFlipFlop(e=!1){let t=e,l=null,s=Promise.resolve(t);const r=()=>(l||(s=new Promise(e=>{l=e})),s);return{getState:()=>t,set:async e=>(t=e,l&&(l(t),l=null),s=Promise.resolve(t),t),async toggle(){return this.set(!t)},waitForChange:r,waitFor:e=>t===e?Promise.resolve(t):new Promise(l=>{const s=()=>{t===e?l(t):r().then(s)};s()})}}}exports.createPromiseLogic=function(e={}){const{prefix:t="",suffix:l="",rename:s={}}=e,r={and:R.and.bind(R),or:R.or.bind(R),not:R.not.bind(R),race:R.race.bind(R),allSettled:R.allSettled.bind(R),xor:R.xor.bind(R),nand:R.nand.bind(R),nor:R.nor.bind(R),xnor:R.xnor.bind(R),majority:R.majority.bind(R),allFulfilled:R.allFulfilled.bind(R),allRejected:R.allRejected.bind(R)},n={};return Object.entries(r).forEach(([e,r])=>{const i=s[e]||e;n[`${t}${i}${l}`]=r}),n};
@@ -1 +1 @@
1
- class e extends Error{constructor(e,t,l){super(t),this.type=e,this.results=l,this.name="PromiseLogicError"}}function t(t,l,s,r,n){const i={XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${l} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${s} promises fulfilled (expected at least one rejection).`,NOR_ERROR:`NOR condition failed: ${l} promises fulfilled (expected all rejected).`,XNOR_ERROR:`XNOR condition failed: ${l}/${s} promises fulfilled (expected all or none).`,MAJORITY_ERROR:`Majority condition failed: ${l}/${s} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${l}/${s} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${s-l}/${s} promises rejected (expected all to fail).`}[t]||"Logic condition failed",a=n&&n?.length>0?`\n失败原因:${n.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,i+a,r)}class l{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}}class s extends l{async execute(e){try{return Promise.any(e)}catch(e){throw t("OR_ERROR",0,0,[],[e])}}}class r extends l{async execute(e){try{return Promise.all(e)}catch(e){throw t("AND_ERROR",0,0,[],[e])}}}class n extends l{async execute(e,l){const s=await Promise.allSettled(e),r=this.filterFulfilledResults(s),n=r.length,i=s.length,a=this.filterRejectedResults(s);if(n>Math.floor(i*l.max))return r;throw t("MAJORITY_ERROR",n,i,s,a)}}class i extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),r=s.length,n=l.length,i=this.filterRejectedResults(l);if(r===n)throw t("NAND_ERROR",r,n,l,i);return s}}class a extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l).length,r=l.length,n=this.filterRejectedResults(l);if(0===s)return[];throw t("NOR_ERROR",s,r,l,n)}}class o extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),r=s.length,n=l.length,i=this.filterRejectedResults(l);if(0===r||r===n)return s;throw t("XNOR_ERROR",r,n,l,i)}}class c extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),r=s.length,n=l.length,i=this.filterRejectedResults(l);if(1===r)return s[0];throw t("XOR_ERROR",r,n,l,i)}}class u extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let s=0;const r=[...e];0!==(r?.length??0)?r.forEach((e,r)=>{Promise.resolve(e).then(e=>{s++,l[r]=e}).catch(()=>{l[r]=void 0}).finally(()=>{s>0&&t(l.filter(e=>void 0!==e))})}):t(l)})}}class d extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let s=0;const r=[...e];0!==(r?.length??0)?r.forEach((e,r)=>{Promise.resolve(e).then(()=>{l[r]=void 0}).catch(e=>{s++,l[r]=e}).finally(()=>{s>0&&t(l.filter(e=>void 0!==e))})}):t(l)})}}class f{constructor(e){this.promise=e}maxTimer(e,t=`Promise timed out after ${e}ms`){let l;return Promise.race([this.promise,new Promise((s,r)=>{l=setTimeout(()=>{r(new Error(t))},e)})]).finally(()=>clearTimeout(l))}then(e,t){return new f(this.promise.then(e,t))}catch(e){return new f(this.promise.catch(e))}finally(e){return new f(this.promise.finally(e))}toPromise(){return this.promise}}class R{static get gates(){return{and:new r,or:new s,xor:new c,nand:new i,nor:new a,xnor:new o,majority:new n,allFulfilled:new u,allRejected:new d}}static and(e){return new f(this.gates.and.execute(e))}static or(e){return new f(this.gates.or.execute(e))}static xor(e){return new f(this.gates.xor.execute(e))}static nand(e){return new f(this.gates.nand.execute(e))}static nor(e){return new f(this.gates.nor.execute(e))}static xnor(e){return new f(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new f(this.gates.majority.execute(e,t))}static allFulfilled(e){return new f(this.gates.allFulfilled.execute(e))}static allRejected(e){return new f(this.gates.allRejected.execute(e))}static not(e){return new f(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new f(Promise.race(e))}static allSettled(e){return new f(Promise.allSettled(e))}static createFlipFlop(e=!1){let t=e,l=null,s=Promise.resolve(t);const r=()=>(l||(s=new Promise(e=>{l=e})),s);return{getState:()=>t,set:async e=>(t=e,l&&(l(t),l=null),s=Promise.resolve(t),t),async toggle(){return this.set(!t)},waitForChange:r,waitFor:e=>t===e?Promise.resolve(t):new Promise(l=>{const s=()=>{t===e?l(t):r().then(s)};s()})}}}function h(e={}){const{prefix:t="",suffix:l="",rename:s={}}=e,r={and:R.and.bind(R),or:R.or.bind(R),not:R.not.bind(R),race:R.race.bind(R),allSettled:R.allSettled.bind(R),xor:R.xor.bind(R),nand:R.nand.bind(R),nor:R.nor.bind(R),xnor:R.xnor.bind(R),majority:R.majority.bind(R),allFulfilled:R.allFulfilled.bind(R),allRejected:R.allRejected.bind(R)},n={};return Object.entries(r).forEach(([e,r])=>{const i=s[e]||e;n[`${t}${i}${l}`]=r}),n}export{h as createPromiseLogic};
1
+ class e extends Error{constructor(e,t,l){super(t),this.type=e,this.results=l,this.name="PromiseLogicError"}}function t(t,l,s,r,n){const i={XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${l} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${s} promises fulfilled (expected at least one rejection).`,NOR_ERROR:`NOR condition failed: ${l} promises fulfilled (expected all rejected).`,XNOR_ERROR:`XNOR condition failed: ${l}/${s} promises fulfilled (expected all or none).`,MAJORITY_ERROR:`Majority condition failed: ${l}/${s} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${l}/${s} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${s-l}/${s} promises rejected (expected all to fail).`}[t]||"Logic condition failed",a=n&&n?.length>0?`\n失败原因:${n.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,i+a,r)}class l{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}}class s extends l{async execute(e){try{return Promise.any(e)}catch(e){throw t("OR_ERROR",0,0,[],[e])}}}class r extends l{async execute(e){try{return Promise.all(e)}catch(e){throw t("AND_ERROR",0,0,[],[e])}}}class n extends l{async execute(e,l){const s=await Promise.allSettled(e),r=this.filterFulfilledResults(s),n=r.length,i=s.length,a=this.filterRejectedResults(s);if(n>Math.floor(i*l.max))return r;throw t("MAJORITY_ERROR",n,i,s,a)}}class i extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),r=s.length,n=l.length,i=this.filterRejectedResults(l);if(r===n)throw t("NAND_ERROR",r,n,l,i);return s}}class a extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l).length,r=l.length,n=this.filterRejectedResults(l);if(0===s)return[];throw t("NOR_ERROR",s,r,l,n)}}class o extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),r=s.length,n=l.length,i=this.filterRejectedResults(l);if(0===r||r===n)return s;throw t("XNOR_ERROR",r,n,l,i)}}class c extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),r=s.length,n=l.length,i=this.filterRejectedResults(l);if(1===r)return s[0];throw t("XOR_ERROR",r,n,l,i)}}class u extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let s=0,r=0;const n=[...e],i=n?.length??0;0!==i?n.forEach((e,n)=>{Promise.resolve(e).then(e=>{r++,l[n]=e}).catch(()=>{l[n]=void 0}).finally(()=>{s++,r>0?t(l.filter(e=>void 0!==e)):s===i&&t([])})}):t(l)})}}class d extends l{async execute(e){return new Promise(t=>{const l=[];let s=0,r=0;const n=[...e],i=n?.length??0;0!==i?n.forEach((e,n)=>{Promise.resolve(e).then(()=>{l[n]=void 0}).catch(e=>{r++,l[n]=e}).finally(()=>{s++,r>0?t(l.filter(e=>void 0!==e)):s===i&&t([])})}):t(l)})}}class f{constructor(e){this.promise=e}maxTimer(e,t=`Promise timed out after ${e}ms`){let l;return Promise.race([this.promise,new Promise((s,r)=>{l=setTimeout(()=>{r(new Error(t))},e)})]).finally(()=>clearTimeout(l))}then(e,t){return new f(this.promise.then(e,t))}catch(e){return new f(this.promise.catch(e))}finally(e){return new f(this.promise.finally(e))}toPromise(){return this.promise}}class R{static get gates(){return{and:new r,or:new s,xor:new c,nand:new i,nor:new a,xnor:new o,majority:new n,allFulfilled:new u,allRejected:new d}}static and(e){return new f(this.gates.and.execute(e))}static or(e){return new f(this.gates.or.execute(e))}static xor(e){return new f(this.gates.xor.execute(e))}static nand(e){return new f(this.gates.nand.execute(e))}static nor(e){return new f(this.gates.nor.execute(e))}static xnor(e){return new f(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new f(this.gates.majority.execute(e,t))}static allFulfilled(e){return new f(this.gates.allFulfilled.execute(e))}static allRejected(e){return new f(this.gates.allRejected.execute(e))}static not(e){return new f(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new f(Promise.race(e))}static allSettled(e){return new f(Promise.allSettled(e))}static createFlipFlop(e=!1){let t=e,l=null,s=Promise.resolve(t);const r=()=>(l||(s=new Promise(e=>{l=e})),s);return{getState:()=>t,set:async e=>(t=e,l&&(l(t),l=null),s=Promise.resolve(t),t),async toggle(){return this.set(!t)},waitForChange:r,waitFor:e=>t===e?Promise.resolve(t):new Promise(l=>{const s=()=>{t===e?l(t):r().then(s)};s()})}}}function h(e={}){const{prefix:t="",suffix:l="",rename:s={}}=e,r={and:R.and.bind(R),or:R.or.bind(R),not:R.not.bind(R),race:R.race.bind(R),allSettled:R.allSettled.bind(R),xor:R.xor.bind(R),nand:R.nand.bind(R),nor:R.nor.bind(R),xnor:R.xnor.bind(R),majority:R.majority.bind(R),allFulfilled:R.allFulfilled.bind(R),allRejected:R.allRejected.bind(R)},n={};return Object.entries(r).forEach(([e,r])=>{const i=s[e]||e;n[`${t}${i}${l}`]=r}),n}export{h as createPromiseLogic};
@@ -1 +1 @@
1
- "use strict";class e extends Error{constructor(e,t,l){super(t),this.type=e,this.results=l,this.name="PromiseLogicError"}}function t(t,l,r,s,i){const n={XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${l} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${r} promises fulfilled (expected at least one rejection).`,NOR_ERROR:`NOR condition failed: ${l} promises fulfilled (expected all rejected).`,XNOR_ERROR:`XNOR condition failed: ${l}/${r} promises fulfilled (expected all or none).`,MAJORITY_ERROR:`Majority condition failed: ${l}/${r} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${l}/${r} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${r-l}/${r} promises rejected (expected all to fail).`}[t]||"Logic condition failed",o=i&&i?.length>0?`\n失败原因:${i.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,n+o,s)}class l{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}}class r extends l{async execute(e){try{return Promise.any(e)}catch(e){throw t("OR_ERROR",0,0,[],[e])}}}class s extends l{async execute(e){try{return Promise.all(e)}catch(e){throw t("AND_ERROR",0,0,[],[e])}}}class i extends l{async execute(e,l){const r=await Promise.allSettled(e),s=this.filterFulfilledResults(r),i=s.length,n=r.length,o=this.filterRejectedResults(r);if(i>Math.floor(n*l.max))return s;throw t("MAJORITY_ERROR",i,n,r,o)}}class n extends l{async execute(e){const l=await Promise.allSettled(e),r=this.filterFulfilledResults(l),s=r.length,i=l.length,n=this.filterRejectedResults(l);if(s===i)throw t("NAND_ERROR",s,i,l,n);return r}}class o extends l{async execute(e){const l=await Promise.allSettled(e),r=this.filterFulfilledResults(l).length,s=l.length,i=this.filterRejectedResults(l);if(0===r)return[];throw t("NOR_ERROR",r,s,l,i)}}class a extends l{async execute(e){const l=await Promise.allSettled(e),r=this.filterFulfilledResults(l),s=r.length,i=l.length,n=this.filterRejectedResults(l);if(0===s||s===i)return r;throw t("XNOR_ERROR",s,i,l,n)}}class c extends l{async execute(e){const l=await Promise.allSettled(e),r=this.filterFulfilledResults(l),s=r.length,i=l.length,n=this.filterRejectedResults(l);if(1===s)return r[0];throw t("XOR_ERROR",s,i,l,n)}}class u extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let r=0;const s=[...e];0!==(s?.length??0)?s.forEach((e,s)=>{Promise.resolve(e).then(e=>{r++,l[s]=e}).catch(()=>{l[s]=void 0}).finally(()=>{r>0&&t(l.filter(e=>void 0!==e))})}):t(l)})}}class d extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let r=0;const s=[...e];0!==(s?.length??0)?s.forEach((e,s)=>{Promise.resolve(e).then(()=>{l[s]=void 0}).catch(e=>{r++,l[s]=e}).finally(()=>{r>0&&t(l.filter(e=>void 0!==e))})}):t(l)})}}class f{constructor(e){this.promise=e}maxTimer(e,t=`Promise timed out after ${e}ms`){let l;return Promise.race([this.promise,new Promise((r,s)=>{l=setTimeout(()=>{s(new Error(t))},e)})]).finally(()=>clearTimeout(l))}then(e,t){return new f(this.promise.then(e,t))}catch(e){return new f(this.promise.catch(e))}finally(e){return new f(this.promise.finally(e))}toPromise(){return this.promise}}class R{static get gates(){return{and:new s,or:new r,xor:new c,nand:new n,nor:new o,xnor:new a,majority:new i,allFulfilled:new u,allRejected:new d}}static and(e){return new f(this.gates.and.execute(e))}static or(e){return new f(this.gates.or.execute(e))}static xor(e){return new f(this.gates.xor.execute(e))}static nand(e){return new f(this.gates.nand.execute(e))}static nor(e){return new f(this.gates.nor.execute(e))}static xnor(e){return new f(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new f(this.gates.majority.execute(e,t))}static allFulfilled(e){return new f(this.gates.allFulfilled.execute(e))}static allRejected(e){return new f(this.gates.allRejected.execute(e))}static not(e){return new f(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new f(Promise.race(e))}static allSettled(e){return new f(Promise.allSettled(e))}static createFlipFlop(e=!1){let t=e,l=null,r=Promise.resolve(t);const s=()=>(l||(r=new Promise(e=>{l=e})),r);return{getState:()=>t,set:async e=>(t=e,l&&(l(t),l=null),r=Promise.resolve(t),t),async toggle(){return this.set(!t)},waitForChange:s,waitFor:e=>t===e?Promise.resolve(t):new Promise(l=>{const r=()=>{t===e?l(t):s().then(r)};r()})}}}exports.PromiseLogic=R,exports.PromiseLogicError=e,exports.PromiseWithTimer=f,exports.createPromiseLogic=function(e={}){const{prefix:t="",suffix:l="",rename:r={}}=e,s={and:R.and.bind(R),or:R.or.bind(R),not:R.not.bind(R),race:R.race.bind(R),allSettled:R.allSettled.bind(R),xor:R.xor.bind(R),nand:R.nand.bind(R),nor:R.nor.bind(R),xnor:R.xnor.bind(R),majority:R.majority.bind(R),allFulfilled:R.allFulfilled.bind(R),allRejected:R.allRejected.bind(R)},i={};return Object.entries(s).forEach(([e,s])=>{const n=r[e]||e;i[`${t}${n}${l}`]=s}),i};
1
+ "use strict";class e extends Error{constructor(e,t,l){super(t),this.type=e,this.results=l,this.name="PromiseLogicError"}}function t(t,l,r,s,i){const n={XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${l} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${r} promises fulfilled (expected at least one rejection).`,NOR_ERROR:`NOR condition failed: ${l} promises fulfilled (expected all rejected).`,XNOR_ERROR:`XNOR condition failed: ${l}/${r} promises fulfilled (expected all or none).`,MAJORITY_ERROR:`Majority condition failed: ${l}/${r} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${l}/${r} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${r-l}/${r} promises rejected (expected all to fail).`}[t]||"Logic condition failed",o=i&&i?.length>0?`\n失败原因:${i.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,n+o,s)}class l{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}}class r extends l{async execute(e){try{return Promise.any(e)}catch(e){throw t("OR_ERROR",0,0,[],[e])}}}class s extends l{async execute(e){try{return Promise.all(e)}catch(e){throw t("AND_ERROR",0,0,[],[e])}}}class i extends l{async execute(e,l){const r=await Promise.allSettled(e),s=this.filterFulfilledResults(r),i=s.length,n=r.length,o=this.filterRejectedResults(r);if(i>Math.floor(n*l.max))return s;throw t("MAJORITY_ERROR",i,n,r,o)}}class n extends l{async execute(e){const l=await Promise.allSettled(e),r=this.filterFulfilledResults(l),s=r.length,i=l.length,n=this.filterRejectedResults(l);if(s===i)throw t("NAND_ERROR",s,i,l,n);return r}}class o extends l{async execute(e){const l=await Promise.allSettled(e),r=this.filterFulfilledResults(l).length,s=l.length,i=this.filterRejectedResults(l);if(0===r)return[];throw t("NOR_ERROR",r,s,l,i)}}class a extends l{async execute(e){const l=await Promise.allSettled(e),r=this.filterFulfilledResults(l),s=r.length,i=l.length,n=this.filterRejectedResults(l);if(0===s||s===i)return r;throw t("XNOR_ERROR",s,i,l,n)}}class c extends l{async execute(e){const l=await Promise.allSettled(e),r=this.filterFulfilledResults(l),s=r.length,i=l.length,n=this.filterRejectedResults(l);if(1===s)return r[0];throw t("XOR_ERROR",s,i,l,n)}}class u extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let r=0,s=0;const i=[...e],n=i?.length??0;0!==n?i.forEach((e,i)=>{Promise.resolve(e).then(e=>{s++,l[i]=e}).catch(()=>{l[i]=void 0}).finally(()=>{r++,s>0?t(l.filter(e=>void 0!==e)):r===n&&t([])})}):t(l)})}}class d extends l{async execute(e){return new Promise(t=>{const l=[];let r=0,s=0;const i=[...e],n=i?.length??0;0!==n?i.forEach((e,i)=>{Promise.resolve(e).then(()=>{l[i]=void 0}).catch(e=>{s++,l[i]=e}).finally(()=>{r++,s>0?t(l.filter(e=>void 0!==e)):r===n&&t([])})}):t(l)})}}class f{constructor(e){this.promise=e}maxTimer(e,t=`Promise timed out after ${e}ms`){let l;return Promise.race([this.promise,new Promise((r,s)=>{l=setTimeout(()=>{s(new Error(t))},e)})]).finally(()=>clearTimeout(l))}then(e,t){return new f(this.promise.then(e,t))}catch(e){return new f(this.promise.catch(e))}finally(e){return new f(this.promise.finally(e))}toPromise(){return this.promise}}class R{static get gates(){return{and:new s,or:new r,xor:new c,nand:new n,nor:new o,xnor:new a,majority:new i,allFulfilled:new u,allRejected:new d}}static and(e){return new f(this.gates.and.execute(e))}static or(e){return new f(this.gates.or.execute(e))}static xor(e){return new f(this.gates.xor.execute(e))}static nand(e){return new f(this.gates.nand.execute(e))}static nor(e){return new f(this.gates.nor.execute(e))}static xnor(e){return new f(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new f(this.gates.majority.execute(e,t))}static allFulfilled(e){return new f(this.gates.allFulfilled.execute(e))}static allRejected(e){return new f(this.gates.allRejected.execute(e))}static not(e){return new f(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new f(Promise.race(e))}static allSettled(e){return new f(Promise.allSettled(e))}static createFlipFlop(e=!1){let t=e,l=null,r=Promise.resolve(t);const s=()=>(l||(r=new Promise(e=>{l=e})),r);return{getState:()=>t,set:async e=>(t=e,l&&(l(t),l=null),r=Promise.resolve(t),t),async toggle(){return this.set(!t)},waitForChange:s,waitFor:e=>t===e?Promise.resolve(t):new Promise(l=>{const r=()=>{t===e?l(t):s().then(r)};r()})}}}exports.PromiseLogic=R,exports.PromiseLogicError=e,exports.PromiseWithTimer=f,exports.createPromiseLogic=function(e={}){const{prefix:t="",suffix:l="",rename:r={}}=e,s={and:R.and.bind(R),or:R.or.bind(R),not:R.not.bind(R),race:R.race.bind(R),allSettled:R.allSettled.bind(R),xor:R.xor.bind(R),nand:R.nand.bind(R),nor:R.nor.bind(R),xnor:R.xnor.bind(R),majority:R.majority.bind(R),allFulfilled:R.allFulfilled.bind(R),allRejected:R.allRejected.bind(R)},i={};return Object.entries(s).forEach(([e,s])=>{const n=r[e]||e;i[`${t}${n}${l}`]=s}),i};
@@ -1 +1 @@
1
- class e extends Error{constructor(e,t,l){super(t),this.type=e,this.results=l,this.name="PromiseLogicError"}}function t(t,l,s,r,n){const i={XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${l} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${s} promises fulfilled (expected at least one rejection).`,NOR_ERROR:`NOR condition failed: ${l} promises fulfilled (expected all rejected).`,XNOR_ERROR:`XNOR condition failed: ${l}/${s} promises fulfilled (expected all or none).`,MAJORITY_ERROR:`Majority condition failed: ${l}/${s} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${l}/${s} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${s-l}/${s} promises rejected (expected all to fail).`}[t]||"Logic condition failed",a=n&&n?.length>0?`\n失败原因:${n.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,i+a,r)}class l{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}}class s extends l{async execute(e){try{return Promise.any(e)}catch(e){throw t("OR_ERROR",0,0,[],[e])}}}class r extends l{async execute(e){try{return Promise.all(e)}catch(e){throw t("AND_ERROR",0,0,[],[e])}}}class n extends l{async execute(e,l){const s=await Promise.allSettled(e),r=this.filterFulfilledResults(s),n=r.length,i=s.length,a=this.filterRejectedResults(s);if(n>Math.floor(i*l.max))return r;throw t("MAJORITY_ERROR",n,i,s,a)}}class i extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),r=s.length,n=l.length,i=this.filterRejectedResults(l);if(r===n)throw t("NAND_ERROR",r,n,l,i);return s}}class a extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l).length,r=l.length,n=this.filterRejectedResults(l);if(0===s)return[];throw t("NOR_ERROR",s,r,l,n)}}class o extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),r=s.length,n=l.length,i=this.filterRejectedResults(l);if(0===r||r===n)return s;throw t("XNOR_ERROR",r,n,l,i)}}class c extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),r=s.length,n=l.length,i=this.filterRejectedResults(l);if(1===r)return s[0];throw t("XOR_ERROR",r,n,l,i)}}class u extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let s=0;const r=[...e];0!==(r?.length??0)?r.forEach((e,r)=>{Promise.resolve(e).then(e=>{s++,l[r]=e}).catch(()=>{l[r]=void 0}).finally(()=>{s>0&&t(l.filter(e=>void 0!==e))})}):t(l)})}}class d extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let s=0;const r=[...e];0!==(r?.length??0)?r.forEach((e,r)=>{Promise.resolve(e).then(()=>{l[r]=void 0}).catch(e=>{s++,l[r]=e}).finally(()=>{s>0&&t(l.filter(e=>void 0!==e))})}):t(l)})}}class f{constructor(e){this.promise=e}maxTimer(e,t=`Promise timed out after ${e}ms`){let l;return Promise.race([this.promise,new Promise((s,r)=>{l=setTimeout(()=>{r(new Error(t))},e)})]).finally(()=>clearTimeout(l))}then(e,t){return new f(this.promise.then(e,t))}catch(e){return new f(this.promise.catch(e))}finally(e){return new f(this.promise.finally(e))}toPromise(){return this.promise}}class R{static get gates(){return{and:new r,or:new s,xor:new c,nand:new i,nor:new a,xnor:new o,majority:new n,allFulfilled:new u,allRejected:new d}}static and(e){return new f(this.gates.and.execute(e))}static or(e){return new f(this.gates.or.execute(e))}static xor(e){return new f(this.gates.xor.execute(e))}static nand(e){return new f(this.gates.nand.execute(e))}static nor(e){return new f(this.gates.nor.execute(e))}static xnor(e){return new f(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new f(this.gates.majority.execute(e,t))}static allFulfilled(e){return new f(this.gates.allFulfilled.execute(e))}static allRejected(e){return new f(this.gates.allRejected.execute(e))}static not(e){return new f(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new f(Promise.race(e))}static allSettled(e){return new f(Promise.allSettled(e))}static createFlipFlop(e=!1){let t=e,l=null,s=Promise.resolve(t);const r=()=>(l||(s=new Promise(e=>{l=e})),s);return{getState:()=>t,set:async e=>(t=e,l&&(l(t),l=null),s=Promise.resolve(t),t),async toggle(){return this.set(!t)},waitForChange:r,waitFor:e=>t===e?Promise.resolve(t):new Promise(l=>{const s=()=>{t===e?l(t):r().then(s)};s()})}}}function h(e={}){const{prefix:t="",suffix:l="",rename:s={}}=e,r={and:R.and.bind(R),or:R.or.bind(R),not:R.not.bind(R),race:R.race.bind(R),allSettled:R.allSettled.bind(R),xor:R.xor.bind(R),nand:R.nand.bind(R),nor:R.nor.bind(R),xnor:R.xnor.bind(R),majority:R.majority.bind(R),allFulfilled:R.allFulfilled.bind(R),allRejected:R.allRejected.bind(R)},n={};return Object.entries(r).forEach(([e,r])=>{const i=s[e]||e;n[`${t}${i}${l}`]=r}),n}export{R as PromiseLogic,e as PromiseLogicError,f as PromiseWithTimer,h as createPromiseLogic};
1
+ class e extends Error{constructor(e,t,l){super(t),this.type=e,this.results=l,this.name="PromiseLogicError"}}function t(t,l,s,r,n){const i={XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${l} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${s} promises fulfilled (expected at least one rejection).`,NOR_ERROR:`NOR condition failed: ${l} promises fulfilled (expected all rejected).`,XNOR_ERROR:`XNOR condition failed: ${l}/${s} promises fulfilled (expected all or none).`,MAJORITY_ERROR:`Majority condition failed: ${l}/${s} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${l}/${s} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${s-l}/${s} promises rejected (expected all to fail).`}[t]||"Logic condition failed",a=n&&n?.length>0?`\n失败原因:${n.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,i+a,r)}class l{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}}class s extends l{async execute(e){try{return Promise.any(e)}catch(e){throw t("OR_ERROR",0,0,[],[e])}}}class r extends l{async execute(e){try{return Promise.all(e)}catch(e){throw t("AND_ERROR",0,0,[],[e])}}}class n extends l{async execute(e,l){const s=await Promise.allSettled(e),r=this.filterFulfilledResults(s),n=r.length,i=s.length,a=this.filterRejectedResults(s);if(n>Math.floor(i*l.max))return r;throw t("MAJORITY_ERROR",n,i,s,a)}}class i extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),r=s.length,n=l.length,i=this.filterRejectedResults(l);if(r===n)throw t("NAND_ERROR",r,n,l,i);return s}}class a extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l).length,r=l.length,n=this.filterRejectedResults(l);if(0===s)return[];throw t("NOR_ERROR",s,r,l,n)}}class o extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),r=s.length,n=l.length,i=this.filterRejectedResults(l);if(0===r||r===n)return s;throw t("XNOR_ERROR",r,n,l,i)}}class c extends l{async execute(e){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),r=s.length,n=l.length,i=this.filterRejectedResults(l);if(1===r)return s[0];throw t("XOR_ERROR",r,n,l,i)}}class u extends l{async execute(e,t={}){return new Promise(t=>{const l=[];let s=0,r=0;const n=[...e],i=n?.length??0;0!==i?n.forEach((e,n)=>{Promise.resolve(e).then(e=>{r++,l[n]=e}).catch(()=>{l[n]=void 0}).finally(()=>{s++,r>0?t(l.filter(e=>void 0!==e)):s===i&&t([])})}):t(l)})}}class d extends l{async execute(e){return new Promise(t=>{const l=[];let s=0,r=0;const n=[...e],i=n?.length??0;0!==i?n.forEach((e,n)=>{Promise.resolve(e).then(()=>{l[n]=void 0}).catch(e=>{r++,l[n]=e}).finally(()=>{s++,r>0?t(l.filter(e=>void 0!==e)):s===i&&t([])})}):t(l)})}}class f{constructor(e){this.promise=e}maxTimer(e,t=`Promise timed out after ${e}ms`){let l;return Promise.race([this.promise,new Promise((s,r)=>{l=setTimeout(()=>{r(new Error(t))},e)})]).finally(()=>clearTimeout(l))}then(e,t){return new f(this.promise.then(e,t))}catch(e){return new f(this.promise.catch(e))}finally(e){return new f(this.promise.finally(e))}toPromise(){return this.promise}}class R{static get gates(){return{and:new r,or:new s,xor:new c,nand:new i,nor:new a,xnor:new o,majority:new n,allFulfilled:new u,allRejected:new d}}static and(e){return new f(this.gates.and.execute(e))}static or(e){return new f(this.gates.or.execute(e))}static xor(e){return new f(this.gates.xor.execute(e))}static nand(e){return new f(this.gates.nand.execute(e))}static nor(e){return new f(this.gates.nor.execute(e))}static xnor(e){return new f(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new f(this.gates.majority.execute(e,t))}static allFulfilled(e){return new f(this.gates.allFulfilled.execute(e))}static allRejected(e){return new f(this.gates.allRejected.execute(e))}static not(e){return new f(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new f(Promise.race(e))}static allSettled(e){return new f(Promise.allSettled(e))}static createFlipFlop(e=!1){let t=e,l=null,s=Promise.resolve(t);const r=()=>(l||(s=new Promise(e=>{l=e})),s);return{getState:()=>t,set:async e=>(t=e,l&&(l(t),l=null),s=Promise.resolve(t),t),async toggle(){return this.set(!t)},waitForChange:r,waitFor:e=>t===e?Promise.resolve(t):new Promise(l=>{const s=()=>{t===e?l(t):r().then(s)};s()})}}}function h(e={}){const{prefix:t="",suffix:l="",rename:s={}}=e,r={and:R.and.bind(R),or:R.or.bind(R),not:R.not.bind(R),race:R.race.bind(R),allSettled:R.allSettled.bind(R),xor:R.xor.bind(R),nand:R.nand.bind(R),nor:R.nor.bind(R),xnor:R.xnor.bind(R),majority:R.majority.bind(R),allFulfilled:R.allFulfilled.bind(R),allRejected:R.allRejected.bind(R)},n={};return Object.entries(r).forEach(([e,r])=>{const i=s[e]||e;n[`${t}${i}${l}`]=r}),n}export{R as PromiseLogic,e as PromiseLogicError,f as PromiseWithTimer,h as createPromiseLogic};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "promise-logic",
3
- "version": "2.8.4",
3
+ "version": "2.8.5",
4
4
  "description": "Compose promises with logic gate semantics (AND, OR, XOR, NAND, NOR, XNOR, Majority). Forget APIs, remember logic.",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs.js",