@mantaq/sugar 2.0.0 → 2.0.2
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 +30 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -0
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -387,6 +387,36 @@ withPromise(fetchData(), input.signal, input.emit, {
|
|
|
387
387
|
});
|
|
388
388
|
```
|
|
389
389
|
|
|
390
|
+
#### Errors as values
|
|
391
|
+
|
|
392
|
+
When a port returns `Promise<Either<PortError, Loaded>>`, map the result with `Either.match` inside `success`. No new helper is needed, and both branches stay typed without a cast:
|
|
393
|
+
|
|
394
|
+
```ts
|
|
395
|
+
import { Either } from "@mantaq/utils";
|
|
396
|
+
import { withPromise } from "@mantaq/sugar";
|
|
397
|
+
|
|
398
|
+
m.effect(loading, {
|
|
399
|
+
name: "load",
|
|
400
|
+
sends: [loaded, failed, loadRejected],
|
|
401
|
+
fn: (input) =>
|
|
402
|
+
withPromise({
|
|
403
|
+
promise: port.load(),
|
|
404
|
+
signal: input.signal,
|
|
405
|
+
emit: input.emit,
|
|
406
|
+
events: {
|
|
407
|
+
success: (result) =>
|
|
408
|
+
Either.match(result, {
|
|
409
|
+
onLeft: (e): LoadEvent => failed.create(e), // e: PortError
|
|
410
|
+
onRight: (v): LoadEvent => loaded.create(v), // v: Loaded
|
|
411
|
+
}),
|
|
412
|
+
error: (err) => loadRejected.create({ message: String(err) }),
|
|
413
|
+
},
|
|
414
|
+
}),
|
|
415
|
+
});
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
`LoadEvent` is the union of the two event shapes; annotating both handlers with it lets `Either.match` infer one result type. A rejected promise still goes through `error`, and an effect aborted before the promise settles emits nothing. See `src/effects/promise.either.test.ts`.
|
|
419
|
+
|
|
390
420
|
### `withTimeout(ms, input, eventFn)`
|
|
391
421
|
|
|
392
422
|
Schedule a timeout event through the actor's clock. Aborts cleanly if the actor is destroyed before timeout fires.
|
package/dist/index.d.mts
CHANGED
|
@@ -99,7 +99,7 @@ interface WithPromiseOptions<T, Emit = AnyEvent> {
|
|
|
99
99
|
};
|
|
100
100
|
}
|
|
101
101
|
export declare function withPromise<T, Emit = AnyEvent>(options: WithPromiseOptions<T, Emit>): Promise<void>;
|
|
102
|
-
export declare function withTimeout<ActorContext, Payload = unknown, Emit = InternalEvent>(durationMs: number, options: {
|
|
102
|
+
export declare function withTimeout<ActorContext, Payload = unknown, Emit extends InternalEvent = InternalEvent>(durationMs: number, options: {
|
|
103
103
|
input: EffectInput<ActorContext, Payload, Emit>;
|
|
104
104
|
event: () => NoInfer<Emit>;
|
|
105
105
|
}): void;
|
package/dist/index.mjs
CHANGED
|
@@ -154,6 +154,7 @@ function withTimeout(durationMs, options) {
|
|
|
154
154
|
const { input, event } = options;
|
|
155
155
|
input.clock.setTimeout(durationMs, {
|
|
156
156
|
signal: input.signal,
|
|
157
|
+
eventName: event().type,
|
|
157
158
|
cb: () => {
|
|
158
159
|
if (input.signal.aborted) return;
|
|
159
160
|
input.emit(event());
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mantaq/sugar",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "Userland sugar for mantaq actors — matches, effect helpers, transition utilities.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"access": "public"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@mantaq/core": "0.5.
|
|
23
|
+
"@mantaq/core": "0.5.2"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@stryker-mutator/core": "^10",
|
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
"vite": "npm:@voidzero-dev/vite-plus-core@1.4.0",
|
|
33
33
|
"vite-plus": "1.4.0",
|
|
34
34
|
"vitest": "5.0.5",
|
|
35
|
-
"@mantaq/pbt": "0.0.3"
|
|
35
|
+
"@mantaq/pbt": "0.0.3",
|
|
36
|
+
"@mantaq/utils": "0.0.4"
|
|
36
37
|
},
|
|
37
38
|
"scripts": {
|
|
38
39
|
"build": "vp pack",
|