promise-logic 1.0.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 +194 -0
- package/dist/factory.cjs.js +208 -0
- package/dist/factory.cjs.js.map +1 -0
- package/dist/factory.esm.js +206 -0
- package/dist/factory.esm.js.map +1 -0
- package/dist/index.cjs.js +211 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.esm.js +206 -0
- package/dist/index.esm.js.map +1 -0
- package/package.json +85 -0
package/README.md
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# PromiseLogic
|
|
2
|
+
|
|
3
|
+
> Compose promises with logic gate semantics (AND, OR, XOR, NAND, NOR, XNOR, Majority)
|
|
4
|
+
|
|
5
|
+
PromiseLogic is a utility library that provides logical gate semantics for Promise composition, allowing developers to work with asynchronous operations using familiar logic concepts.
|
|
6
|
+
|
|
7
|
+
## Philosophy
|
|
8
|
+
|
|
9
|
+
**"Forget APIs, remember logic."** - Replace fragmented API knowledge with fundamental logic concepts.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- 🧠 **Logic Gate Semantics**: AND, OR, XOR, NAND, NOR, XNOR, Majority
|
|
14
|
+
- 🔧 **Dual API Pattern**: Static class methods + Factory function
|
|
15
|
+
- 📦 **Production Ready**: Complete build configuration and type support
|
|
16
|
+
- 🧪 **Test Driven**: Comprehensive test coverage
|
|
17
|
+
- 🌳 **Tree Shaking**: Optimized bundle output
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install promise-logic
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
### Basic Usage
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
import { PromiseLogic } from 'promise-logic';
|
|
31
|
+
|
|
32
|
+
// AND logic - All promises must succeed
|
|
33
|
+
const results = await PromiseLogic.and([
|
|
34
|
+
fetch('/api/users'),
|
|
35
|
+
fetch('/api/posts'),
|
|
36
|
+
fetch('/api/comments')
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
// OR logic - Any promise succeeds
|
|
40
|
+
const data = await PromiseLogic.or([
|
|
41
|
+
fetchPrimaryService(),
|
|
42
|
+
fetchBackupService()
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
// RACE logic - First settled promise wins
|
|
46
|
+
const winner = await PromiseLogic.race([
|
|
47
|
+
fastApiCall(),
|
|
48
|
+
slowApiCall()
|
|
49
|
+
]);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Factory Function Usage
|
|
53
|
+
|
|
54
|
+
```javascript
|
|
55
|
+
import { createPromiseLogic } from 'promise-logic';
|
|
56
|
+
|
|
57
|
+
// Custom naming
|
|
58
|
+
const myLogic = createPromiseLogic({
|
|
59
|
+
prefix: 'async',
|
|
60
|
+
suffix: 'Logic',
|
|
61
|
+
rename: { and: 'conjunction' }
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Use custom named methods
|
|
65
|
+
const results = await myLogic.conjunction([
|
|
66
|
+
task1(), task2(), task3()
|
|
67
|
+
]);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Advanced Logic Methods
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
// XOR - Exactly one promise must succeed
|
|
74
|
+
const result = await PromiseLogic.xor([
|
|
75
|
+
primaryService(),
|
|
76
|
+
backupService()
|
|
77
|
+
]);
|
|
78
|
+
|
|
79
|
+
// NAND - Not all promises must succeed
|
|
80
|
+
const data = await PromiseLogic.nand([
|
|
81
|
+
cacheLookup(),
|
|
82
|
+
databaseQuery()
|
|
83
|
+
]);
|
|
84
|
+
|
|
85
|
+
// Majority - More than half must succeed
|
|
86
|
+
const decisions = await PromiseLogic.majority([
|
|
87
|
+
validator1(), validator2(), validator3()
|
|
88
|
+
]);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## API Reference
|
|
92
|
+
|
|
93
|
+
### PromiseLogic Class
|
|
94
|
+
|
|
95
|
+
#### Core Methods
|
|
96
|
+
|
|
97
|
+
- `PromiseLogic.and(iterable)` - Resolves when all promises succeed (proxies Promise.all)
|
|
98
|
+
- `PromiseLogic.or(iterable)` - Resolves when any promise succeeds (proxies Promise.any)
|
|
99
|
+
- `PromiseLogic.race(iterable)` - Resolves with first settled promise (proxies Promise.race)
|
|
100
|
+
- `PromiseLogic.allSettled(iterable)` - Resolves when all promises settle (proxies Promise.allSettled)
|
|
101
|
+
|
|
102
|
+
#### Advanced Logic Methods
|
|
103
|
+
|
|
104
|
+
- `PromiseLogic.xor(iterable)` - **XOR**: Resolves when exactly one promise succeeds
|
|
105
|
+
- `PromiseLogic.nand(iterable)` - **NAND**: Resolves when not all promises succeed
|
|
106
|
+
- `PromiseLogic.nor(iterable)` - **NOR**: Resolves when all promises reject
|
|
107
|
+
- `PromiseLogic.xnor(iterable)` - **XNOR**: Resolves when all succeed or all reject
|
|
108
|
+
- `PromiseLogic.majority(iterable)` - **Majority**: Resolves when more than half succeed
|
|
109
|
+
- `PromiseLogic.createFlipFlop(initialState)` - Creates an asynchronous flip-flop instance
|
|
110
|
+
|
|
111
|
+
### createPromiseLogic Factory Function
|
|
112
|
+
|
|
113
|
+
```javascript
|
|
114
|
+
createPromiseLogic(options?: {
|
|
115
|
+
prefix?: string;
|
|
116
|
+
suffix?: string;
|
|
117
|
+
rename?: Record<string, string>;
|
|
118
|
+
}): Record<string, Function>
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Development
|
|
122
|
+
|
|
123
|
+
### Project Structure
|
|
124
|
+
|
|
125
|
+
```
|
|
126
|
+
promise-logic/
|
|
127
|
+
├── src/
|
|
128
|
+
│ ├── index.js # Main entry point
|
|
129
|
+
│ ├── PromiseLogic.js # Core class implementation
|
|
130
|
+
│ ├── factory.js # Factory function
|
|
131
|
+
│ ├── utils/
|
|
132
|
+
│ │ └── errors.js # Error handling utilities
|
|
133
|
+
│ └── types/
|
|
134
|
+
│ └── index.d.ts # TypeScript definitions
|
|
135
|
+
├── test/ # Test files
|
|
136
|
+
├── examples/ # Usage examples
|
|
137
|
+
└── dist/ # Build output
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Development Commands
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
# Install dependencies
|
|
144
|
+
npm install
|
|
145
|
+
|
|
146
|
+
# Build the project
|
|
147
|
+
npm run build
|
|
148
|
+
|
|
149
|
+
# Run tests
|
|
150
|
+
npm test
|
|
151
|
+
|
|
152
|
+
# Watch mode build
|
|
153
|
+
npm run dev
|
|
154
|
+
|
|
155
|
+
# Code linting
|
|
156
|
+
npm run lint
|
|
157
|
+
|
|
158
|
+
# Code formatting
|
|
159
|
+
npm run format
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Examples
|
|
163
|
+
|
|
164
|
+
See the `examples/` directory for complete usage examples:
|
|
165
|
+
|
|
166
|
+
- `basic-usage.js` - Basic usage examples
|
|
167
|
+
- `advanced-patterns.js` - Advanced patterns and use cases
|
|
168
|
+
|
|
169
|
+
## Error Handling
|
|
170
|
+
|
|
171
|
+
PromiseLogic provides detailed error information including:
|
|
172
|
+
|
|
173
|
+
```javascript
|
|
174
|
+
try {
|
|
175
|
+
await PromiseLogic.xor([promise1, promise2]);
|
|
176
|
+
} catch (error) {
|
|
177
|
+
if (error.name === 'PromiseLogicError') {
|
|
178
|
+
console.log('Error type:', error.type); // 'XOR_ERROR'
|
|
179
|
+
console.log('All results:', error.results);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Error Types
|
|
185
|
+
|
|
186
|
+
- `XOR_ERROR` - XOR condition failed (not exactly one promise fulfilled)
|
|
187
|
+
- `NAND_ERROR` - NAND condition failed (all promises fulfilled)
|
|
188
|
+
- `NOR_ERROR` - NOR condition failed (some promises fulfilled)
|
|
189
|
+
- `XNOR_ERROR` - XNOR condition failed (mixed results)
|
|
190
|
+
- `MAJORITY_ERROR` - Majority condition failed (not enough promises fulfilled)
|
|
191
|
+
|
|
192
|
+
## License
|
|
193
|
+
|
|
194
|
+
MIT
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class PromiseLogicError extends Error {
|
|
4
|
+
constructor(type, message, results) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = 'PromiseLogicError';
|
|
7
|
+
this.type = type; // 'XOR_ERROR', 'NAND_ERROR', etc.
|
|
8
|
+
this.results = results; // settlement results of all Promises
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Error factory function
|
|
13
|
+
function createLogicError(type, fulfilledCount, total, results) {
|
|
14
|
+
const messages = {
|
|
15
|
+
XOR_ERROR: `XOR condition failed: expected exactly 1 promise to fulfill, but ${fulfilledCount} fulfilled.`,
|
|
16
|
+
NAND_ERROR: `NAND condition failed: all ${total} promises fulfilled (expected at least one rejection).`,
|
|
17
|
+
NOR_ERROR: `NOR condition failed: ${fulfilledCount} promises fulfilled (expected all rejected).`,
|
|
18
|
+
MAJORITY_ERROR: `Majority condition failed: ${fulfilledCount}/${total} fulfilled (need majority).`
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
return new PromiseLogicError(type, messages[type] || 'Logic condition failed', results);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
class PromiseLogic {
|
|
25
|
+
static and(iterable) {
|
|
26
|
+
return Promise.all(iterable);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static or(iterable) {
|
|
30
|
+
return Promise.any(iterable);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static race(iterable) {
|
|
34
|
+
return Promise.race(iterable);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static allSettled(iterable) {
|
|
38
|
+
return Promise.allSettled(iterable);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static xor(iterable) {
|
|
42
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
43
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
44
|
+
const fulfilledCount = fulfilled.length;
|
|
45
|
+
const total = results.length;
|
|
46
|
+
|
|
47
|
+
if (fulfilledCount === 1) {
|
|
48
|
+
// 恰好一个成功,返回成功的值
|
|
49
|
+
return fulfilled[0].value;
|
|
50
|
+
} else {
|
|
51
|
+
// 0个或多个(>1)成功,抛出XOR_ERROR
|
|
52
|
+
throw createLogicError('XOR_ERROR', fulfilledCount, total, results);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static nand(iterable) {
|
|
58
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
59
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
60
|
+
const fulfilledCount = fulfilled.length;
|
|
61
|
+
const total = results.length;
|
|
62
|
+
|
|
63
|
+
if (fulfilledCount === total) {
|
|
64
|
+
// 全部成功,抛出NAND_ERROR
|
|
65
|
+
throw createLogicError('NAND_ERROR', fulfilledCount, total, results);
|
|
66
|
+
} else {
|
|
67
|
+
// 不是所有都成功,返回成功的值数组
|
|
68
|
+
return fulfilled.map(result => result.value);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static nor(iterable) {
|
|
74
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
75
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
76
|
+
const fulfilledCount = fulfilled.length;
|
|
77
|
+
const total = results.length;
|
|
78
|
+
|
|
79
|
+
if (fulfilledCount === 0) {
|
|
80
|
+
// 全部失败,返回空数组表示成功
|
|
81
|
+
return [];
|
|
82
|
+
} else {
|
|
83
|
+
// 任意成功,抛出NOR_ERROR
|
|
84
|
+
throw createLogicError('NOR_ERROR', fulfilledCount, total, results);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
static xnor(iterable) {
|
|
90
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
91
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
92
|
+
const fulfilledCount = fulfilled.length;
|
|
93
|
+
const total = results.length;
|
|
94
|
+
|
|
95
|
+
if (fulfilledCount === 0 || fulfilledCount === total) {
|
|
96
|
+
// 全部成功或全部失败,返回成功的值数组
|
|
97
|
+
return fulfilled.map(result => result.value);
|
|
98
|
+
} else {
|
|
99
|
+
// 部分成功部分失败,抛出XNOR_ERROR
|
|
100
|
+
throw createLogicError('XNOR_ERROR', fulfilledCount, total, results);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
static majority(iterable) {
|
|
106
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
107
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
108
|
+
const fulfilledCount = fulfilled.length;
|
|
109
|
+
const total = results.length;
|
|
110
|
+
|
|
111
|
+
// 多数逻辑:成功数 > 失败数
|
|
112
|
+
if (fulfilledCount > total - fulfilledCount) {
|
|
113
|
+
// 超过半数成功,返回成功的值数组
|
|
114
|
+
return fulfilled.map(result => result.value);
|
|
115
|
+
} else {
|
|
116
|
+
// 未达到多数,抛出MAJORITY_ERROR
|
|
117
|
+
throw createLogicError('MAJORITY_ERROR', fulfilledCount, total, results);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
static createFlipFlop(initialState = false) {
|
|
123
|
+
let state = initialState;
|
|
124
|
+
let resolveCurrent = null;
|
|
125
|
+
let currentPromise = Promise.resolve(state);
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
// 获取当前状态
|
|
129
|
+
getState() {
|
|
130
|
+
return state;
|
|
131
|
+
},
|
|
132
|
+
|
|
133
|
+
// 异步设置状态
|
|
134
|
+
set(newState) {
|
|
135
|
+
state = newState;
|
|
136
|
+
if (resolveCurrent) {
|
|
137
|
+
resolveCurrent(state);
|
|
138
|
+
resolveCurrent = null;
|
|
139
|
+
}
|
|
140
|
+
currentPromise = Promise.resolve(state);
|
|
141
|
+
return this;
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
// 切换状态
|
|
145
|
+
toggle() {
|
|
146
|
+
return this.set(!state);
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
// 等待状态变化
|
|
150
|
+
waitForChange() {
|
|
151
|
+
if (!resolveCurrent) {
|
|
152
|
+
currentPromise = new Promise((resolve) => {
|
|
153
|
+
resolveCurrent = resolve;
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
return currentPromise;
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
// 等待特定状态
|
|
160
|
+
waitFor(targetState) {
|
|
161
|
+
if (state === targetState) {
|
|
162
|
+
return Promise.resolve(state);
|
|
163
|
+
}
|
|
164
|
+
return new Promise((resolve) => {
|
|
165
|
+
const checkState = () => {
|
|
166
|
+
if (state === targetState) {
|
|
167
|
+
resolve(state);
|
|
168
|
+
} else {
|
|
169
|
+
this.waitForChange().then(checkState);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
checkState();
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function createPromiseLogic(options = {}) {
|
|
180
|
+
const { prefix = '', suffix = '', rename = {} } = options;
|
|
181
|
+
|
|
182
|
+
// 基础方法映射
|
|
183
|
+
const methods = {
|
|
184
|
+
and: PromiseLogic.and,
|
|
185
|
+
or: PromiseLogic.or,
|
|
186
|
+
race: PromiseLogic.race,
|
|
187
|
+
allSettled: PromiseLogic.allSettled,
|
|
188
|
+
xor: PromiseLogic.xor,
|
|
189
|
+
nand: PromiseLogic.nand,
|
|
190
|
+
nor: PromiseLogic.nor,
|
|
191
|
+
xnor: PromiseLogic.xnor,
|
|
192
|
+
majority: PromiseLogic.majority
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
// 应用命名转换
|
|
196
|
+
const result = {};
|
|
197
|
+
Object.entries(methods).forEach(([key, fn]) => {
|
|
198
|
+
// 优先使用rename映射,然后应用prefix和suffix
|
|
199
|
+
const baseName = rename[key] || key;
|
|
200
|
+
const finalName = `${prefix}${baseName}${suffix}`;
|
|
201
|
+
result[finalName] = fn;
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
return result;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
exports.createPromiseLogic = createPromiseLogic;
|
|
208
|
+
//# sourceMappingURL=factory.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.cjs.js","sources":["../src/utils/errors.js","../src/PromiseLogic.js","../src/factory.js"],"sourcesContent":["export class PromiseLogicError extends Error {\r\n constructor(type, message, results) {\r\n super(message);\r\n this.name = 'PromiseLogicError';\r\n this.type = type; // 'XOR_ERROR', 'NAND_ERROR', etc.\r\n this.results = results; // settlement results of all Promises\r\n }\r\n}\r\n\r\n// Error factory function\r\nexport function createLogicError(type, fulfilledCount, total, results) {\r\n const messages = {\r\n XOR_ERROR: `XOR condition failed: expected exactly 1 promise to fulfill, but ${fulfilledCount} fulfilled.`,\r\n NAND_ERROR: `NAND condition failed: all ${total} promises fulfilled (expected at least one rejection).`,\r\n NOR_ERROR: `NOR condition failed: ${fulfilledCount} promises fulfilled (expected all rejected).`,\r\n MAJORITY_ERROR: `Majority condition failed: ${fulfilledCount}/${total} fulfilled (need majority).`\r\n };\r\n \r\n return new PromiseLogicError(type, messages[type] || 'Logic condition failed', results);\r\n}","import { createLogicError } from './utils/errors.js';\r\n\r\nexport class PromiseLogic {\r\n static and(iterable) {\r\n return Promise.all(iterable);\r\n }\r\n\r\n static or(iterable) {\r\n return Promise.any(iterable);\r\n }\r\n\r\n static race(iterable) {\r\n return Promise.race(iterable);\r\n }\r\n\r\n static allSettled(iterable) {\r\n return Promise.allSettled(iterable);\r\n }\r\n\r\n static xor(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n if (fulfilledCount === 1) {\r\n // 恰好一个成功,返回成功的值\r\n return fulfilled[0].value;\r\n } else {\r\n // 0个或多个(>1)成功,抛出XOR_ERROR\r\n throw createLogicError('XOR_ERROR', fulfilledCount, total, results);\r\n }\r\n });\r\n }\r\n\r\n static nand(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n if (fulfilledCount === total) {\r\n // 全部成功,抛出NAND_ERROR\r\n throw createLogicError('NAND_ERROR', fulfilledCount, total, results);\r\n } else {\r\n // 不是所有都成功,返回成功的值数组\r\n return fulfilled.map(result => result.value);\r\n }\r\n });\r\n }\r\n\r\n static nor(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n if (fulfilledCount === 0) {\r\n // 全部失败,返回空数组表示成功\r\n return [];\r\n } else {\r\n // 任意成功,抛出NOR_ERROR\r\n throw createLogicError('NOR_ERROR', fulfilledCount, total, results);\r\n }\r\n });\r\n }\r\n\r\n static xnor(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n if (fulfilledCount === 0 || fulfilledCount === total) {\r\n // 全部成功或全部失败,返回成功的值数组\r\n return fulfilled.map(result => result.value);\r\n } else {\r\n // 部分成功部分失败,抛出XNOR_ERROR\r\n throw createLogicError('XNOR_ERROR', fulfilledCount, total, results);\r\n }\r\n });\r\n }\r\n\r\n static majority(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n // 多数逻辑:成功数 > 失败数\r\n if (fulfilledCount > total - fulfilledCount) {\r\n // 超过半数成功,返回成功的值数组\r\n return fulfilled.map(result => result.value);\r\n } else {\r\n // 未达到多数,抛出MAJORITY_ERROR\r\n throw createLogicError('MAJORITY_ERROR', fulfilledCount, total, results);\r\n }\r\n });\r\n }\r\n\r\n static createFlipFlop(initialState = false) {\r\n let state = initialState;\r\n let resolveCurrent = null;\r\n let currentPromise = Promise.resolve(state);\r\n \r\n return {\r\n // 获取当前状态\r\n getState() {\r\n return state;\r\n },\r\n \r\n // 异步设置状态\r\n set(newState) {\r\n state = newState;\r\n if (resolveCurrent) {\r\n resolveCurrent(state);\r\n resolveCurrent = null;\r\n }\r\n currentPromise = Promise.resolve(state);\r\n return this;\r\n },\r\n \r\n // 切换状态\r\n toggle() {\r\n return this.set(!state);\r\n },\r\n \r\n // 等待状态变化\r\n waitForChange() {\r\n if (!resolveCurrent) {\r\n currentPromise = new Promise((resolve) => {\r\n resolveCurrent = resolve;\r\n });\r\n }\r\n return currentPromise;\r\n },\r\n \r\n // 等待特定状态\r\n waitFor(targetState) {\r\n if (state === targetState) {\r\n return Promise.resolve(state);\r\n }\r\n return new Promise((resolve) => {\r\n const checkState = () => {\r\n if (state === targetState) {\r\n resolve(state);\r\n } else {\r\n this.waitForChange().then(checkState);\r\n }\r\n };\r\n checkState();\r\n });\r\n }\r\n };\r\n }\r\n}","import { PromiseLogic } from './PromiseLogic.js';\n\nexport function createPromiseLogic(options = {}) {\n const { prefix = '', suffix = '', rename = {} } = options;\n \n // 基础方法映射\n const methods = {\n and: PromiseLogic.and,\n or: PromiseLogic.or,\n race: PromiseLogic.race,\n allSettled: PromiseLogic.allSettled,\n xor: PromiseLogic.xor,\n nand: PromiseLogic.nand,\n nor: PromiseLogic.nor,\n xnor: PromiseLogic.xnor,\n majority: PromiseLogic.majority\n };\n \n // 应用命名转换\n const result = {};\n Object.entries(methods).forEach(([key, fn]) => {\n // 优先使用rename映射,然后应用prefix和suffix\n const baseName = rename[key] || key;\n const finalName = `${prefix}${baseName}${suffix}`;\n result[finalName] = fn;\n });\n \n return result;\n}"],"names":[],"mappings":";;AAAO,MAAM,iBAAiB,SAAS,KAAK,CAAC;AAC7C,EAAE,WAAW,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE;AACtC,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;AACnB,IAAI,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;AACpC,IAAI,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;AACrB,IAAI,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;AAC3B,EAAE,CAAC;AACH,CAAC;AACD;AACA;AACO,SAAS,gBAAgB,CAAC,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,OAAO,EAAE;AACvE,EAAE,MAAM,QAAQ,GAAG;AACnB,IAAI,SAAS,EAAE,CAAC,iEAAiE,EAAE,cAAc,CAAC,WAAW,CAAC;AAC9G,IAAI,UAAU,EAAE,CAAC,2BAA2B,EAAE,KAAK,CAAC,sDAAsD,CAAC;AAC3G,IAAI,SAAS,EAAE,CAAC,sBAAsB,EAAE,cAAc,CAAC,4CAA4C,CAAC;AACpG,IAAI,cAAc,EAAE,CAAC,2BAA2B,EAAE,cAAc,CAAC,CAAC,EAAE,KAAK,CAAC,2BAA2B,CAAC;AACtG,GAAG,CAAC;AACJ;AACA,EAAE,OAAO,IAAI,iBAAiB,CAAC,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC,IAAI,wBAAwB,EAAE,OAAO,CAAC,CAAC;AAC1F;;ACjBO,MAAM,YAAY,CAAC;AAC1B,EAAE,OAAO,GAAG,CAAC,QAAQ,EAAE;AACvB,IAAI,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;AACjC,EAAE,CAAC;AACH;AACA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AACvC,IAAI,MAAM,SAAS,GAAG,CAAC,EAAE,MAAM,CAAC,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,CAAC;AACrD,IAAI,MAAM,CAAC,SAAS,CAAC,GAAG,EAAE;AAC1B,EAAE,CAAC,CAAC;AACJ;AACA,EAAE,OAAO,MAAM;AACf;;;;"}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
class PromiseLogicError extends Error {
|
|
2
|
+
constructor(type, message, results) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = 'PromiseLogicError';
|
|
5
|
+
this.type = type; // 'XOR_ERROR', 'NAND_ERROR', etc.
|
|
6
|
+
this.results = results; // settlement results of all Promises
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// Error factory function
|
|
11
|
+
function createLogicError(type, fulfilledCount, total, results) {
|
|
12
|
+
const messages = {
|
|
13
|
+
XOR_ERROR: `XOR condition failed: expected exactly 1 promise to fulfill, but ${fulfilledCount} fulfilled.`,
|
|
14
|
+
NAND_ERROR: `NAND condition failed: all ${total} promises fulfilled (expected at least one rejection).`,
|
|
15
|
+
NOR_ERROR: `NOR condition failed: ${fulfilledCount} promises fulfilled (expected all rejected).`,
|
|
16
|
+
MAJORITY_ERROR: `Majority condition failed: ${fulfilledCount}/${total} fulfilled (need majority).`
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
return new PromiseLogicError(type, messages[type] || 'Logic condition failed', results);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
class PromiseLogic {
|
|
23
|
+
static and(iterable) {
|
|
24
|
+
return Promise.all(iterable);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static or(iterable) {
|
|
28
|
+
return Promise.any(iterable);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
static race(iterable) {
|
|
32
|
+
return Promise.race(iterable);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static allSettled(iterable) {
|
|
36
|
+
return Promise.allSettled(iterable);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static xor(iterable) {
|
|
40
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
41
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
42
|
+
const fulfilledCount = fulfilled.length;
|
|
43
|
+
const total = results.length;
|
|
44
|
+
|
|
45
|
+
if (fulfilledCount === 1) {
|
|
46
|
+
// 恰好一个成功,返回成功的值
|
|
47
|
+
return fulfilled[0].value;
|
|
48
|
+
} else {
|
|
49
|
+
// 0个或多个(>1)成功,抛出XOR_ERROR
|
|
50
|
+
throw createLogicError('XOR_ERROR', fulfilledCount, total, results);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static nand(iterable) {
|
|
56
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
57
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
58
|
+
const fulfilledCount = fulfilled.length;
|
|
59
|
+
const total = results.length;
|
|
60
|
+
|
|
61
|
+
if (fulfilledCount === total) {
|
|
62
|
+
// 全部成功,抛出NAND_ERROR
|
|
63
|
+
throw createLogicError('NAND_ERROR', fulfilledCount, total, results);
|
|
64
|
+
} else {
|
|
65
|
+
// 不是所有都成功,返回成功的值数组
|
|
66
|
+
return fulfilled.map(result => result.value);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
static nor(iterable) {
|
|
72
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
73
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
74
|
+
const fulfilledCount = fulfilled.length;
|
|
75
|
+
const total = results.length;
|
|
76
|
+
|
|
77
|
+
if (fulfilledCount === 0) {
|
|
78
|
+
// 全部失败,返回空数组表示成功
|
|
79
|
+
return [];
|
|
80
|
+
} else {
|
|
81
|
+
// 任意成功,抛出NOR_ERROR
|
|
82
|
+
throw createLogicError('NOR_ERROR', fulfilledCount, total, results);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
static xnor(iterable) {
|
|
88
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
89
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
90
|
+
const fulfilledCount = fulfilled.length;
|
|
91
|
+
const total = results.length;
|
|
92
|
+
|
|
93
|
+
if (fulfilledCount === 0 || fulfilledCount === total) {
|
|
94
|
+
// 全部成功或全部失败,返回成功的值数组
|
|
95
|
+
return fulfilled.map(result => result.value);
|
|
96
|
+
} else {
|
|
97
|
+
// 部分成功部分失败,抛出XNOR_ERROR
|
|
98
|
+
throw createLogicError('XNOR_ERROR', fulfilledCount, total, results);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
static majority(iterable) {
|
|
104
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
105
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
106
|
+
const fulfilledCount = fulfilled.length;
|
|
107
|
+
const total = results.length;
|
|
108
|
+
|
|
109
|
+
// 多数逻辑:成功数 > 失败数
|
|
110
|
+
if (fulfilledCount > total - fulfilledCount) {
|
|
111
|
+
// 超过半数成功,返回成功的值数组
|
|
112
|
+
return fulfilled.map(result => result.value);
|
|
113
|
+
} else {
|
|
114
|
+
// 未达到多数,抛出MAJORITY_ERROR
|
|
115
|
+
throw createLogicError('MAJORITY_ERROR', fulfilledCount, total, results);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
static createFlipFlop(initialState = false) {
|
|
121
|
+
let state = initialState;
|
|
122
|
+
let resolveCurrent = null;
|
|
123
|
+
let currentPromise = Promise.resolve(state);
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
// 获取当前状态
|
|
127
|
+
getState() {
|
|
128
|
+
return state;
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
// 异步设置状态
|
|
132
|
+
set(newState) {
|
|
133
|
+
state = newState;
|
|
134
|
+
if (resolveCurrent) {
|
|
135
|
+
resolveCurrent(state);
|
|
136
|
+
resolveCurrent = null;
|
|
137
|
+
}
|
|
138
|
+
currentPromise = Promise.resolve(state);
|
|
139
|
+
return this;
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
// 切换状态
|
|
143
|
+
toggle() {
|
|
144
|
+
return this.set(!state);
|
|
145
|
+
},
|
|
146
|
+
|
|
147
|
+
// 等待状态变化
|
|
148
|
+
waitForChange() {
|
|
149
|
+
if (!resolveCurrent) {
|
|
150
|
+
currentPromise = new Promise((resolve) => {
|
|
151
|
+
resolveCurrent = resolve;
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
return currentPromise;
|
|
155
|
+
},
|
|
156
|
+
|
|
157
|
+
// 等待特定状态
|
|
158
|
+
waitFor(targetState) {
|
|
159
|
+
if (state === targetState) {
|
|
160
|
+
return Promise.resolve(state);
|
|
161
|
+
}
|
|
162
|
+
return new Promise((resolve) => {
|
|
163
|
+
const checkState = () => {
|
|
164
|
+
if (state === targetState) {
|
|
165
|
+
resolve(state);
|
|
166
|
+
} else {
|
|
167
|
+
this.waitForChange().then(checkState);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
checkState();
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function createPromiseLogic(options = {}) {
|
|
178
|
+
const { prefix = '', suffix = '', rename = {} } = options;
|
|
179
|
+
|
|
180
|
+
// 基础方法映射
|
|
181
|
+
const methods = {
|
|
182
|
+
and: PromiseLogic.and,
|
|
183
|
+
or: PromiseLogic.or,
|
|
184
|
+
race: PromiseLogic.race,
|
|
185
|
+
allSettled: PromiseLogic.allSettled,
|
|
186
|
+
xor: PromiseLogic.xor,
|
|
187
|
+
nand: PromiseLogic.nand,
|
|
188
|
+
nor: PromiseLogic.nor,
|
|
189
|
+
xnor: PromiseLogic.xnor,
|
|
190
|
+
majority: PromiseLogic.majority
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
// 应用命名转换
|
|
194
|
+
const result = {};
|
|
195
|
+
Object.entries(methods).forEach(([key, fn]) => {
|
|
196
|
+
// 优先使用rename映射,然后应用prefix和suffix
|
|
197
|
+
const baseName = rename[key] || key;
|
|
198
|
+
const finalName = `${prefix}${baseName}${suffix}`;
|
|
199
|
+
result[finalName] = fn;
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
return result;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export { createPromiseLogic };
|
|
206
|
+
//# sourceMappingURL=factory.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.esm.js","sources":["../src/utils/errors.js","../src/PromiseLogic.js","../src/factory.js"],"sourcesContent":["export class PromiseLogicError extends Error {\r\n constructor(type, message, results) {\r\n super(message);\r\n this.name = 'PromiseLogicError';\r\n this.type = type; // 'XOR_ERROR', 'NAND_ERROR', etc.\r\n this.results = results; // settlement results of all Promises\r\n }\r\n}\r\n\r\n// Error factory function\r\nexport function createLogicError(type, fulfilledCount, total, results) {\r\n const messages = {\r\n XOR_ERROR: `XOR condition failed: expected exactly 1 promise to fulfill, but ${fulfilledCount} fulfilled.`,\r\n NAND_ERROR: `NAND condition failed: all ${total} promises fulfilled (expected at least one rejection).`,\r\n NOR_ERROR: `NOR condition failed: ${fulfilledCount} promises fulfilled (expected all rejected).`,\r\n MAJORITY_ERROR: `Majority condition failed: ${fulfilledCount}/${total} fulfilled (need majority).`\r\n };\r\n \r\n return new PromiseLogicError(type, messages[type] || 'Logic condition failed', results);\r\n}","import { createLogicError } from './utils/errors.js';\r\n\r\nexport class PromiseLogic {\r\n static and(iterable) {\r\n return Promise.all(iterable);\r\n }\r\n\r\n static or(iterable) {\r\n return Promise.any(iterable);\r\n }\r\n\r\n static race(iterable) {\r\n return Promise.race(iterable);\r\n }\r\n\r\n static allSettled(iterable) {\r\n return Promise.allSettled(iterable);\r\n }\r\n\r\n static xor(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n if (fulfilledCount === 1) {\r\n // 恰好一个成功,返回成功的值\r\n return fulfilled[0].value;\r\n } else {\r\n // 0个或多个(>1)成功,抛出XOR_ERROR\r\n throw createLogicError('XOR_ERROR', fulfilledCount, total, results);\r\n }\r\n });\r\n }\r\n\r\n static nand(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n if (fulfilledCount === total) {\r\n // 全部成功,抛出NAND_ERROR\r\n throw createLogicError('NAND_ERROR', fulfilledCount, total, results);\r\n } else {\r\n // 不是所有都成功,返回成功的值数组\r\n return fulfilled.map(result => result.value);\r\n }\r\n });\r\n }\r\n\r\n static nor(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n if (fulfilledCount === 0) {\r\n // 全部失败,返回空数组表示成功\r\n return [];\r\n } else {\r\n // 任意成功,抛出NOR_ERROR\r\n throw createLogicError('NOR_ERROR', fulfilledCount, total, results);\r\n }\r\n });\r\n }\r\n\r\n static xnor(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n if (fulfilledCount === 0 || fulfilledCount === total) {\r\n // 全部成功或全部失败,返回成功的值数组\r\n return fulfilled.map(result => result.value);\r\n } else {\r\n // 部分成功部分失败,抛出XNOR_ERROR\r\n throw createLogicError('XNOR_ERROR', fulfilledCount, total, results);\r\n }\r\n });\r\n }\r\n\r\n static majority(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n // 多数逻辑:成功数 > 失败数\r\n if (fulfilledCount > total - fulfilledCount) {\r\n // 超过半数成功,返回成功的值数组\r\n return fulfilled.map(result => result.value);\r\n } else {\r\n // 未达到多数,抛出MAJORITY_ERROR\r\n throw createLogicError('MAJORITY_ERROR', fulfilledCount, total, results);\r\n }\r\n });\r\n }\r\n\r\n static createFlipFlop(initialState = false) {\r\n let state = initialState;\r\n let resolveCurrent = null;\r\n let currentPromise = Promise.resolve(state);\r\n \r\n return {\r\n // 获取当前状态\r\n getState() {\r\n return state;\r\n },\r\n \r\n // 异步设置状态\r\n set(newState) {\r\n state = newState;\r\n if (resolveCurrent) {\r\n resolveCurrent(state);\r\n resolveCurrent = null;\r\n }\r\n currentPromise = Promise.resolve(state);\r\n return this;\r\n },\r\n \r\n // 切换状态\r\n toggle() {\r\n return this.set(!state);\r\n },\r\n \r\n // 等待状态变化\r\n waitForChange() {\r\n if (!resolveCurrent) {\r\n currentPromise = new Promise((resolve) => {\r\n resolveCurrent = resolve;\r\n });\r\n }\r\n return currentPromise;\r\n },\r\n \r\n // 等待特定状态\r\n waitFor(targetState) {\r\n if (state === targetState) {\r\n return Promise.resolve(state);\r\n }\r\n return new Promise((resolve) => {\r\n const checkState = () => {\r\n if (state === targetState) {\r\n resolve(state);\r\n } else {\r\n this.waitForChange().then(checkState);\r\n }\r\n };\r\n checkState();\r\n });\r\n }\r\n };\r\n }\r\n}","import { PromiseLogic } from './PromiseLogic.js';\n\nexport function createPromiseLogic(options = {}) {\n const { prefix = '', suffix = '', rename = {} } = options;\n \n // 基础方法映射\n const methods = {\n and: PromiseLogic.and,\n or: PromiseLogic.or,\n race: PromiseLogic.race,\n allSettled: PromiseLogic.allSettled,\n xor: PromiseLogic.xor,\n nand: PromiseLogic.nand,\n nor: PromiseLogic.nor,\n xnor: PromiseLogic.xnor,\n majority: PromiseLogic.majority\n };\n \n // 应用命名转换\n const result = {};\n Object.entries(methods).forEach(([key, fn]) => {\n // 优先使用rename映射,然后应用prefix和suffix\n const baseName = rename[key] || key;\n const finalName = `${prefix}${baseName}${suffix}`;\n result[finalName] = fn;\n });\n \n return result;\n}"],"names":[],"mappings":"AAAO,MAAM,iBAAiB,SAAS,KAAK,CAAC;AAC7C,EAAE,WAAW,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE;AACtC,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;AACnB,IAAI,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;AACpC,IAAI,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;AACrB,IAAI,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;AAC3B,EAAE,CAAC;AACH,CAAC;AACD;AACA;AACO,SAAS,gBAAgB,CAAC,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,OAAO,EAAE;AACvE,EAAE,MAAM,QAAQ,GAAG;AACnB,IAAI,SAAS,EAAE,CAAC,iEAAiE,EAAE,cAAc,CAAC,WAAW,CAAC;AAC9G,IAAI,UAAU,EAAE,CAAC,2BAA2B,EAAE,KAAK,CAAC,sDAAsD,CAAC;AAC3G,IAAI,SAAS,EAAE,CAAC,sBAAsB,EAAE,cAAc,CAAC,4CAA4C,CAAC;AACpG,IAAI,cAAc,EAAE,CAAC,2BAA2B,EAAE,cAAc,CAAC,CAAC,EAAE,KAAK,CAAC,2BAA2B,CAAC;AACtG,GAAG,CAAC;AACJ;AACA,EAAE,OAAO,IAAI,iBAAiB,CAAC,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC,IAAI,wBAAwB,EAAE,OAAO,CAAC,CAAC;AAC1F;;ACjBO,MAAM,YAAY,CAAC;AAC1B,EAAE,OAAO,GAAG,CAAC,QAAQ,EAAE;AACvB,IAAI,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,C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,CAAC,GAAG,CAAC,IAAI,GAAG;AACvC,IAAI,MAAM,SAAS,GAAG,CAAC,EAAE,MAAM,CAAC,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,CAAC;AACrD,IAAI,MAAM,CAAC,SAAS,CAAC,GAAG,EAAE;AAC1B,EAAE,CAAC,CAAC;AACJ;AACA,EAAE,OAAO,MAAM;AACf;;;;"}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class PromiseLogicError extends Error {
|
|
4
|
+
constructor(type, message, results) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = 'PromiseLogicError';
|
|
7
|
+
this.type = type; // 'XOR_ERROR', 'NAND_ERROR', etc.
|
|
8
|
+
this.results = results; // settlement results of all Promises
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Error factory function
|
|
13
|
+
function createLogicError(type, fulfilledCount, total, results) {
|
|
14
|
+
const messages = {
|
|
15
|
+
XOR_ERROR: `XOR condition failed: expected exactly 1 promise to fulfill, but ${fulfilledCount} fulfilled.`,
|
|
16
|
+
NAND_ERROR: `NAND condition failed: all ${total} promises fulfilled (expected at least one rejection).`,
|
|
17
|
+
NOR_ERROR: `NOR condition failed: ${fulfilledCount} promises fulfilled (expected all rejected).`,
|
|
18
|
+
MAJORITY_ERROR: `Majority condition failed: ${fulfilledCount}/${total} fulfilled (need majority).`
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
return new PromiseLogicError(type, messages[type] || 'Logic condition failed', results);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
class PromiseLogic {
|
|
25
|
+
static and(iterable) {
|
|
26
|
+
return Promise.all(iterable);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static or(iterable) {
|
|
30
|
+
return Promise.any(iterable);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static race(iterable) {
|
|
34
|
+
return Promise.race(iterable);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static allSettled(iterable) {
|
|
38
|
+
return Promise.allSettled(iterable);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static xor(iterable) {
|
|
42
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
43
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
44
|
+
const fulfilledCount = fulfilled.length;
|
|
45
|
+
const total = results.length;
|
|
46
|
+
|
|
47
|
+
if (fulfilledCount === 1) {
|
|
48
|
+
// 恰好一个成功,返回成功的值
|
|
49
|
+
return fulfilled[0].value;
|
|
50
|
+
} else {
|
|
51
|
+
// 0个或多个(>1)成功,抛出XOR_ERROR
|
|
52
|
+
throw createLogicError('XOR_ERROR', fulfilledCount, total, results);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static nand(iterable) {
|
|
58
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
59
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
60
|
+
const fulfilledCount = fulfilled.length;
|
|
61
|
+
const total = results.length;
|
|
62
|
+
|
|
63
|
+
if (fulfilledCount === total) {
|
|
64
|
+
// 全部成功,抛出NAND_ERROR
|
|
65
|
+
throw createLogicError('NAND_ERROR', fulfilledCount, total, results);
|
|
66
|
+
} else {
|
|
67
|
+
// 不是所有都成功,返回成功的值数组
|
|
68
|
+
return fulfilled.map(result => result.value);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static nor(iterable) {
|
|
74
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
75
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
76
|
+
const fulfilledCount = fulfilled.length;
|
|
77
|
+
const total = results.length;
|
|
78
|
+
|
|
79
|
+
if (fulfilledCount === 0) {
|
|
80
|
+
// 全部失败,返回空数组表示成功
|
|
81
|
+
return [];
|
|
82
|
+
} else {
|
|
83
|
+
// 任意成功,抛出NOR_ERROR
|
|
84
|
+
throw createLogicError('NOR_ERROR', fulfilledCount, total, results);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
static xnor(iterable) {
|
|
90
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
91
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
92
|
+
const fulfilledCount = fulfilled.length;
|
|
93
|
+
const total = results.length;
|
|
94
|
+
|
|
95
|
+
if (fulfilledCount === 0 || fulfilledCount === total) {
|
|
96
|
+
// 全部成功或全部失败,返回成功的值数组
|
|
97
|
+
return fulfilled.map(result => result.value);
|
|
98
|
+
} else {
|
|
99
|
+
// 部分成功部分失败,抛出XNOR_ERROR
|
|
100
|
+
throw createLogicError('XNOR_ERROR', fulfilledCount, total, results);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
static majority(iterable) {
|
|
106
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
107
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
108
|
+
const fulfilledCount = fulfilled.length;
|
|
109
|
+
const total = results.length;
|
|
110
|
+
|
|
111
|
+
// 多数逻辑:成功数 > 失败数
|
|
112
|
+
if (fulfilledCount > total - fulfilledCount) {
|
|
113
|
+
// 超过半数成功,返回成功的值数组
|
|
114
|
+
return fulfilled.map(result => result.value);
|
|
115
|
+
} else {
|
|
116
|
+
// 未达到多数,抛出MAJORITY_ERROR
|
|
117
|
+
throw createLogicError('MAJORITY_ERROR', fulfilledCount, total, results);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
static createFlipFlop(initialState = false) {
|
|
123
|
+
let state = initialState;
|
|
124
|
+
let resolveCurrent = null;
|
|
125
|
+
let currentPromise = Promise.resolve(state);
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
// 获取当前状态
|
|
129
|
+
getState() {
|
|
130
|
+
return state;
|
|
131
|
+
},
|
|
132
|
+
|
|
133
|
+
// 异步设置状态
|
|
134
|
+
set(newState) {
|
|
135
|
+
state = newState;
|
|
136
|
+
if (resolveCurrent) {
|
|
137
|
+
resolveCurrent(state);
|
|
138
|
+
resolveCurrent = null;
|
|
139
|
+
}
|
|
140
|
+
currentPromise = Promise.resolve(state);
|
|
141
|
+
return this;
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
// 切换状态
|
|
145
|
+
toggle() {
|
|
146
|
+
return this.set(!state);
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
// 等待状态变化
|
|
150
|
+
waitForChange() {
|
|
151
|
+
if (!resolveCurrent) {
|
|
152
|
+
currentPromise = new Promise((resolve) => {
|
|
153
|
+
resolveCurrent = resolve;
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
return currentPromise;
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
// 等待特定状态
|
|
160
|
+
waitFor(targetState) {
|
|
161
|
+
if (state === targetState) {
|
|
162
|
+
return Promise.resolve(state);
|
|
163
|
+
}
|
|
164
|
+
return new Promise((resolve) => {
|
|
165
|
+
const checkState = () => {
|
|
166
|
+
if (state === targetState) {
|
|
167
|
+
resolve(state);
|
|
168
|
+
} else {
|
|
169
|
+
this.waitForChange().then(checkState);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
checkState();
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function createPromiseLogic(options = {}) {
|
|
180
|
+
const { prefix = '', suffix = '', rename = {} } = options;
|
|
181
|
+
|
|
182
|
+
// 基础方法映射
|
|
183
|
+
const methods = {
|
|
184
|
+
and: PromiseLogic.and,
|
|
185
|
+
or: PromiseLogic.or,
|
|
186
|
+
race: PromiseLogic.race,
|
|
187
|
+
allSettled: PromiseLogic.allSettled,
|
|
188
|
+
xor: PromiseLogic.xor,
|
|
189
|
+
nand: PromiseLogic.nand,
|
|
190
|
+
nor: PromiseLogic.nor,
|
|
191
|
+
xnor: PromiseLogic.xnor,
|
|
192
|
+
majority: PromiseLogic.majority
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
// 应用命名转换
|
|
196
|
+
const result = {};
|
|
197
|
+
Object.entries(methods).forEach(([key, fn]) => {
|
|
198
|
+
// 优先使用rename映射,然后应用prefix和suffix
|
|
199
|
+
const baseName = rename[key] || key;
|
|
200
|
+
const finalName = `${prefix}${baseName}${suffix}`;
|
|
201
|
+
result[finalName] = fn;
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
return result;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
exports.PromiseLogic = PromiseLogic;
|
|
208
|
+
exports.PromiseLogicError = PromiseLogicError;
|
|
209
|
+
exports.createLogicError = createLogicError;
|
|
210
|
+
exports.createPromiseLogic = createPromiseLogic;
|
|
211
|
+
//# sourceMappingURL=index.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../src/utils/errors.js","../src/PromiseLogic.js","../src/factory.js"],"sourcesContent":["export class PromiseLogicError extends Error {\r\n constructor(type, message, results) {\r\n super(message);\r\n this.name = 'PromiseLogicError';\r\n this.type = type; // 'XOR_ERROR', 'NAND_ERROR', etc.\r\n this.results = results; // settlement results of all Promises\r\n }\r\n}\r\n\r\n// Error factory function\r\nexport function createLogicError(type, fulfilledCount, total, results) {\r\n const messages = {\r\n XOR_ERROR: `XOR condition failed: expected exactly 1 promise to fulfill, but ${fulfilledCount} fulfilled.`,\r\n NAND_ERROR: `NAND condition failed: all ${total} promises fulfilled (expected at least one rejection).`,\r\n NOR_ERROR: `NOR condition failed: ${fulfilledCount} promises fulfilled (expected all rejected).`,\r\n MAJORITY_ERROR: `Majority condition failed: ${fulfilledCount}/${total} fulfilled (need majority).`\r\n };\r\n \r\n return new PromiseLogicError(type, messages[type] || 'Logic condition failed', results);\r\n}","import { createLogicError } from './utils/errors.js';\r\n\r\nexport class PromiseLogic {\r\n static and(iterable) {\r\n return Promise.all(iterable);\r\n }\r\n\r\n static or(iterable) {\r\n return Promise.any(iterable);\r\n }\r\n\r\n static race(iterable) {\r\n return Promise.race(iterable);\r\n }\r\n\r\n static allSettled(iterable) {\r\n return Promise.allSettled(iterable);\r\n }\r\n\r\n static xor(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n if (fulfilledCount === 1) {\r\n // 恰好一个成功,返回成功的值\r\n return fulfilled[0].value;\r\n } else {\r\n // 0个或多个(>1)成功,抛出XOR_ERROR\r\n throw createLogicError('XOR_ERROR', fulfilledCount, total, results);\r\n }\r\n });\r\n }\r\n\r\n static nand(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n if (fulfilledCount === total) {\r\n // 全部成功,抛出NAND_ERROR\r\n throw createLogicError('NAND_ERROR', fulfilledCount, total, results);\r\n } else {\r\n // 不是所有都成功,返回成功的值数组\r\n return fulfilled.map(result => result.value);\r\n }\r\n });\r\n }\r\n\r\n static nor(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n if (fulfilledCount === 0) {\r\n // 全部失败,返回空数组表示成功\r\n return [];\r\n } else {\r\n // 任意成功,抛出NOR_ERROR\r\n throw createLogicError('NOR_ERROR', fulfilledCount, total, results);\r\n }\r\n });\r\n }\r\n\r\n static xnor(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n if (fulfilledCount === 0 || fulfilledCount === total) {\r\n // 全部成功或全部失败,返回成功的值数组\r\n return fulfilled.map(result => result.value);\r\n } else {\r\n // 部分成功部分失败,抛出XNOR_ERROR\r\n throw createLogicError('XNOR_ERROR', fulfilledCount, total, results);\r\n }\r\n });\r\n }\r\n\r\n static majority(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n // 多数逻辑:成功数 > 失败数\r\n if (fulfilledCount > total - fulfilledCount) {\r\n // 超过半数成功,返回成功的值数组\r\n return fulfilled.map(result => result.value);\r\n } else {\r\n // 未达到多数,抛出MAJORITY_ERROR\r\n throw createLogicError('MAJORITY_ERROR', fulfilledCount, total, results);\r\n }\r\n });\r\n }\r\n\r\n static createFlipFlop(initialState = false) {\r\n let state = initialState;\r\n let resolveCurrent = null;\r\n let currentPromise = Promise.resolve(state);\r\n \r\n return {\r\n // 获取当前状态\r\n getState() {\r\n return state;\r\n },\r\n \r\n // 异步设置状态\r\n set(newState) {\r\n state = newState;\r\n if (resolveCurrent) {\r\n resolveCurrent(state);\r\n resolveCurrent = null;\r\n }\r\n currentPromise = Promise.resolve(state);\r\n return this;\r\n },\r\n \r\n // 切换状态\r\n toggle() {\r\n return this.set(!state);\r\n },\r\n \r\n // 等待状态变化\r\n waitForChange() {\r\n if (!resolveCurrent) {\r\n currentPromise = new Promise((resolve) => {\r\n resolveCurrent = resolve;\r\n });\r\n }\r\n return currentPromise;\r\n },\r\n \r\n // 等待特定状态\r\n waitFor(targetState) {\r\n if (state === targetState) {\r\n return Promise.resolve(state);\r\n }\r\n return new Promise((resolve) => {\r\n const checkState = () => {\r\n if (state === targetState) {\r\n resolve(state);\r\n } else {\r\n this.waitForChange().then(checkState);\r\n }\r\n };\r\n checkState();\r\n });\r\n }\r\n };\r\n }\r\n}","import { PromiseLogic } from './PromiseLogic.js';\n\nexport function createPromiseLogic(options = {}) {\n const { prefix = '', suffix = '', rename = {} } = options;\n \n // 基础方法映射\n const methods = {\n and: PromiseLogic.and,\n or: PromiseLogic.or,\n race: PromiseLogic.race,\n allSettled: PromiseLogic.allSettled,\n xor: PromiseLogic.xor,\n nand: PromiseLogic.nand,\n nor: PromiseLogic.nor,\n xnor: PromiseLogic.xnor,\n majority: PromiseLogic.majority\n };\n \n // 应用命名转换\n const result = {};\n Object.entries(methods).forEach(([key, fn]) => {\n // 优先使用rename映射,然后应用prefix和suffix\n const baseName = rename[key] || key;\n const finalName = `${prefix}${baseName}${suffix}`;\n result[finalName] = fn;\n });\n \n return result;\n}"],"names":[],"mappings":";;AAAO,MAAM,iBAAiB,SAAS,KAAK,CAAC;AAC7C,EAAE,WAAW,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE;AACtC,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;AACnB,IAAI,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;AACpC,IAAI,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;AACrB,IAAI,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;AAC3B,EAAE,CAAC;AACH,CAAC;AACD;AACA;AACO,SAAS,gBAAgB,CAAC,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,OAAO,EAAE;AACvE,EAAE,MAAM,QAAQ,GAAG;AACnB,IAAI,SAAS,EAAE,CAAC,iEAAiE,EAAE,cAAc,CAAC,WAAW,CAAC;AAC9G,IAAI,UAAU,EAAE,CAAC,2BAA2B,EAAE,KAAK,CAAC,sDAAsD,CAAC;AAC3G,IAAI,SAAS,EAAE,CAAC,sBAAsB,EAAE,cAAc,CAAC,4CAA4C,CAAC;AACpG,IAAI,cAAc,EAAE,CAAC,2BAA2B,EAAE,cAAc,CAAC,CAAC,EAAE,KAAK,CAAC,2BAA2B,CAAC;AACtG,GAAG,CAAC;AACJ;AACA,EAAE,OAAO,IAAI,iBAAiB,CAAC,IAAI,EAAE,QAAQ,CAAC,IAA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A,EAAE,MAAM,MAAM,GAAG,EAAE;AACnB,EAAE,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,KAAK;AACjD;AACA,IAAI,MAAM,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAC,IAAI,GAAG;AACvC,IAAI,MAAM,SAAS,GAAG,CAAC,EAAE,MAAM,CAAC,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,CAAC;AACrD,IAAI,MAAM,CAAC,SAAS,CAAC,GAAG,EAAE;AAC1B,EAAE,CAAC,CAAC;AACJ;AACA,EAAE,OAAO,MAAM;AACf;;;;;;;"}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
class PromiseLogicError extends Error {
|
|
2
|
+
constructor(type, message, results) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = 'PromiseLogicError';
|
|
5
|
+
this.type = type; // 'XOR_ERROR', 'NAND_ERROR', etc.
|
|
6
|
+
this.results = results; // settlement results of all Promises
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// Error factory function
|
|
11
|
+
function createLogicError(type, fulfilledCount, total, results) {
|
|
12
|
+
const messages = {
|
|
13
|
+
XOR_ERROR: `XOR condition failed: expected exactly 1 promise to fulfill, but ${fulfilledCount} fulfilled.`,
|
|
14
|
+
NAND_ERROR: `NAND condition failed: all ${total} promises fulfilled (expected at least one rejection).`,
|
|
15
|
+
NOR_ERROR: `NOR condition failed: ${fulfilledCount} promises fulfilled (expected all rejected).`,
|
|
16
|
+
MAJORITY_ERROR: `Majority condition failed: ${fulfilledCount}/${total} fulfilled (need majority).`
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
return new PromiseLogicError(type, messages[type] || 'Logic condition failed', results);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
class PromiseLogic {
|
|
23
|
+
static and(iterable) {
|
|
24
|
+
return Promise.all(iterable);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static or(iterable) {
|
|
28
|
+
return Promise.any(iterable);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
static race(iterable) {
|
|
32
|
+
return Promise.race(iterable);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static allSettled(iterable) {
|
|
36
|
+
return Promise.allSettled(iterable);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static xor(iterable) {
|
|
40
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
41
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
42
|
+
const fulfilledCount = fulfilled.length;
|
|
43
|
+
const total = results.length;
|
|
44
|
+
|
|
45
|
+
if (fulfilledCount === 1) {
|
|
46
|
+
// 恰好一个成功,返回成功的值
|
|
47
|
+
return fulfilled[0].value;
|
|
48
|
+
} else {
|
|
49
|
+
// 0个或多个(>1)成功,抛出XOR_ERROR
|
|
50
|
+
throw createLogicError('XOR_ERROR', fulfilledCount, total, results);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static nand(iterable) {
|
|
56
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
57
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
58
|
+
const fulfilledCount = fulfilled.length;
|
|
59
|
+
const total = results.length;
|
|
60
|
+
|
|
61
|
+
if (fulfilledCount === total) {
|
|
62
|
+
// 全部成功,抛出NAND_ERROR
|
|
63
|
+
throw createLogicError('NAND_ERROR', fulfilledCount, total, results);
|
|
64
|
+
} else {
|
|
65
|
+
// 不是所有都成功,返回成功的值数组
|
|
66
|
+
return fulfilled.map(result => result.value);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
static nor(iterable) {
|
|
72
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
73
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
74
|
+
const fulfilledCount = fulfilled.length;
|
|
75
|
+
const total = results.length;
|
|
76
|
+
|
|
77
|
+
if (fulfilledCount === 0) {
|
|
78
|
+
// 全部失败,返回空数组表示成功
|
|
79
|
+
return [];
|
|
80
|
+
} else {
|
|
81
|
+
// 任意成功,抛出NOR_ERROR
|
|
82
|
+
throw createLogicError('NOR_ERROR', fulfilledCount, total, results);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
static xnor(iterable) {
|
|
88
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
89
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
90
|
+
const fulfilledCount = fulfilled.length;
|
|
91
|
+
const total = results.length;
|
|
92
|
+
|
|
93
|
+
if (fulfilledCount === 0 || fulfilledCount === total) {
|
|
94
|
+
// 全部成功或全部失败,返回成功的值数组
|
|
95
|
+
return fulfilled.map(result => result.value);
|
|
96
|
+
} else {
|
|
97
|
+
// 部分成功部分失败,抛出XNOR_ERROR
|
|
98
|
+
throw createLogicError('XNOR_ERROR', fulfilledCount, total, results);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
static majority(iterable) {
|
|
104
|
+
return Promise.allSettled(iterable).then((results) => {
|
|
105
|
+
const fulfilled = results.filter(result => result.status === 'fulfilled');
|
|
106
|
+
const fulfilledCount = fulfilled.length;
|
|
107
|
+
const total = results.length;
|
|
108
|
+
|
|
109
|
+
// 多数逻辑:成功数 > 失败数
|
|
110
|
+
if (fulfilledCount > total - fulfilledCount) {
|
|
111
|
+
// 超过半数成功,返回成功的值数组
|
|
112
|
+
return fulfilled.map(result => result.value);
|
|
113
|
+
} else {
|
|
114
|
+
// 未达到多数,抛出MAJORITY_ERROR
|
|
115
|
+
throw createLogicError('MAJORITY_ERROR', fulfilledCount, total, results);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
static createFlipFlop(initialState = false) {
|
|
121
|
+
let state = initialState;
|
|
122
|
+
let resolveCurrent = null;
|
|
123
|
+
let currentPromise = Promise.resolve(state);
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
// 获取当前状态
|
|
127
|
+
getState() {
|
|
128
|
+
return state;
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
// 异步设置状态
|
|
132
|
+
set(newState) {
|
|
133
|
+
state = newState;
|
|
134
|
+
if (resolveCurrent) {
|
|
135
|
+
resolveCurrent(state);
|
|
136
|
+
resolveCurrent = null;
|
|
137
|
+
}
|
|
138
|
+
currentPromise = Promise.resolve(state);
|
|
139
|
+
return this;
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
// 切换状态
|
|
143
|
+
toggle() {
|
|
144
|
+
return this.set(!state);
|
|
145
|
+
},
|
|
146
|
+
|
|
147
|
+
// 等待状态变化
|
|
148
|
+
waitForChange() {
|
|
149
|
+
if (!resolveCurrent) {
|
|
150
|
+
currentPromise = new Promise((resolve) => {
|
|
151
|
+
resolveCurrent = resolve;
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
return currentPromise;
|
|
155
|
+
},
|
|
156
|
+
|
|
157
|
+
// 等待特定状态
|
|
158
|
+
waitFor(targetState) {
|
|
159
|
+
if (state === targetState) {
|
|
160
|
+
return Promise.resolve(state);
|
|
161
|
+
}
|
|
162
|
+
return new Promise((resolve) => {
|
|
163
|
+
const checkState = () => {
|
|
164
|
+
if (state === targetState) {
|
|
165
|
+
resolve(state);
|
|
166
|
+
} else {
|
|
167
|
+
this.waitForChange().then(checkState);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
checkState();
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function createPromiseLogic(options = {}) {
|
|
178
|
+
const { prefix = '', suffix = '', rename = {} } = options;
|
|
179
|
+
|
|
180
|
+
// 基础方法映射
|
|
181
|
+
const methods = {
|
|
182
|
+
and: PromiseLogic.and,
|
|
183
|
+
or: PromiseLogic.or,
|
|
184
|
+
race: PromiseLogic.race,
|
|
185
|
+
allSettled: PromiseLogic.allSettled,
|
|
186
|
+
xor: PromiseLogic.xor,
|
|
187
|
+
nand: PromiseLogic.nand,
|
|
188
|
+
nor: PromiseLogic.nor,
|
|
189
|
+
xnor: PromiseLogic.xnor,
|
|
190
|
+
majority: PromiseLogic.majority
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
// 应用命名转换
|
|
194
|
+
const result = {};
|
|
195
|
+
Object.entries(methods).forEach(([key, fn]) => {
|
|
196
|
+
// 优先使用rename映射,然后应用prefix和suffix
|
|
197
|
+
const baseName = rename[key] || key;
|
|
198
|
+
const finalName = `${prefix}${baseName}${suffix}`;
|
|
199
|
+
result[finalName] = fn;
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
return result;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export { PromiseLogic, PromiseLogicError, createLogicError, createPromiseLogic };
|
|
206
|
+
//# sourceMappingURL=index.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/utils/errors.js","../src/PromiseLogic.js","../src/factory.js"],"sourcesContent":["export class PromiseLogicError extends Error {\r\n constructor(type, message, results) {\r\n super(message);\r\n this.name = 'PromiseLogicError';\r\n this.type = type; // 'XOR_ERROR', 'NAND_ERROR', etc.\r\n this.results = results; // settlement results of all Promises\r\n }\r\n}\r\n\r\n// Error factory function\r\nexport function createLogicError(type, fulfilledCount, total, results) {\r\n const messages = {\r\n XOR_ERROR: `XOR condition failed: expected exactly 1 promise to fulfill, but ${fulfilledCount} fulfilled.`,\r\n NAND_ERROR: `NAND condition failed: all ${total} promises fulfilled (expected at least one rejection).`,\r\n NOR_ERROR: `NOR condition failed: ${fulfilledCount} promises fulfilled (expected all rejected).`,\r\n MAJORITY_ERROR: `Majority condition failed: ${fulfilledCount}/${total} fulfilled (need majority).`\r\n };\r\n \r\n return new PromiseLogicError(type, messages[type] || 'Logic condition failed', results);\r\n}","import { createLogicError } from './utils/errors.js';\r\n\r\nexport class PromiseLogic {\r\n static and(iterable) {\r\n return Promise.all(iterable);\r\n }\r\n\r\n static or(iterable) {\r\n return Promise.any(iterable);\r\n }\r\n\r\n static race(iterable) {\r\n return Promise.race(iterable);\r\n }\r\n\r\n static allSettled(iterable) {\r\n return Promise.allSettled(iterable);\r\n }\r\n\r\n static xor(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n if (fulfilledCount === 1) {\r\n // 恰好一个成功,返回成功的值\r\n return fulfilled[0].value;\r\n } else {\r\n // 0个或多个(>1)成功,抛出XOR_ERROR\r\n throw createLogicError('XOR_ERROR', fulfilledCount, total, results);\r\n }\r\n });\r\n }\r\n\r\n static nand(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n if (fulfilledCount === total) {\r\n // 全部成功,抛出NAND_ERROR\r\n throw createLogicError('NAND_ERROR', fulfilledCount, total, results);\r\n } else {\r\n // 不是所有都成功,返回成功的值数组\r\n return fulfilled.map(result => result.value);\r\n }\r\n });\r\n }\r\n\r\n static nor(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n if (fulfilledCount === 0) {\r\n // 全部失败,返回空数组表示成功\r\n return [];\r\n } else {\r\n // 任意成功,抛出NOR_ERROR\r\n throw createLogicError('NOR_ERROR', fulfilledCount, total, results);\r\n }\r\n });\r\n }\r\n\r\n static xnor(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n if (fulfilledCount === 0 || fulfilledCount === total) {\r\n // 全部成功或全部失败,返回成功的值数组\r\n return fulfilled.map(result => result.value);\r\n } else {\r\n // 部分成功部分失败,抛出XNOR_ERROR\r\n throw createLogicError('XNOR_ERROR', fulfilledCount, total, results);\r\n }\r\n });\r\n }\r\n\r\n static majority(iterable) {\r\n return Promise.allSettled(iterable).then((results) => {\r\n const fulfilled = results.filter(result => result.status === 'fulfilled');\r\n const fulfilledCount = fulfilled.length;\r\n const total = results.length;\r\n \r\n // 多数逻辑:成功数 > 失败数\r\n if (fulfilledCount > total - fulfilledCount) {\r\n // 超过半数成功,返回成功的值数组\r\n return fulfilled.map(result => result.value);\r\n } else {\r\n // 未达到多数,抛出MAJORITY_ERROR\r\n throw createLogicError('MAJORITY_ERROR', fulfilledCount, total, results);\r\n }\r\n });\r\n }\r\n\r\n static createFlipFlop(initialState = false) {\r\n let state = initialState;\r\n let resolveCurrent = null;\r\n let currentPromise = Promise.resolve(state);\r\n \r\n return {\r\n // 获取当前状态\r\n getState() {\r\n return state;\r\n },\r\n \r\n // 异步设置状态\r\n set(newState) {\r\n state = newState;\r\n if (resolveCurrent) {\r\n resolveCurrent(state);\r\n resolveCurrent = null;\r\n }\r\n currentPromise = Promise.resolve(state);\r\n return this;\r\n },\r\n \r\n // 切换状态\r\n toggle() {\r\n return this.set(!state);\r\n },\r\n \r\n // 等待状态变化\r\n waitForChange() {\r\n if (!resolveCurrent) {\r\n currentPromise = new Promise((resolve) => {\r\n resolveCurrent = resolve;\r\n });\r\n }\r\n return currentPromise;\r\n },\r\n \r\n // 等待特定状态\r\n waitFor(targetState) {\r\n if (state === targetState) {\r\n return Promise.resolve(state);\r\n }\r\n return new Promise((resolve) => {\r\n const checkState = () => {\r\n if (state === targetState) {\r\n resolve(state);\r\n } else {\r\n this.waitForChange().then(checkState);\r\n }\r\n };\r\n checkState();\r\n });\r\n }\r\n };\r\n }\r\n}","import { PromiseLogic } from './PromiseLogic.js';\n\nexport function createPromiseLogic(options = {}) {\n const { prefix = '', suffix = '', rename = {} } = options;\n \n // 基础方法映射\n const methods = {\n and: PromiseLogic.and,\n or: PromiseLogic.or,\n race: PromiseLogic.race,\n allSettled: PromiseLogic.allSettled,\n xor: PromiseLogic.xor,\n nand: PromiseLogic.nand,\n nor: PromiseLogic.nor,\n xnor: PromiseLogic.xnor,\n majority: PromiseLogic.majority\n };\n \n // 应用命名转换\n const result = {};\n Object.entries(methods).forEach(([key, fn]) => {\n // 优先使用rename映射,然后应用prefix和suffix\n const baseName = rename[key] || key;\n const finalName = `${prefix}${baseName}${suffix}`;\n result[finalName] = fn;\n });\n \n return result;\n}"],"names":[],"mappings":"AAAO,MAAM,iBAAiB,SAAS,KAAK,CAAC;AAC7C,EAAE,WAAW,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE;AACtC,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;AACnB,IAAI,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;AACpC,IAAI,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;AACrB,IAAI,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;AAC3B,EAAE,CAAC;AACH,CAAC;AACD;AACA;AACO,SAAS,gBAAgB,CAAC,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,OAAO,EAAE;AACvE,EAAE,MAAM,QAAQ,GAAG;AACnB,IAAI,SAAS,EAAE,CAAC,iEAAiE,EAAE,cAAc,CAAC,WAAW,CAAC;AAC9G,IAAI,UAAU,EAAE,CAAC,2BAA2B,EAAE,KAAK,CAAC,sDAAsD,CAAC;AAC3G,IAAI,SAAS,EAAE,CAAC,sBAAsB,EAAE,cAAc,CAAC,4CAA4C,CAAC;AACpG,IAAI,cAAc,EAAE,CAAC,2BAA2B,EAAE,cAAc,CAAC,CAAC,EAAE,KAAK,CAAC,2BAA2B,CAAC;AACtG,GAAG,CAAC;AACJ;AACA,E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3B,IAAI,QAAQ,EAAE,YAAY,CAAC;AAC3B,GAAG;AACH;AACA;AACA,EAAE,MAAM,MAAM,GAAG,EAAE;AACnB,EAAE,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,KAAK;AACjD;AACA,IAAI,MAAM,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAC,IAAI,GAAG;AACvC,IAAI,MAAM,SAAS,GAAG,CAAC,EAAE,MAAM,CAAC,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,CAAC;AACrD,IAAI,MAAM,CAAC,SAAS,CAAC,GAAG,EAAE;AAC1B,EAAE,CAAC,CAAC;AACJ;AACA,EAAE,OAAO,MAAM;AACf;;;;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "promise-logic",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Compose promises with logic gate semantics (AND, OR, XOR, NAND, NOR, XNOR, Majority). Forget APIs, remember logic.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.cjs.js",
|
|
7
|
+
"module": "dist/index.esm.js",
|
|
8
|
+
"types": "dist/types/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.esm.js",
|
|
12
|
+
"require": "./dist/index.cjs.js",
|
|
13
|
+
"types": "./dist/types/index.d.ts"
|
|
14
|
+
},
|
|
15
|
+
"./factory": {
|
|
16
|
+
"import": "./dist/factory.esm.js",
|
|
17
|
+
"require": "./dist/factory.cjs.js",
|
|
18
|
+
"types": "./dist/types/factory.d.ts"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "rollup -c",
|
|
26
|
+
"dev": "rollup -c -w",
|
|
27
|
+
"test": "jest --config jest.config.cjs",
|
|
28
|
+
"test:watch": "jest --watch --config jest.config.cjs",
|
|
29
|
+
"test:coverage": "jest --coverage --config jest.config.cjs",
|
|
30
|
+
"lint": "eslint src test",
|
|
31
|
+
"lint:fix": "eslint src test --fix",
|
|
32
|
+
"format": "prettier --write \"src/**/*.js\" \"test/**/*.js\"",
|
|
33
|
+
"prepublishOnly": "npm run build && npm test",
|
|
34
|
+
"prepare": "npm run build"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"promise",
|
|
38
|
+
"logic",
|
|
39
|
+
"async",
|
|
40
|
+
"logic-gates",
|
|
41
|
+
"xor",
|
|
42
|
+
"nand",
|
|
43
|
+
"nor",
|
|
44
|
+
"xnor",
|
|
45
|
+
"majority",
|
|
46
|
+
"combinational",
|
|
47
|
+
"async-programming",
|
|
48
|
+
"concurrency"
|
|
49
|
+
],
|
|
50
|
+
"author": {
|
|
51
|
+
"name": "再乔乔我一下 (Let me hug you again)",
|
|
52
|
+
"email": "2820387220@qq.com"
|
|
53
|
+
},
|
|
54
|
+
"license": "MIT",
|
|
55
|
+
"repository": {
|
|
56
|
+
"type": "git",
|
|
57
|
+
"url": "https://github.com/xier123456/promise-logic.git"
|
|
58
|
+
},
|
|
59
|
+
"bugs": {
|
|
60
|
+
"url": "https://github.com/xier123456/promise-logic.git/issues"
|
|
61
|
+
},
|
|
62
|
+
"homepage": "https://github.com/xier123456/promise-logic/docs/promise_logic",
|
|
63
|
+
"engines": {
|
|
64
|
+
"node": ">=14.0.0"
|
|
65
|
+
},
|
|
66
|
+
"sideEffects": false,
|
|
67
|
+
"devDependencies": {
|
|
68
|
+
"@babel/core": "^7.28.6",
|
|
69
|
+
"@babel/preset-env": "^7.28.6",
|
|
70
|
+
"@rollup/plugin-commonjs": "^25.0.0",
|
|
71
|
+
"@rollup/plugin-node-resolve": "^15.0.0",
|
|
72
|
+
"@types/jest": "^30.0.0",
|
|
73
|
+
"babel-jest": "^30.2.0",
|
|
74
|
+
"eslint": "^8.0.0",
|
|
75
|
+
"jest": "^29.0.0",
|
|
76
|
+
"jest-environment-jsdom": "^30.2.0",
|
|
77
|
+
"prettier": "^3.0.0",
|
|
78
|
+
"rollup": "^4.0.0"
|
|
79
|
+
},
|
|
80
|
+
"dependencies": {
|
|
81
|
+
"@monaco-editor/react": "^4.7.0",
|
|
82
|
+
"clsx": "^2.1.1",
|
|
83
|
+
"tailwind-merge": "^3.4.0"
|
|
84
|
+
}
|
|
85
|
+
}
|