promise-logic 2.7.0 → 2.8.1

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.
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ### **1. Core Philosophy**
2
2
 
3
3
  **Replace API Memory with Logical Concepts**
4
- The design philosophy of `promise-logic` is: **Developers should focus on business logic, not on the details of Promise APIs**.
4
+ The design philosophy of `promise-logic` is: **Developers should focus on business logic, not on details of Promise APIs**.
5
5
  Traditional Promise combinations (such as `Promise.all`, `Promise.race`) have naming and semantics that are not intuitive enough, especially in complex asynchronous scenarios where code readability rapidly declines.
6
6
  `promise-logic` abstracts asynchronous combinations into logical operations like `and`, `or`, `xor` through the concept of **Logic Gates**, making code semantically clear and self-explanatory.
7
7
 
@@ -10,8 +10,8 @@ Traditional Promise combinations (such as `Promise.all`, `Promise.race`) have na
10
10
  ### **2. Features**
11
11
 
12
12
  1. **Logical Semantics**
13
- - `and`: All tasks must succeed (equivalent to `Promise.all`)
14
- - `or`: At least one task succeeds (equivalent to `Promise.any`)
13
+ - `and`: All tasks must succeed (equivalent to native `Promise.all`)
14
+ - `or`: At least one task succeeds (equivalent to native `Promise.any`)
15
15
  - `xor`: **Exactly one task succeeds**
16
16
  - `nand`: Not all tasks succeed (at least one fails)
17
17
  - `nor`: All tasks fail (no task succeeds)
@@ -32,11 +32,13 @@ Traditional Promise combinations (such as `Promise.all`, `Promise.race`) have na
32
32
  5. **Timeout Control**
33
33
  - `maxTimer`: Adds timeout functionality to any Promise operation (unit: milliseconds).
34
34
 
35
- maxTimer can only detect timeout of Promise operations, cannot interrupt or cancel Promise operations themselves, this is a JavaScript characteristic.
35
+ **Note**:
36
+ - After timeout, it immediately interrupts the execution of the current Promise chain and jumps to error handling
37
+ - However, please note that this does not cancel underlying asynchronous operations that have already started (such as network requests, file read/write, etc.)
36
38
 
37
39
  6. **Extended Operations**
38
- - `allFulfilled`: Returns all successful results
39
- - `allRejected`: Returns all failed results
40
+ - `allFulfilled`: Returns all successful results in order. Returns immediately when a result exists.
41
+ - `allRejected`: Returns all failed results in order. Returns immediately when a result exists.
40
42
  - `allSettled`: Returns all results (both successful and failed)
41
43
 
42
44
  ---
@@ -119,18 +121,18 @@ PromiseLogic.majority<Response>(services)
119
121
  ```javascript
120
122
  import { PromiseLogic } from 'promise-logic';
121
123
 
122
- // Execute operation with timeout
124
+ // Execute operation with custom timeout error message
123
125
  PromiseLogic.and([
124
126
  Promise.resolve(1),
125
- new Promise((resolve) => setTimeout(resolve, 1000)), // 1 second operation
127
+ new Promise((resolve) => setTimeout(resolve, 3000)), // 3 second operation
126
128
  Promise.resolve(3)
127
129
  ])
128
- .maxTimer(2000) // 2 second timeout
130
+ .maxTimer(2000, 'Custom timeout error: operation did not complete within 2000ms') // 2 second timeout, custom error message
129
131
  .then((result) => {
130
132
  console.log('Operation completed within timeout:', result);
131
133
  })
132
134
  .catch((error) => {
133
- console.error('Operation timed out or failed:', error.message);
135
+ console.error('Operation timed out:', error.message); // Output: Custom timeout error: operation did not complete within 2000ms
134
136
  });
135
137
  ```
136
138
 
@@ -146,12 +148,12 @@ const operations = [
146
148
  Promise.reject('error2')
147
149
  ];
148
150
 
149
- // Get all successful results
151
+ // Get all successful results (returns immediately when a result exists)
150
152
  PromiseLogic.allFulfilled(operations).then((results) => {
151
153
  console.log('Successful results:', results); // ['success1', 'success2']
152
154
  });
153
155
 
