Skip to content

Commit 07fecb0

Browse files
committed
updated docs
1 parent 18b7498 commit 07fecb0

9 files changed

Lines changed: 861 additions & 4406 deletions

File tree

docs/.context.md

Lines changed: 404 additions & 0 deletions
Large diffs are not rendered by default.

docs/Context.md

Lines changed: 0 additions & 3740 deletions
This file was deleted.

docs/Events.md

Lines changed: 72 additions & 215 deletions
Large diffs are not rendered by default.

docs/Exception.md

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Exception
22

3-
Enhanced error handling with expressive error reporting and stack trace support.
3+
Enhanced error handling with expressive error reporting and stack trace support. Provides better error information than standard JavaScript errors with template-based error messages, validation error aggregation, enhanced stack trace parsing, and HTTP status code integration.
44

55
```typescript
66
const exception = new Exception('Invalid Parameters: %s', 400)
@@ -11,13 +11,13 @@ const exception = new Exception('Invalid Parameters: %s', 400)
1111
.withPosition(100, 200);
1212
```
1313

14-
## Static Methods
14+
## 1. Static Methods
1515

16-
The following methods can be accessed directly from Exception itself.
16+
The following methods can be accessed directly from Exception itself. These factory methods provide convenient ways to create Exception instances for common error scenarios without needing to instantiate the class directly.
1717

18-
### Creating Exceptions with Templates
18+
### 1.1. Creating Exceptions with Templates
1919

20-
The following example shows how to create exceptions with template strings.
20+
The following example shows how to create exceptions with template strings using placeholder substitution.
2121

