indisposed 0.0.7 → 0.1.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 +106 -1
- package/dist/explicit-resource-management-BSyjFgS-.js +837 -0
- package/dist/{explicit-resource-management-0TNUBRTo.js.map → explicit-resource-management-BSyjFgS-.js.map} +1 -1
- package/dist/functions/channel.d.ts +82 -0
- package/dist/functions/channel.d.ts.map +1 -0
- package/dist/functions/handlers.d.ts +7 -4
- package/dist/functions/handlers.d.ts.map +1 -1
- package/dist/functions/timing.d.ts +91 -0
- package/dist/functions/timing.d.ts.map +1 -0
- package/dist/index.d.ts +1 -2
- package/dist/index.js +17 -12
- package/dist/index.js.map +1 -1
- package/dist/no-polyfill.d.ts +5 -3
- package/dist/no-polyfill.d.ts.map +1 -1
- package/dist/no-polyfill.js +139 -58
- package/dist/no-polyfill.js.map +1 -1
- package/dist/path-DtH12Oyl.js +1398 -0
- package/dist/{path-CAJHNV4f.js.map → path-DtH12Oyl.js.map} +1 -1
- package/dist/with-resolvers-0tPpPN4C.js +1243 -0
- package/dist/{with-resolvers-DDdeEz5R.js.map → with-resolvers-0tPpPN4C.js.map} +1 -1
- package/package.json +8 -9
- package/dist/explicit-resource-management-0TNUBRTo.js +0 -448
- package/dist/path-CAJHNV4f.js +0 -918
- package/dist/with-resolvers-DDdeEz5R.js +0 -715
package/README.md
CHANGED
|
@@ -10,6 +10,8 @@ The missing utilities for JavaScript's [Explicit Resource Management](https://gi
|
|
|
10
10
|
|
|
11
11
|
- 🧹 **Resource Management** - Convert any resource into a disposable with `toDisposable` and `toAsyncDisposable`
|
|
12
12
|
- 🎧 **Event Handlers** - Transform event emitters into disposable iterators with `on` and promises with `once`
|
|
13
|
+
- ⏱️ **Timing Utilities** - Disposable `timeout` and `interval` for clean timer management
|
|
14
|
+
- 📡 **Channels** - Build async iterators from push-based sources with `channel`
|
|
13
15
|
- 🔒 **Scoped Execution** - Execute code with automatic cleanup using `invoke`
|
|
14
16
|
- 📦 **Zero Dependencies** - Lightweight and focused
|
|
15
17
|
- 🔧 **TypeScript First** - Full type safety and inference
|
|
@@ -133,7 +135,7 @@ import { once } from "indisposed";
|
|
|
133
135
|
}
|
|
134
136
|
```
|
|
135
137
|
|
|
136
|
-
### `on(emitter, event,
|
|
138
|
+
### `on(emitter, event, options?)`
|
|
137
139
|
|
|
138
140
|
Create a disposable async iterator for multiple events.
|
|
139
141
|
|
|
@@ -164,6 +166,109 @@ import { on } from "indisposed";
|
|
|
164
166
|
console.log(`Position: ${x}, ${y}`);
|
|
165
167
|
}
|
|
166
168
|
}
|
|
169
|
+
|
|
170
|
+
// With buffer options
|
|
171
|
+
{
|
|
172
|
+
using events = on(emitter, "data", { maxBuffer: 10, drain: true });
|
|
173
|
+
// ...
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### `timeout(ms)`
|
|
178
|
+
|
|
179
|
+
Create a disposable promise that resolves after a delay.
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
import { timeout } from "indisposed";
|
|
183
|
+
|
|
184
|
+
// Basic usage
|
|
185
|
+
await timeout(1000);
|
|
186
|
+
console.log("1 second passed");
|
|
187
|
+
|
|
188
|
+
// With using - automatically clears timeout when scope exits
|
|
189
|
+
{
|
|
190
|
+
using timer = timeout(5000);
|
|
191
|
+
await timer;
|
|
192
|
+
} // timeout cleared if scope exits early
|
|
193
|
+
|
|
194
|
+
// Racing with other promises
|
|
195
|
+
{
|
|
196
|
+
using timer = timeout(10000);
|
|
197
|
+
using data = once(socket, "data");
|
|
198
|
+
|
|
199
|
+
await Promise.race([timer, data]);
|
|
200
|
+
} // both cleaned up regardless of which wins
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### `interval(ms, options?)`
|
|
204
|
+
|
|
205
|
+
Create a disposable async iterator that yields incrementing numbers at a fixed interval.
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
import { interval } from "indisposed";
|
|
209
|
+
|
|
210
|
+
// Basic usage - tick every second
|
|
211
|
+
{
|
|
212
|
+
using ticks = interval(1000);
|
|
213
|
+
|
|
214
|
+
for await (const tick of ticks) {
|
|
215
|
+
console.log(`Tick ${tick}`); // 0, 1, 2, ...
|
|
216
|
+
if (tick >= 5) break;
|
|
217
|
+
}
|
|
218
|
+
} // interval automatically cleared
|
|
219
|
+
|
|
220
|
+
// Polling pattern
|
|
221
|
+
{
|
|
222
|
+
using poll = interval(5000);
|
|
223
|
+
|
|
224
|
+
for await (const _ of poll) {
|
|
225
|
+
const status = await checkStatus();
|
|
226
|
+
if (status === "complete") break;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// With options
|
|
231
|
+
const ticks = interval(100, { maxBuffer: 10, drain: true });
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
### `channel<T>(options?)`
|
|
235
|
+
|
|
236
|
+
Create a buffered async channel for pushing values and consuming them via async iteration.
|
|
237
|
+
|
|
238
|
+
This is a low-level primitive for building async iterators from push-based sources.
|
|
239
|
+
The channel separates producer (`push`) and consumer (`iterator`) concerns - only expose
|
|
240
|
+
the `iterator` to downstream code.
|
|
241
|
+
|
|
242
|
+
**Options:**
|
|
243
|
+
|
|
244
|
+
- `maxBuffer` - Maximum events to buffer (default: 100). Set to 0 for no buffering.
|
|
245
|
+
- `drain` - Whether to drain buffered events on dispose (default: false)
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
import { channel } from "indisposed";
|
|
249
|
+
|
|
250
|
+
// Basic usage - producer keeps the channel, consumer gets the iterator
|
|
251
|
+
const ch = channel<string>();
|
|
252
|
+
|
|
253
|
+
// Producer side
|
|
254
|
+
ch.push("hello");
|
|
255
|
+
ch.push("world");
|
|
256
|
+
|
|
257
|
+
// Consumer side - only sees the iterator
|
|
258
|
+
{
|
|
259
|
+
using iter = ch.iterator;
|
|
260
|
+
for await (const value of iter) {
|
|
261
|
+
console.log(value);
|
|
262
|
+
if (shouldStop) break;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Building a custom async source
|
|
267
|
+
function createDataStream() {
|
|
268
|
+
const ch = channel<Data>();
|
|
269
|
+
source.on("data", (d) => ch.push(d));
|
|
270
|
+
return ch.iterator; // Only expose the iterator
|
|
271
|
+
}
|
|
167
272
|
```
|
|
168
273
|
|
|
169
274
|
### `invoke<T>(fn)`
|