154
- // Get all failed results
156
+ // Get all failed results (returns immediately when a result exists)
155
157
  PromiseLogic.allRejected(operations).then((errors) => {
156
158
  console.log('Failed results:', errors); // ['error1', 'error2']
157
159
  });
@@ -159,9 +161,90 @@ PromiseLogic.allRejected(operations).then((errors) => {
159
161
  // Get all results (both success and failure)
160
162
  PromiseLogic.allSettled(operations).then((results) => {
161
163
  console.log('All results:', results);
164
+ // Output:
165
+ // [
166
+ // { status: 'fulfilled', value: 'success1' },
167
+ // { status: 'rejected', reason: 'error1' },
168
+ // { status: 'fulfilled', value: 'success2' },
169
+ // { status: 'rejected', reason: 'error2' }
170
+ // ]
162
171
  });
163
172
  ```
164
173
 
174
+ #### Example: allFulfilled - Execution Timing and Results
175
+
176
+ ```javascript
177
+ import { PromiseLogic } from 'promise-logic';
178
+
179
+ const startTime = Date.now();
180
+ console.log('Start executing allFulfilled, time:', startTime);
181
+
182
+ const allFulfilledResult = await PromiseLogic.allFulfilled([
183
+ new Promise(resolve => {
184
+ console.log('First Promise started (slow)');
185
+ setTimeout(() => {
186
+ console.log('First Promise completed:', 'success1');
187
+ resolve('success1');
188
+ }, 100);
189
+ }),
190
+ Promise.reject('error'),
191
+ new Promise(resolve => {
192
+ console.log('Third Promise started (fast)');
193
+ setTimeout(() => {
194
+ console.log('Third Promise completed:', 'success2');
195
+ resolve('success2');
196
+ }, 10);
197
+ })
198
+ ]);
199
+
200
+ const endTime = Date.now();
201
+ const elapsedTime = endTime - startTime;
202
+ console.log('allFulfilled complete results:', allFulfilledResult); // ['success1', 'success2']
203
+ ```
204
+
205
+ **Explanation:**
206
+ - **First return info**: Third Promise completes at 10ms, immediately returns `['success2']`
207
+ - **Complete return info**: First Promise completes at 100ms, final complete result is `['success1', 'success2']`
208
+ - **Execution timing**: Returns immediately when a result exists, does not wait for all Promises to complete
209
+ - **Order preservation**: Complete results are returned in input order, not completion order
210
+
211
+ #### Example: allRejected - Execution Timing and Results
212
+
213
+ ```javascript
214
+ import { PromiseLogic } from 'promise-logic';
215
+
216
+ const startTime = Date.now();
217
+ console.log('Start executing allRejected, time:', startTime);
218
+
219
+ const allRejectedResult = await PromiseLogic.allRejected([
220
+ Promise.resolve('success1'),
221
+ new Promise((_, reject) => {
222
+ console.log('Second Promise started (fast)');
223
+ setTimeout(() => {
224
+ console.log('Second Promise completed:', 'error1');
225
+ reject('error1');
226
+ }, 10);
227
+ }),
228
+ new Promise((_, reject) => {
229
+ console.log('Third Promise started (slow)');
230
+ setTimeout(() => {
231
+ console.log('Third Promise completed:', 'error2');
232
+ reject('error2');
233
+ }, 100);
234
+ })
235
+ ]);
236
+
237
+ const endTime = Date.now();
238
+ const elapsedTime = endTime - startTime;
239
+ console.log('allRejected complete results:', allRejectedResult); // ['error1', 'error2']
240
+ ```
241
+
242
+ **Explanation:**
243
+ - **First return info**: Second Promise completes at 10ms, immediately returns `['error1']`
244
+ - **Complete return info**: Third Promise completes at 100ms, final complete result is `['error1', 'error2']`
245
+ - **Execution timing**: Returns immediately when a result exists, does not wait for all Promises to complete
246
+ - **Order preservation**: Complete results are returned in input order, not completion order
247
+
165
248
  #### Example: Custom majority threshold
166
249
 
167
250
  ```javascript
@@ -187,9 +270,17 @@ PromiseLogic.majority(services, { max: 0.4 })
187
270
 
188
271
  ## Recent Updates
189
272
 
273
+ ### v2.8.0
274
+
275
+ - **Performance optimization**: Optimized `allFulfilled` and `allRejected` implementation logic from the bottom layer, existing results return immediately while maintaining consistent input and output order
276
+ - **Added chain timeout control with custom error messages**: Can customize timeout error messages in the `maxTimer` method
277
+ - **Type fixes**: Fixed TypeScript version type declaration issues
278
+ - **Test completion**: Added complete test cases for `allFulfilled`, `allRejected`, and `maxTimer`
279
+ - **Code refactoring**: Improved code structure for better maintainability
280
+
190
281
  ### v2.7.0
191
282
 
192
- - **Added Modular Architecture**: Separated logic gate implementations into independent modules for better code maintainability
283
+ - **Added modular architecture**: Separated logic gate implementations into independent modules for better code maintainability
193
284
  - **Fixed NOT Logic Gate**: Fixed potential risks in NOT logic gate in production environments
194
285
  - **Improved Error Messages**: Enhanced error message format for clearer error details
195
286
  - **Enhanced Test Coverage**: Added complete factory function tests for v1 and v2 versions
@@ -242,41 +333,62 @@ PromiseLogic.and<number>([Promise.resolve(1), Promise.resolve(2)]);
242
333
 
243
334
  | API | Description |
244
335
  | :------------- | :--------------------------------------------------------------------------------------------------------------------------- |
245
- | `and` | All Promises succeed, returns result array; any failure causes overall failure. |
246
- | `or` | At least one Promise succeeds, returns first success result; all failures cause overall failure. |
336
+ | `and` | All Promises succeed, returns result array; any failure causes overall failure. Equivalent to native `Promise.all`. |
337
+ | `or` | At least one Promise succeeds, returns first success result; all failures cause overall failure. Equivalent to native `Promise.any`. |
247
338
  | `xor` | **Exactly one Promise succeeds**, returns that result; otherwise throws `XOR_ERROR`. |
248
339
  | `nand` | Not all Promises succeed (at least one fails), returns success result array; all succeed causes overall failure. |
249
340
  | `nor` | All Promises fail (no task succeeds), returns empty array; any success causes overall failure. |
250
341
  | `xnor` | All Promises succeed or all fail (same state), returns success result array; otherwise throws `XNOR_ERROR`. |
251
342
  | `not` | Inverts the result of a single Promise: success becomes failure, failure becomes success. |
252
- | `majority` | More than specified threshold of Promises succeed, returns success result array; otherwise overall failure. Accepts `options` parameter, where `max` property can customize threshold (default: 0.5), range: (0, 1). |
253
- | `allFulfilled` | Returns all successful results as an array, ignoring failures. |
254
- | `allRejected` | Returns all failed results as an array, ignoring successes. |
255
- | `allSettled` | Returns all results (both successful and failed) as an array. |
256
- | `race` | Returns the first completed Promise result (regardless of success or failure). |
257
- | `maxTimer` | Adds timeout functionality to any Promise operation (unit: milliseconds). |
343
+ | `majority` | More than specified threshold of Promises succeed, returns success result array; otherwise overall failure. Accepts `options` parameter, where `max` property can customize threshold (default: 0.5), range: [0, 1]. |
344
+ | `allFulfilled` | Returns all successful results as an array, ignoring failures. Returns immediately when a result exists, while maintaining consistent input and output order. |
345
+ | `allRejected` | Returns all failed results as an array, ignoring successes. Returns immediately when a result exists, while maintaining consistent input and output order. |
346
+ | `allSettled` | Returns all results (both successful and failed) as an array. Equivalent to native `Promise.allSettled`. |
347
+ | `race` | Returns the first completed Promise result (regardless of success or failure). Equivalent to native `Promise.race`. |
348
+ | `maxTimer` | Adds timeout functionality to any Promise operation (unit: milliseconds). Supports custom timeout error messages. |
258
349
 
259
- ## **Note**: `maxTimer` can only detect timeout of Promise operations, cannot interrupt or cancel Promise operations themselves, this is a JavaScript characteristic.
260
350
 
261
351
  ### **6. Real-world Application Scenarios**
262
352
 
263
353
  1. **Primary/Backup Service Calls**
264
354
  - Use `xor` to ensure **exactly one service responds**, avoiding duplicate processing.
355
+
265
356
  2. **Distributed Decision Making**
266
357
  - Use `majority` to implement majority consensus (e.g., distributed voting).
358
+
267
359
  3. **Resource Competition**
268
360
  - Use `or` to get the first available resource (e.g., CDN node selection).
269
361
  - Use `not` to check if a resource is available.
362
+
270
363
  4. **Full-link Validation**
271
364
  - Use `and` to ensure all dependent services succeed (e.g., order creation).
272
365
 
366
+ 5. **Timeout Control**
367
+ - Use `maxTimer` to add timeout functionality to any Promise operation (unit: milliseconds).
368
+ - Returns custom error message after timeout, default: `Promise timed out after ${ms} ms`.
369
+
370
+ 6. **Partial Success Handling**
371
+ - Use `allFulfilled` to execute all Promises concurrently and return successful result array (e.g., batch API calls, suitable for high concurrency and partial failure acceptance scenarios).
372
+ - Use `allRejected` to execute all Promises concurrently and return failed result array (e.g., error log collection, suitable for batch failure processing scenarios).
373
+
374
+ 7. **Full Result Retrieval**
375
+ - Use `allSettled` to get results from all Promises (regardless of success or failure).
376
+
377
+ 8. **Fast Response**
378
+ - Use `race` to return the first completed Promise result (regardless of success or failure).
379
+
380
+ 9. **State Validation**
381
+ - Use `nand` to verify that not all Promises succeed (at least one fails).
382
+ - Use `nor` to verify that all Promises fail (no task succeeds).
383
+ - Use `xnor` to verify that all Promises succeed or all fail (same state).
384
+
273
385
  ---
274
386
 
275
387
  ### **7. Contribution Guide**
276
388
 
277
389
  1. **Development Environment**
278
390
  ```bash
279
- git clone https://github.com/haowhite/promise-logic.git
391
+ git clone https://github.com/xier123456/promise-logic.git
280
392
  cd promise-logic
281
393
  npm install
282
394
  ```
@@ -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,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,[...e],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{constructor(e){this.promise=e}maxTimer(e){let t;return Promise.race([this.promise,new Promise((r,l)=>{t=setTimeout(()=>{l(new Error(`Promise timed out after ${e}ms`))},e)})]).finally(()=>clearTimeout(t))}then(e,t){return new u(this.promise.then(e,t))}catch(e){return new u(this.promise.catch(e))}finally(e){return new u(this.promise.finally(e))}toPromise(){return this.promise}}class d{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}}static and(e){return new u(this.gates.and.execute(e))}static or(e){return new u(this.gates.or.execute(e))}static not(e){return new u(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new u(Promise.race(e).then(e=>Promise.resolve(e)))}static allSettled(e){return new u(Promise.allSettled(e))}static xor(e){return new u(this.gates.xor.execute(e))}static nand(e){return new u(this.gates.nand.execute(e))}static nor(e){return new u(this.gates.nor.execute(e))}static xnor(e){return new u(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new u(this.gates.majority.execute(e,t))}static allFulfilled(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value)))}static allRejected(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason)))}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:d.and.bind(d),or:d.or.bind(d),race:d.race.bind(d),allSettled:d.allSettled.bind(d),xor:d.xor.bind(d),not:d.not.bind(d),nand:d.nand.bind(d),nor:d.nor.bind(d),xnor:d.xnor.bind(d),majority:d.majority.bind(d),allFulfilled:d.allFulfilled.bind(d),allRejected:d.allRejected.bind(d)},s={};return Object.entries(n).forEach(([e,n])=>{const i=l[e]||e;s[`${t}${i}${r}`]=n}),s};
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,[...e],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 +1 @@
1
- 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,[...e],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{constructor(e){this.promise=e}maxTimer(e){let t;return Promise.race([this.promise,new Promise((r,l)=>{t=setTimeout(()=>{l(new Error(`Promise timed out after ${e}ms`))},e)})]).finally(()=>clearTimeout(t))}then(e,t){return new u(this.promise.then(e,t))}catch(e){return new u(this.promise.catch(e))}finally(e){return new u(this.promise.finally(e))}toPromise(){return this.promise}}class d{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}}static and(e){return new u(this.gates.and.execute(e))}static or(e){return new u(this.gates.or.execute(e))}static not(e){return new u(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new u(Promise.race(e).then(e=>Promise.resolve(e)))}static allSettled(e){return new u(Promise.allSettled(e))}static xor(e){return new u(this.gates.xor.execute(e))}static nand(e){return new u(this.gates.nand.execute(e))}static nor(e){return new u(this.gates.nor.execute(e))}static xnor(e){return new u(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new u(this.gates.majority.execute(e,t))}static allFulfilled(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value)))}static allRejected(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason)))}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()})}}}}function f(e={}){const{prefix:t="",suffix:r="",rename:l={}}=e,n={and:d.and.bind(d),or:d.or.bind(d),race:d.race.bind(d),allSettled:d.allSettled.bind(d),xor:d.xor.bind(d),not:d.not.bind(d),nand:d.nand.bind(d),nor:d.nor.bind(d),xnor:d.xnor.bind(d),majority:d.majority.bind(d),allFulfilled:d.allFulfilled.bind(d),allRejected:d.allRejected.bind(d)},s={};return Object.entries(n).forEach(([e,n])=>{const i=l[e]||e;s[`${t}${i}${r}`]=n}),s}export{f 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,[...e],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};
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,i){const n={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=i.length>0?`\n失败原因:${i.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,n+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,[...e],r)}}}class i extends r{async execute(e,r={}){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),i=this.filterRejectedResults(l),n=s.length,o=l.length;if(1===n)return s[0];throw t("XOR_ERROR",n,o,l,i)}}class n extends r{async execute(e,r={}){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),i=s.length,n=l.length;if(i===n)throw t("NAND_ERROR",i,n,l,s);return s}}class o extends r{async execute(e,r={}){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),i=s.length,n=l.length;if(0===i)return[];throw t("NOR_ERROR",i,n,l,s)}}class a extends r{async execute(e,r={}){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),i=s.length,n=l.length;if(0===i||i===n)return s;throw t("XNOR_ERROR",i,n,l,s)}}class c extends r{async execute(e,r={max:.5}){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),i=s.length,n=l.length;if(i>Math.floor(n*r.max))return s;throw t("MAJORITY_ERROR",i,n,l)}}class u{constructor(e){this.promise=e}maxTimer(e){let t;return Promise.race([this.promise,new Promise((r,l)=>{t=setTimeout(()=>{l(new Error(`Promise timed out after ${e}ms`))},e)})]).finally(()=>clearTimeout(t))}then(e,t){return new u(this.promise.then(e,t))}catch(e){return new u(this.promise.catch(e))}finally(e){return new u(this.promise.finally(e))}toPromise(){return this.promise}}class d{static get gates(){return{and:new l,or:new s,xor:new i,nand:new n,nor:new o,xnor:new a,majority:new c}}static and(e){return new u(this.gates.and.execute(e))}static or(e){return new u(this.gates.or.execute(e))}static not(e){return new u(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new u(Promise.race(e).then(e=>Promise.resolve(e)))}static allSettled(e){return new u(Promise.allSettled(e))}static xor(e){return new u(this.gates.xor.execute(e))}static nand(e){return new u(this.gates.nand.execute(e))}static nor(e){return new u(this.gates.nor.execute(e))}static xnor(e){return new u(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new u(this.gates.majority.execute(e,t))}static allFulfilled(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value)))}static allRejected(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason)))}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=d,exports.PromiseLogicError=e,exports.PromiseWithTimer=u,exports.createLogicError=t,exports.createPromiseLogic=function(e={}){const{prefix:t="",suffix:r="",rename:l={}}=e,s={and:d.and.bind(d),or:d.or.bind(d),race:d.race.bind(d),allSettled:d.allSettled.bind(d),xor:d.xor.bind(d),not:d.not.bind(d),nand:d.nand.bind(d),nor:d.nor.bind(d),xnor:d.xnor.bind(d),majority:d.majority.bind(d),allFulfilled:d.allFulfilled.bind(d),allRejected:d.allRejected.bind(d)},i={};return Object.entries(s).forEach(([e,s])=>{const n=l[e]||e;i[`${t}${n}${r}`]=s}),i};
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,[...e],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};
package/dist/index.esm.js CHANGED
@@ -1 +1 @@
1
- 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,[...e],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{constructor(e){this.promise=e}maxTimer(e){let t;return Promise.race([this.promise,new Promise((r,l)=>{t=setTimeout(()=>{l(new Error(`Promise timed out after ${e}ms`))},e)})]).finally(()=>clearTimeout(t))}then(e,t){return new u(this.promise.then(e,t))}catch(e){return new u(this.promise.catch(e))}finally(e){return new u(this.promise.finally(e))}toPromise(){return this.promise}}class d{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}}static and(e){return new u(this.gates.and.execute(e))}static or(e){return new u(this.gates.or.execute(e))}static not(e){return new u(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new u(Promise.race(e).then(e=>Promise.resolve(e)))}static allSettled(e){return new u(Promise.allSettled(e))}static xor(e){return new u(this.gates.xor.execute(e))}static nand(e){return new u(this.gates.nand.execute(e))}static nor(e){return new u(this.gates.nor.execute(e))}static xnor(e){return new u(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new u(this.gates.majority.execute(e,t))}static allFulfilled(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value)))}static allRejected(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason)))}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()})}}}}function f(e={}){const{prefix:t="",suffix:r="",rename:l={}}=e,n={and:d.and.bind(d),or:d.or.bind(d),race:d.race.bind(d),allSettled:d.allSettled.bind(d),xor:d.xor.bind(d),not:d.not.bind(d),nand:d.nand.bind(d),nor:d.nor.bind(d),xnor:d.xnor.bind(d),majority:d.majority.bind(d),allFulfilled:d.allFulfilled.bind(d),allRejected:d.allRejected.bind(d)},s={};return Object.entries(n).forEach(([e,n])=>{const i=l[e]||e;s[`${t}${i}${r}`]=n}),s}export{d as PromiseLogic,e as PromiseLogicError,u as PromiseWithTimer,t as createLogicError,f 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,[...e],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 +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",a=i&&i?.length>0?`\n失败原因:${i.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,n+a,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,a=this.filterRejectedResults(r);if(i>Math.floor(n*l.max))return s;throw t("MAJORITY_ERROR",i,n,r,a)}}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 a 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 o 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{constructor(e){this.promise=e}maxTimer(e){let t;return Promise.race([this.promise,new Promise((l,r)=>{t=setTimeout(()=>{r(new Error(`Promise timed out after ${e}ms,${this.promise}`))},e)})]).finally(()=>clearTimeout(t))}then(e,t){return new u(this.promise.then(e,t))}catch(e){return new u(this.promise.catch(e))}finally(e){return new u(this.promise.finally(e))}toPromise(){return this.promise}}class d{static get gates(){return{and:new s,or:new r,xor:new c,nand:new n,nor:new a,xnor:new o,majority:new i}}static and(e){return new u(this.gates.and.execute(e))}static or(e){return new u(this.gates.or.execute(e))}static xor(e){return new u(this.gates.xor.execute(e))}static nand(e){return new u(this.gates.nand.execute(e))}static nor(e){return new u(this.gates.nor.execute(e))}static xnor(e){return new u(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new u(this.gates.majority.execute(e,t))}static allFulfilled(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value)))}static allRejected(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason)))}static not(e){return new u(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new u(Promise.race(e))}static allSettled(e){return new u(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.createPromiseLogic=function(e={}){const{prefix:t="",suffix:l="",rename:r={}}=e,s={and:d.and.bind(d),or:d.or.bind(d),not:d.not.bind(d),race:d.race.bind(d),allSettled:d.allSettled.bind(d),xor:d.xor.bind(d),nand:d.nand.bind(d),nor:d.nor.bind(d),xnor:d.xnor.bind(d),majority:d.majority.bind(d),allFulfilled:d.allFulfilled.bind(d),allRejected:d.allRejected.bind(d)},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,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 +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,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",a=i&&i?.length>0?`\n失败原因:${i.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,n+a,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,a=this.filterRejectedResults(r);if(i>Math.floor(n*l.max))return s;throw t("MAJORITY_ERROR",i,n,r,a)}}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 a 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 o 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{constructor(e){this.promise=e}maxTimer(e){let t;return Promise.race([this.promise,new Promise((l,r)=>{t=setTimeout(()=>{r(new Error(`Promise timed out after ${e}ms,${this.promise}`))},e)})]).finally(()=>clearTimeout(t))}then(e,t){return new u(this.promise.then(e,t))}catch(e){return new u(this.promise.catch(e))}finally(e){return new u(this.promise.finally(e))}toPromise(){return this.promise}}class d{static get gates(){return{and:new s,or:new r,xor:new c,nand:new n,nor:new a,xnor:new o,majority:new i}}static and(e){return new u(this.gates.and.execute(e))}static or(e){return new u(this.gates.or.execute(e))}static xor(e){return new u(this.gates.xor.execute(e))}static nand(e){return new u(this.gates.nand.execute(e))}static nor(e){return new u(this.gates.nor.execute(e))}static xnor(e){return new u(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new u(this.gates.majority.execute(e,t))}static allFulfilled(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value)))}static allRejected(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason)))}static not(e){return new u(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new u(Promise.race(e))}static allSettled(e){return new u(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()})}}}function R(e={}){const{prefix:t="",suffix:l="",rename:r={}}=e,s={and:d.and.bind(d),or:d.or.bind(d),not:d.not.bind(d),race:d.race.bind(d),allSettled:d.allSettled.bind(d),xor:d.xor.bind(d),nand:d.nand.bind(d),nor:d.nor.bind(d),xnor:d.xnor.bind(d),majority:d.majority.bind(d),allFulfilled:d.allFulfilled.bind(d),allRejected:d.allRejected.bind(d)},i={};return Object.entries(s).forEach(([e,s])=>{const n=r[e]||e;i[`${t}${n}${l}`]=s}),i}export{R 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;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 +1 @@
1
- "use strict";class e extends Error{constructor(e,t,r){super(t),this.type=e,this.results=r,this.name="PromiseLogicError"}}function t(t,r,l,s,i){const n={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).`,NOR_ERROR:`NOR condition failed: ${r} promises fulfilled (expected all rejected).`,XNOR_ERROR:`XNOR condition failed: ${r}/${l} promises fulfilled (expected all or none).`,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]||"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 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}}class l extends r{async execute(e){try{return Promise.any(e)}catch(e){throw t("OR_ERROR",0,0,[],[e])}}}class s extends r{async execute(e){try{return Promise.all(e)}catch(e){throw t("AND_ERROR",0,0,[],[e])}}}class i extends r{async execute(e,r){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),i=s.length,n=l.length,o=this.filterRejectedResults(l);if(i>Math.floor(n*r.max))return s;throw t("MAJORITY_ERROR",i,n,l,o)}}class n extends r{async execute(e){const r=await Promise.allSettled(e),l=this.filterFulfilledResults(r),s=l.length,i=r.length,n=this.filterRejectedResults(r);if(s===i)throw t("NAND_ERROR",s,i,r,n);return l}}class o extends r{async execute(e){const r=await Promise.allSettled(e),l=this.filterFulfilledResults(r).length,s=r.length,i=this.filterRejectedResults(r);if(0===l)return[];throw t("NOR_ERROR",l,s,r,i)}}class a extends r{async execute(e){const r=await Promise.allSettled(e),l=this.filterFulfilledResults(r),s=l.length,i=r.length,n=this.filterRejectedResults(r);if(0===s||s===i)return l;throw t("XNOR_ERROR",s,i,r,n)}}class c extends r{async execute(e){const r=await Promise.allSettled(e),l=this.filterFulfilledResults(r),s=l.length,i=r.length,n=this.filterRejectedResults(r);if(1===s)return l[0];throw t("XOR_ERROR",s,i,r,n)}}class u{constructor(e){this.promise=e}maxTimer(e){let t;return Promise.race([this.promise,new Promise((r,l)=>{t=setTimeout(()=>{l(new Error(`Promise timed out after ${e}ms,${this.promise}`))},e)})]).finally(()=>clearTimeout(t))}then(e,t){return new u(this.promise.then(e,t))}catch(e){return new u(this.promise.catch(e))}finally(e){return new u(this.promise.finally(e))}toPromise(){return this.promise}}class d{static get gates(){return{and:new s,or:new l,xor:new c,nand:new n,nor:new o,xnor:new a,majority:new i}}static and(e){return new u(this.gates.and.execute(e))}static or(e){return new u(this.gates.or.execute(e))}static xor(e){return new u(this.gates.xor.execute(e))}static nand(e){return new u(this.gates.nand.execute(e))}static nor(e){return new u(this.gates.nor.execute(e))}static xnor(e){return new u(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new u(this.gates.majority.execute(e,t))}static allFulfilled(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value)))}static allRejected(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason)))}static not(e){return new u(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new u(Promise.race(e))}static allSettled(e){return new u(Promise.allSettled(e))}static createFlipFlop(e=!1){let t=e,r=null,l=Promise.resolve(t);const s=()=>(r||(l=new Promise(e=>{r=e})),l);return{getState:()=>t,set:async e=>(t=e,r&&(r(t),r=null),l=Promise.resolve(t),t),async toggle(){return this.set(!t)},waitForChange:s,waitFor:e=>t===e?Promise.resolve(t):new Promise(r=>{const l=()=>{t===e?r(t):s().then(l)};l()})}}}exports.PromiseLogic=d,exports.PromiseLogicError=e,exports.PromiseWithTimer=u,exports.createPromiseLogic=function(e={}){const{prefix:t="",suffix:r="",rename:l={}}=e,s={and:d.and.bind(d),or:d.or.bind(d),not:d.not.bind(d),race:d.race.bind(d),allSettled:d.allSettled.bind(d),xor:d.xor.bind(d),nand:d.nand.bind(d),nor:d.nor.bind(d),xnor:d.xnor.bind(d),majority:d.majority.bind(d),allFulfilled:d.allFulfilled.bind(d),allRejected:d.allRejected.bind(d)},i={};return Object.entries(s).forEach(([e,s])=>{const n=l[e]||e;i[`${t}${n}${r}`]=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;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 +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,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",a=i&&i?.length>0?`\n失败原因:${i.map((e,t)=>`[${t+1}] ${e}`).join("\n")}`:"";return new e(t,n+a,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,a=this.filterRejectedResults(r);if(i>Math.floor(n*l.max))return s;throw t("MAJORITY_ERROR",i,n,r,a)}}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 a 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 o 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{constructor(e){this.promise=e}maxTimer(e){let t;return Promise.race([this.promise,new Promise((l,r)=>{t=setTimeout(()=>{r(new Error(`Promise timed out after ${e}ms,${this.promise}`))},e)})]).finally(()=>clearTimeout(t))}then(e,t){return new u(this.promise.then(e,t))}catch(e){return new u(this.promise.catch(e))}finally(e){return new u(this.promise.finally(e))}toPromise(){return this.promise}}class d{static get gates(){return{and:new s,or:new r,xor:new c,nand:new n,nor:new a,xnor:new o,majority:new i}}static and(e){return new u(this.gates.and.execute(e))}static or(e){return new u(this.gates.or.execute(e))}static xor(e){return new u(this.gates.xor.execute(e))}static nand(e){return new u(this.gates.nand.execute(e))}static nor(e){return new u(this.gates.nor.execute(e))}static xnor(e){return new u(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new u(this.gates.majority.execute(e,t))}static allFulfilled(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value)))}static allRejected(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason)))}static not(e){return new u(Promise.resolve(e).then(e=>Promise.reject(new Error(`NOT: ${e}`)),e=>Promise.resolve(e)))}static race(e){return new u(Promise.race(e))}static allSettled(e){return new u(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()})}}}function R(e={}){const{prefix:t="",suffix:l="",rename:r={}}=e,s={and:d.and.bind(d),or:d.or.bind(d),not:d.not.bind(d),race:d.race.bind(d),allSettled:d.allSettled.bind(d),xor:d.xor.bind(d),nand:d.nand.bind(d),nor:d.nor.bind(d),xnor:d.xnor.bind(d),majority:d.majority.bind(d),allFulfilled:d.allFulfilled.bind(d),allRejected:d.allRejected.bind(d)},i={};return Object.entries(s).forEach(([e,s])=>{const n=r[e]||e;i[`${t}${n}${l}`]=s}),i}export{d as PromiseLogic,e as PromiseLogicError,u as PromiseWithTimer,R 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;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};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "promise-logic",
3
- "version": "2.7.0",
3
+ "version": "2.8.1",
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",