promise-logic 2.6.5 → 2.7.0
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 +136 -80
- package/dist/factory.cjs.js +1 -1
- package/dist/factory.esm.js +1 -1
- package/dist/index.cjs.js +1 -1
- package/dist/index.esm.js +1 -1
- package/dist/v2/factory.cjs.js +1 -1
- package/dist/v2/factory.esm.js +1 -1
- package/dist/v2/index.cjs.js +1 -1
- package/dist/v2/index.esm.js +1 -1
- package/package.json +1 -6
package/README.md
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
1
|
### **1. Core Philosophy**
|
|
5
2
|
|
|
6
3
|
**Replace API Memory with Logical Concepts**
|
|
7
|
-
The design philosophy of `promise-logic` is: **Developers should focus on business logic, not the details of Promise APIs**.
|
|
4
|
+
The design philosophy of `promise-logic` is: **Developers should focus on business logic, not on the details of Promise APIs**.
|
|
8
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.
|
|
9
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.
|
|
10
7
|
|
|
@@ -12,29 +9,32 @@ Traditional Promise combinations (such as `Promise.all`, `Promise.race`) have na
|
|
|
12
9
|
|
|
13
10
|
### **2. Features**
|
|
14
11
|
|
|
15
|
-
1. **Logical Semantics**
|
|
16
|
-
- `and`: All tasks must succeed (equivalent to `Promise.all`)
|
|
17
|
-
- `or`: At least one task succeeds (equivalent to `Promise.
|
|
18
|
-
- `xor`: **Exactly one task succeeds**
|
|
19
|
-
- `nand`:
|
|
20
|
-
|
|
21
|
-
- `
|
|
22
|
-
- `
|
|
23
|
-
|
|
24
|
-
|
|
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`)
|
|
15
|
+
- `xor`: **Exactly one task succeeds**
|
|
16
|
+
- `nand`: Not all tasks succeed (at least one fails)
|
|
17
|
+
- `nor`: All tasks fail (no task succeeds)
|
|
18
|
+
- `xnor`: All tasks succeed or all fail (same state)
|
|
19
|
+
- `not`: Inverts the result of a single Promise
|
|
20
|
+
- `majority`: Most tasks succeed
|
|
21
|
+
|
|
22
|
+
2. **Zero Dependencies**
|
|
25
23
|
Only depends on native Promise, no additional runtime dependencies.
|
|
26
24
|
|
|
27
|
-
3. **Full Test Coverage**
|
|
25
|
+
3. **Full Test Coverage**
|
|
28
26
|
All logic gates have undergone rigorous unit testing to ensure behavior meets expectations.
|
|
29
27
|
|
|
30
|
-
4. **Clear Error Classification**
|
|
31
|
-
- `PromiseLogicError` unified error type
|
|
28
|
+
4. **Clear Error Classification**
|
|
29
|
+
- `PromiseLogicError` unified error type
|
|
32
30
|
- `error.type` distinguishes specific logical errors (e.g., `'XOR_ERROR'`)
|
|
33
31
|
|
|
34
|
-
5. **Timeout Control**
|
|
35
|
-
- `maxTimer`: Adds timeout functionality to any Promise operation
|
|
32
|
+
5. **Timeout Control**
|
|
33
|
+
- `maxTimer`: Adds timeout functionality to any Promise operation (unit: milliseconds).
|
|
34
|
+
|
|
35
|
+
maxTimer can only detect timeout of Promise operations, cannot interrupt or cancel Promise operations themselves, this is a JavaScript characteristic.
|
|
36
36
|
|
|
37
|
-
6. **Extended Operations**
|
|
37
|
+
6. **Extended Operations**
|
|
38
38
|
- `allFulfilled`: Returns all successful results
|
|
39
39
|
- `allRejected`: Returns all failed results
|
|
40
40
|
- `allSettled`: Returns all results (both successful and failed)
|
|
@@ -52,6 +52,7 @@ npm install promise-logic
|
|
|
52
52
|
### **4. Quick Start**
|
|
53
53
|
|
|
54
54
|
#### Example: Primary/Backup Service Call (XOR Scenario)
|
|
55
|
+
|
|
55
56
|
```javascript
|
|
56
57
|
import { PromiseLogic } from 'promise-logic';
|
|
57
58
|
|
|
@@ -62,10 +63,10 @@ const backup = fetch('https://api.backup.com/data');
|
|
|
62
63
|
|
|
63
64
|
// Execute XOR logic: exactly one success
|
|
64
65
|
PromiseLogic.xor([primary, backup])
|
|
65
|
-
.then(result => {
|
|
66
|
+
.then((result) => {
|
|
66
67
|
console.log('Successfully fetched data:', result);
|
|
67
68
|
})
|
|
68
|
-
.catch(error => {
|
|
69
|
+
.catch((error) => {
|
|
69
70
|
if (error.type === 'XOR_ERROR') {
|
|
70
71
|
console.error('Both primary and backup services succeeded or failed, which does not meet XOR semantics');
|
|
71
72
|
} else {
|
|
@@ -75,6 +76,7 @@ PromiseLogic.xor([primary, backup])
|
|
|
75
76
|
```
|
|
76
77
|
|
|
77
78
|
#### Example: Majority Decision (Majority Scenario)
|
|
79
|
+
|
|
78
80
|
```javascript
|
|
79
81
|
import { PromiseLogic } from 'promise-logic';
|
|
80
82
|
|
|
@@ -85,10 +87,10 @@ const services = [
|
|
|
85
87
|
];
|
|
86
88
|
|
|
87
89
|
PromiseLogic.majority(services)
|
|
88
|
-
.then(results => {
|
|
90
|
+
.then((results) => {
|
|
89
91
|
console.log('Majority of services returned success:', results);
|
|
90
92
|
})
|
|
91
|
-
.catch(error => {
|
|
93
|
+
.catch((error) => {
|
|
92
94
|
console.error('Majority of services failed:', error);
|
|
93
95
|
});
|
|
94
96
|
```
|
|
@@ -102,35 +104,38 @@ const services = [
|
|
|
102
104
|
fetch('https://api.node3.com/vote')
|
|
103
105
|
];
|
|
104
106
|
|
|
107
|
+
// Type assertion can be done, or let PromiseLogic infer types automatically
|
|
105
108
|
PromiseLogic.majority<Response>(services)
|
|
106
|
-
.then(results => {
|
|
109
|
+
.then((results) => {
|
|
107
110
|
console.log('Majority of services returned success:', results);
|
|
108
111
|
})
|
|
109
|
-
.catch(error => {
|
|
112
|
+
.catch((error) => {
|
|
110
113
|
console.error('Majority of services failed:', error);
|
|
111
114
|
});
|
|
112
115
|
```
|
|
113
116
|
|
|
114
117
|
#### Example: Timeout Control
|
|
118
|
+
|
|
115
119
|
```javascript
|
|
116
120
|
import { PromiseLogic } from 'promise-logic';
|
|
117
121
|
|
|
118
122
|
// Execute operation with timeout
|
|
119
123
|
PromiseLogic.and([
|
|
120
124
|
Promise.resolve(1),
|
|
121
|
-
new Promise(resolve => setTimeout(resolve, 1000)), // 1 second operation
|
|
125
|
+
new Promise((resolve) => setTimeout(resolve, 1000)), // 1 second operation
|
|
122
126
|
Promise.resolve(3)
|
|
123
127
|
])
|
|
124
|
-
.maxTimer(2000) // 2 second timeout
|
|
125
|
-
.then(result => {
|
|
126
|
-
|
|
127
|
-
})
|
|
128
|
-
.catch(error => {
|
|
129
|
-
|
|
130
|
-
});
|
|
128
|
+
.maxTimer(2000) // 2 second timeout
|
|
129
|
+
.then((result) => {
|
|
130
|
+
console.log('Operation completed within timeout:', result);
|
|
131
|
+
})
|
|
132
|
+
.catch((error) => {
|
|
133
|
+
console.error('Operation timed out or failed:', error.message);
|
|
134
|
+
});
|
|
131
135
|
```
|
|
132
136
|
|
|
133
137
|
#### Example: Extended Operations
|
|
138
|
+
|
|
134
139
|
```javascript
|
|
135
140
|
import { PromiseLogic } from 'promise-logic';
|
|
136
141
|
|
|
@@ -142,25 +147,23 @@ const operations = [
|
|
|
142
147
|
];
|
|
143
148
|
|
|
144
149
|
// Get all successful results
|
|
145
|
-
PromiseLogic.allFulfilled(operations)
|
|
146
|
-
.
|
|
147
|
-
|
|
148
|
-
});
|
|
150
|
+
PromiseLogic.allFulfilled(operations).then((results) => {
|
|
151
|
+
console.log('Successful results:', results); // ['success1', 'success2']
|
|
152
|
+
});
|
|
149
153
|
|
|
150
154
|
// Get all failed results
|
|
151
|
-
PromiseLogic.allRejected(operations)
|
|
152
|
-
.
|
|
153
|
-
|
|
154
|
-
});
|
|
155
|
+
PromiseLogic.allRejected(operations).then((errors) => {
|
|
156
|
+
console.log('Failed results:', errors); // ['error1', 'error2']
|
|
157
|
+
});
|
|
155
158
|
|
|
156
159
|
// Get all results (both success and failure)
|
|
157
|
-
PromiseLogic.allSettled(operations)
|
|
158
|
-
.
|
|
159
|
-
|
|
160
|
-
});
|
|
160
|
+
PromiseLogic.allSettled(operations).then((results) => {
|
|
161
|
+
console.log('All results:', results);
|
|
162
|
+
});
|
|
161
163
|
```
|
|
162
164
|
|
|
163
165
|
#### Example: Custom majority threshold
|
|
166
|
+
|
|
164
167
|
```javascript
|
|
165
168
|
import { PromiseLogic } from 'promise-logic';
|
|
166
169
|
|
|
@@ -174,68 +177,121 @@ const services = [
|
|
|
174
177
|
// Default threshold (0.5): requires at least 3 successes
|
|
175
178
|
// Custom threshold (0.4): requires at least 2 successes
|
|
176
179
|
PromiseLogic.majority(services, { max: 0.4 })
|
|
177
|
-
.then(results => {
|
|
180
|
+
.then((results) => {
|
|
178
181
|
console.log('Custom threshold met, successful results:', results); // ['service1', 'service2']
|
|
179
182
|
})
|
|
180
|
-
.catch(error => {
|
|
183
|
+
.catch((error) => {
|
|
181
184
|
console.error('Custom threshold not met:', error);
|
|
182
185
|
});
|
|
183
186
|
```
|
|
184
187
|
|
|
185
|
-
|
|
188
|
+
## Recent Updates
|
|
186
189
|
|
|
187
|
-
###
|
|
190
|
+
### v2.7.0
|
|
191
|
+
|
|
192
|
+
- **Added Modular Architecture**: Separated logic gate implementations into independent modules for better code maintainability
|
|
193
|
+
- **Fixed NOT Logic Gate**: Fixed potential risks in NOT logic gate in production environments
|
|
194
|
+
- **Improved Error Messages**: Enhanced error message format for clearer error details
|
|
195
|
+
- **Enhanced Test Coverage**: Added complete factory function tests for v1 and v2 versions
|
|
196
|
+
- **Updated Documentation**: Added custom factory function usage guide
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
### Using Factory Function
|
|
200
|
+
|
|
201
|
+
The factory function allows you to create PromiseLogic methods with custom names:
|
|
202
|
+
|
|
203
|
+
```javascript
|
|
204
|
+
import { createPromiseLogic } from 'promise-logic/factory';
|
|
205
|
+
|
|
206
|
+
// Create instance with custom naming
|
|
207
|
+
const logic = createPromiseLogic({
|
|
208
|
+
prefix: 'api_',
|
|
209
|
+
suffix: '_call',
|
|
210
|
+
rename: {
|
|
211
|
+
and: 'all',
|
|
212
|
+
or: 'any',
|
|
213
|
+
xor: 'exclusive'
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// Use custom-named methods
|
|
218
|
+
logic.api_all_call([fetch('/api/users'), fetch('/api/posts')]);
|
|
219
|
+
|
|
220
|
+
logic.api_any_call([fetch('/api/cache'), fetch('/api/database')]);
|
|
221
|
+
```
|
|
188
222
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
223
|
+
### TypeScript Support
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
import { PromiseLogic } from 'promise-logic/typescript';
|
|
227
|
+
|
|
228
|
+
// Type inference
|
|
229
|
+
PromiseLogic.and([Promise.resolve(1), Promise.resolve(2)]).then(
|
|
230
|
+
(results: number[]) => {
|
|
231
|
+
console.log(results);
|
|
232
|
+
}
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
// Type assertion
|
|
236
|
+
PromiseLogic.and<number>([Promise.resolve(1), Promise.resolve(2)]);
|
|
237
|
+
```
|
|
201
238
|
|
|
202
239
|
---
|
|
203
240
|
|
|
241
|
+
### **5. API Reference**
|
|
242
|
+
|
|
243
|
+
| API | Description |
|
|
244
|
+
| :------------- | :--------------------------------------------------------------------------------------------------------------------------- |
|
|
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. |
|
|
247
|
+
| `xor` | **Exactly one Promise succeeds**, returns that result; otherwise throws `XOR_ERROR`. |
|
|
248
|
+
| `nand` | Not all Promises succeed (at least one fails), returns success result array; all succeed causes overall failure. |
|
|
249
|
+
| `nor` | All Promises fail (no task succeeds), returns empty array; any success causes overall failure. |
|
|
250
|
+
| `xnor` | All Promises succeed or all fail (same state), returns success result array; otherwise throws `XNOR_ERROR`. |
|
|
251
|
+
| `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). |
|
|
258
|
+
|
|
259
|
+
## **Note**: `maxTimer` can only detect timeout of Promise operations, cannot interrupt or cancel Promise operations themselves, this is a JavaScript characteristic.
|
|
260
|
+
|
|
204
261
|
### **6. Real-world Application Scenarios**
|
|
205
262
|
|
|
206
|
-
1. **Primary/Backup Service Calls**
|
|
207
|
-
- Use `xor` to ensure **exactly one service responds**, avoiding duplicate processing.
|
|
208
|
-
2. **Distributed Decision Making**
|
|
209
|
-
- Use `majority` to implement majority consensus (e.g., distributed voting).
|
|
210
|
-
3. **Resource Competition**
|
|
211
|
-
- Use `or` to get the first available resource (e.g., CDN node selection).
|
|
212
|
-
- Use `not` to check if a resource is available.
|
|
213
|
-
4. **Full-link Validation**
|
|
214
|
-
- Use `and` to ensure all dependent services succeed (e.g., order creation).
|
|
263
|
+
1. **Primary/Backup Service Calls**
|
|
264
|
+
- Use `xor` to ensure **exactly one service responds**, avoiding duplicate processing.
|
|
265
|
+
2. **Distributed Decision Making**
|
|
266
|
+
- Use `majority` to implement majority consensus (e.g., distributed voting).
|
|
267
|
+
3. **Resource Competition**
|
|
268
|
+
- Use `or` to get the first available resource (e.g., CDN node selection).
|
|
269
|
+
- Use `not` to check if a resource is available.
|
|
270
|
+
4. **Full-link Validation**
|
|
271
|
+
- Use `and` to ensure all dependent services succeed (e.g., order creation).
|
|
215
272
|
|
|
216
273
|
---
|
|
217
274
|
|
|
218
275
|
### **7. Contribution Guide**
|
|
219
276
|
|
|
220
|
-
1. **Development Environment**
|
|
277
|
+
1. **Development Environment**
|
|
221
278
|
```bash
|
|
222
279
|
git clone https://github.com/haowhite/promise-logic.git
|
|
223
280
|
cd promise-logic
|
|
224
281
|
npm install
|
|
225
282
|
```
|
|
226
|
-
2. **Testing**
|
|
283
|
+
2. **Testing**
|
|
227
284
|
```bash
|
|
228
285
|
npm test
|
|
229
286
|
```
|
|
230
|
-
3. **Commit Guidelines**
|
|
231
|
-
- Commit messages must include prefixes like `feat:` (new feature), `fix:` (bug fix), `docs:` (documentation).
|
|
232
|
-
- Pull Requests must include test cases.
|
|
287
|
+
3. **Commit Guidelines**
|
|
288
|
+
- Commit messages must include prefixes like `feat:` (new feature), `fix:` (bug fix), `docs:` (documentation).
|
|
289
|
+
- Pull Requests must include test cases.
|
|
233
290
|
|
|
234
291
|
---
|
|
235
292
|
|
|
236
293
|
### **8. Resource Links**
|
|
237
294
|
|
|
238
|
-
- **GitHub Repository
|
|
239
|
-
- **npm Package
|
|
240
|
-
- **Issue Tracking
|
|
241
|
-
|
|
295
|
+
- **GitHub Repository**: [https://github.com/xier123456/promise-logic](https://github.com/xier123456/promise-logic)
|
|
296
|
+
- **npm Package**: [https://www.npmjs.com/package/promise-logic](https://www.npmjs.com/package/promise-logic)
|
|
297
|
+
- **Issue Tracking**: [GitHub Issues](https://github.com/xier123456/promise-logic/issues)
|
package/dist/factory.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,
|
|
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};
|
package/dist/factory.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,
|
|
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};
|
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,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,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};
|
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,
|
|
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};
|
package/dist/v2/factory.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";class e
|
|
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};
|
package/dist/v2/factory.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class e
|
|
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};
|
package/dist/v2/index.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";class e
|
|
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};
|
package/dist/v2/index.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class e
|
|
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};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "promise-logic",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.7.0",
|
|
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",
|
|
@@ -12,11 +12,6 @@
|
|
|
12
12
|
"require": "./dist/index.cjs.js",
|
|
13
13
|
"types": "./dist/types/index.d.ts"
|
|
14
14
|
},
|
|
15
|
-
"./factory": {
|
|
16
|
-
"import": "./dist/factory.esm.js",
|
|
17
|
-
"require": "./dist/factory.cjs.js",
|
|
18
|
-
"types": "./dist/types/factory.d.ts"
|
|
19
|
-
},
|
|
20
15
|
"./typescript": {
|
|
21
16
|
"import": "./dist/v2/index.esm.js",
|
|
22
17
|
"require": "./dist/v2/index.cjs.js",
|