2222
```typescript
2323
throw Exception.for('Something %s is %s', 'good', 'bad');
@@ -35,9 +35,9 @@ throw Exception.for('Something %s is %s', 'good', 'bad');
3535

3636
A new Exception instance with the formatted message.
3737

38-
### Creating Exceptions from Response Objects
38+
### 1.2. Creating Exceptions from Response Objects
3939

40-
The following example shows how to create exceptions from response objects.
40+
The following example shows how to create exceptions from response objects containing error details.
4141

4242
```typescript
4343
const response = {
@@ -59,9 +59,9 @@ throw Exception.forResponse(response);
5959

6060
A new Exception instance configured from the response object.
6161

62-
### Creating Exceptions for Validation Errors
62+
### 1.3. Creating Exceptions for Validation Errors
6363

64-
The following example shows how to create exceptions for validation errors.
64+
The following example shows how to create exceptions specifically for validation errors with field-specific error messages.
6565

6666
```typescript
6767
throw Exception.forErrors({
@@ -80,9 +80,9 @@ throw Exception.forErrors({
8080

8181
A new Exception instance with "Invalid Parameters" message and error details.
8282

83-
### Requiring Conditions
83+
### 1.4. Requiring Conditions
8484

85-
The following example shows how to assert conditions and throw if they fail.
85+
The following example shows how to assert conditions and throw exceptions if they fail.
8686

8787
```typescript
8888
Exception.require(count > 0, 'Count %s must be positive', count);
@@ -100,9 +100,9 @@ Exception.require(count > 0, 'Count %s must be positive', count);
100100

101101
Void if condition is true, throws Exception if false.
102102

103-
### Try-Catch Wrapper
103+
### 1.5. Try-Catch Wrapper
104104

105-
The following example shows how to use the synchronous try-catch wrapper.
105+
The following example shows how to use the synchronous try-catch wrapper for safe operation execution.
106106

107107
```typescript
108108
const result = Exception
@@ -123,9 +123,9 @@ const result = Exception
123123

124124
An object with a `catch` method for handling errors.
125125

126-
### Upgrading Errors
126+
### 1.6. Upgrading Errors
127127

128-
The following example shows how to upgrade regular errors to exceptions.
128+
The following example shows how to upgrade regular errors to exceptions with additional context.
129129

130130
```typescript
131131
try {
@@ -146,9 +146,9 @@ try {
146146

147147
An Exception instance (returns original if already an Exception).
148148

149-
## Properties
149+
## 2. Properties
150150

151-
The following properties are available when instantiating an Exception.
151+
The following properties are available when instantiating an Exception. These properties provide access to error details, HTTP status codes, and position information for debugging purposes.
152152

153153
| Property | Type | Description |
154154
|----------|------|-------------|
@@ -158,13 +158,13 @@ The following properties are available when instantiating an Exception.
158158
| `start` | `number` | Starting character position of the error |
159159
| `type` | `string` | Exception type name |
160160

161-
## Methods
161+
## 3. Methods
162162

163-
The following methods are available when instantiating an Exception.
163+
The following methods are available when instantiating an Exception. These methods allow you to configure the exception with additional context and convert it to various formats for different use cases.
164164

165-
### Setting Error Code
165+
### 3.1. Setting Error Code
166166

167-
The following example shows how to set the HTTP status code.
167+
The following example shows how to set the HTTP status code for the exception.
168168

169169
```typescript
170170
exception.withCode(404);
@@ -180,9 +180,9 @@ exception.withCode(404);
180180

181181
The Exception instance to allow method chaining.
182182

183-
### Adding Validation Errors
183+
### 3.2. Adding Validation Errors
184184

185-
The following example shows how to add validation errors.
185+
The following example shows how to add validation errors with field-specific error messages.
186186

187187
```typescript
188188
exception.withErrors({
@@ -201,9 +201,9 @@ exception.withErrors({
201201

202202
The Exception instance to allow method chaining.
203203

204-
### Setting Position Information
204+
### 3.3. Setting Position Information
205205

206-
The following example shows how to set character position information.
206+
The following example shows how to set character position information for parsing errors.
207207

208208
```typescript
209209
exception.withPosition(100, 200);
@@ -220,9 +220,9 @@ exception.withPosition(100, 200);
220220

221221
The Exception instance to allow method chaining.
222222

223-
### Converting to Response Object
223+
### 3.4. Converting to Response Object
224224

225-
The following example shows how to convert the exception to a response object.
225+
The following example shows how to convert the exception to a response object suitable for API responses.
226226

227227
```typescript
228228
const response = exception.toResponse();
@@ -240,9 +240,9 @@ const response = exception.toResponse();
240240

241241
An ErrorResponse object with all exception details.
242242

243-
### Converting to JSON
243+
### 3.5. Converting to JSON
244244

245-
The following example shows how to convert the exception to JSON.
245+
The following example shows how to convert the exception to a formatted JSON string.
246246

247247
```typescript
248248
const json = exception.toJSON();
@@ -253,9 +253,9 @@ console.log(json); // Pretty-printed JSON string
253253

254254
A formatted JSON string representation of the exception.
255255

256-
### Getting Stack Trace
256+
### 3.6. Getting Stack Trace
257257

258-
The following example shows how to get the parsed stack trace.
258+
The following example shows how to get the parsed stack trace with detailed frame information.
259259

260260
```typescript
261261
const trace = exception.trace();

docs/Queues.md

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1-
# Queue
1+
# Queues
22

3-
Priority-based queue implementations for managing items and tasks with FIFO ordering.
3+
Priority-based queue implementations for managing items and tasks with FIFO ordering. Includes both generic item queues and specialized task queues with priority-based ordering, FIFO ordering within same priority levels, task execution with before/after hooks, and chainable API for queue operations.
44

5-
When an event is triggered, Stackpress doesnt just fire off listeners blindly. Instead, it organizes them into a `TaskQueue`, which then consumes items sequentially by priority.
5+
When an event is triggered, Stackpress doesn't just fire off listeners blindly. Instead, it organizes them into a `TaskQueue`, which then consumes items sequentially by priority. Because events can be defined anywhere, event priority allows you to structure execution like a series of steps—making the flow of your application predictable and easy to follow.
66

7-
Because events can be defined anywhere, event priority allows you to structure execution like a series of steps—making the flow of your application predictable and easy to follow.
8-
9-
Even better, the `TaskQueue` makes the `EventEmitter` a true **plugin system**: you can insert new code between existing listeners without rewriting or restructuring what’s already there. This means features, extensions, or third-party modules can seamlessly “hook into” the event pipeline without breaking your core logic.
10-
11-
<img height="324" alt="image" src="https://github.com/user-attachments/assets/b313723b-618f-4911-8820-82ff8ab0998d" />
7+
Even better, the `TaskQueue` makes the `EventEmitter` a true **plugin system**: you can insert new code between existing listeners without rewriting or restructuring what's already there. This means features, extensions, or third-party modules can seamlessly "hook into" the event pipeline without breaking your core logic.
128

139
```typescript
1410
const itemQueue = new ItemQueue<string>();
1511
const taskQueue = new TaskQueue<[number]>();
1612
```
1713

18-
## ItemQueue
14+
## 1. ItemQueue
1915

20-
An item queue that orders and consumes items sequentially based on priority (FIFO by default).
16+
An item queue that orders and consumes items sequentially based on priority with FIFO ordering by default. The ItemQueue provides the foundation for priority-based processing where items with higher priority values are processed before items with lower priority values.
2117

22-
### Properties
18+
### 1.1. Properties
2319

2420
The following properties are available when instantiating an ItemQueue.
2521

@@ -28,13 +24,9 @@ The following properties are available when instantiating an ItemQueue.
2824
| `queue` | `Item<I>[]` | The internal queue array (readonly) |
2925
| `size` | `number` | The number of items in the queue |
3026

31-
### Methods
32-
33-
The following methods are available when instantiating an ItemQueue.
34-
35-
#### Adding Items with Priority
27+
### 1.2. Adding Items with Priority
3628

37-
The following example shows how to add items with specific priority levels.
29+
The following example shows how to add items with specific priority levels for controlled execution order.
3830

3931
```typescript
4032
const queue = new ItemQueue<string>();
@@ -54,9 +46,9 @@ queue.add('low', 1);
5446

5547
The ItemQueue instance to allow method chaining.
5648

57-
#### Adding Items to Bottom
49+
### 1.3. Adding Items to Bottom
5850

59-
The following example shows how to add items to the bottom of the queue (lowest priority).
51+
The following example shows how to add items to the bottom of the queue with the lowest priority.
6052

6153
```typescript
6254
queue.push('bottom-item');
@@ -72,9 +64,9 @@ queue.push('bottom-item');
7264

7365
The ItemQueue instance to allow method chaining.
7466

75-
#### Adding Items to Top
67+
### 1.4. Adding Items to Top
7668

77-
The following example shows how to add items to the top of the queue (highest priority).
69+
The following example shows how to add items to the top of the queue with the highest priority.
7870

7971
```typescript
8072
queue.shift('top-item');
@@ -90,7 +82,7 @@ queue.shift('top-item');
9082

9183
The ItemQueue instance to allow method chaining.
9284

93-
#### Consuming Items
85+
### 1.5. Consuming Items
9486

9587
The following example shows how to consume items one at a time in priority order.
9688

@@ -103,9 +95,9 @@ console.log(item); // 'top-item'
10395

10496
The next item in the queue, or `undefined` if the queue is empty.
10597

106-
## TaskQueue
98+
## 2. TaskQueue
10799

108-
A task queue that extends ItemQueue specifically for executing functions sequentially.
100+
A task queue that extends ItemQueue specifically for executing functions sequentially with priority-based ordering. The TaskQueue provides advanced features like before/after hooks, execution control, and status reporting for complex task management scenarios.
109101

110102
```typescript
111103
const queue = new TaskQueue<[number]>();
@@ -115,7 +107,7 @@ queue.add(async (x) => console.log(x + 3), 10);
115107
await queue.run(5);
116108
```
117109

118-
### Properties
110+
### 2.1. Properties
119111

120112
The following properties are available when instantiating a TaskQueue.
121113

@@ -126,13 +118,9 @@ The following properties are available when instantiating a TaskQueue.
126118
| `queue` | `Item<TaskAction<A>>[]` | The internal queue array (inherited) |
127119
| `size` | `number` | The number of tasks in the queue (inherited) |
128120

129-
### Methods
121+
### 2.2. Running Tasks
130122

131-
The following methods are available when instantiating a TaskQueue.
132-
133-
#### Running Tasks
134-
135-
The following example shows how to execute all tasks in the queue sequentially.
123+
The following example shows how to execute all tasks in the queue sequentially with proper error handling.
136124

137125
```typescript
138126
const queue = new TaskQueue<[number]>();
@@ -159,9 +147,9 @@ console.log(result.code); // 309 (ABORT) or 200 (OK)
159147

160148
A promise that resolves to a ResponseStatus indicating success, abort, or not found.
161149

162-
#### Adding Tasks with Priority
150+
### 2.3. Adding Tasks with Priority
163151

164-
The following example shows how to add tasks with specific priority levels.
152+
The following example shows how to add tasks with specific priority levels for controlled execution order.
165153

166154
```typescript
167155
queue.add(async (x) => console.log('high priority', x), 10);
@@ -178,9 +166,9 @@ queue.add(async (x) => console.log('high priority', x), 10);
178166

179167
The TaskQueue instance to allow method chaining.
180168

181-
#### Adding Tasks to Bottom
169+
### 2.4. Adding Tasks to Bottom
182170

183-
The following example shows how to add tasks to the bottom of the queue.
171+
The following example shows how to add tasks to the bottom of the queue with the lowest priority.
184172

185173
```typescript
186174
queue.push(async (x) => console.log('low priority', x));
@@ -196,9 +184,9 @@ queue.push(async (x) => console.log('low priority', x));
196184

197185
The TaskQueue instance to allow method chaining.
198186

199-
#### Adding Tasks to Top
187+
### 2.5. Adding Tasks to Top
200188

201-
The following example shows how to add tasks to the top of the queue.
189+
The following example shows how to add tasks to the top of the queue with the highest priority.
202190

203191
```typescript
204192
queue.shift(async (x) => console.log('high priority', x));
@@ -214,9 +202,9 @@ queue.shift(async (x) => console.log('high priority', x));
214202

215203
The TaskQueue instance to allow method chaining.
216204

217-
#### Setting Hooks
205+
### 2.6. Setting Hooks
218206

219-
The following example shows how to set before and after hooks for task execution.
207+
The following example shows how to set before and after hooks for task execution monitoring and control.
220208

221209
```typescript
222210
queue.before = async (x) => {
@@ -240,16 +228,34 @@ queue.after = async (x) => {
240228

241229
For hooks: `false` to abort execution, any other value to continue.
242230

243-
### Task Execution Flow
231+
## 3. Task Execution Flow
232+
233+
The following describes the sequential execution flow when running tasks in a TaskQueue. Understanding this flow is essential for implementing proper error handling and execution control in your applications.
234+
235+
### 3.1. Execution Steps
236+
237+
The task execution follows a predictable sequence that allows for monitoring and control at each stage.
238+
239+
1. **Before Hook** — Called before each task (if set)
240+
2. **Task Execution** — The actual task function is called
241+
3. **After Hook** — Called after each task (if set)
242+
243+
### 3.2. Execution Control
244+
245+
If any step returns `false`, execution is aborted and the queue returns `Status.ABORT`. This provides fine-grained control over task execution flow.
246+
247+
- **Continue Execution** — Return `true` or any truthy value
248+
- **Abort Execution** — Return `false` to stop processing remaining tasks
249+
250+
## 4. Status Codes
251+
252+
The following status codes are returned by TaskQueue operations to indicate the result of task execution. These codes follow HTTP status code conventions for consistency across the Stackpress ecosystem.
244253

245-
1. **Before Hook**: Called before each task (if set)
246-
2. **Task Execution**: The actual task function is called
247-
3. **After Hook**: Called after each task (if set)
254+
### 4.1. Success Codes
248255

249-
If any step returns `false`, execution is aborted and the queue returns `Status.ABORT`.
256+
- **Status.OK (200)** — All tasks completed successfully
257+
- **Status.NOT_FOUND (404)** — No tasks in the queue
250258

251-
### Status Codes
259+
### 4.2. Error Codes
252260

253-
- `Status.OK` (200): All tasks completed successfully
254-
- `Status.ABORT` (309): Execution was aborted by a task or hook
255-
- `Status.NOT_FOUND` (404): No tasks in the queue
261+
- **Status.ABORT (309)** — Execution was aborted by a task or hook

0 commit comments

Comments
 (0)