File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { describe , expect , test } from 'vitest' ;
2+ import { DispatchApi , useDispatch } from './dispatch.ts' ;
3+ import { createHost } from './host.ts' ;
4+
5+ const { dispatch } = DispatchApi . methods ;
6+
7+ describe ( 'dispatch' , ( ) => {
8+ test ( 'catches errors thrown by middleware installed on the host' , ( ) => {
9+ const host = createHost ( ) ;
10+ const reason = new Error ( 'dispatch failed' ) ;
11+ useDispatch ( host ) ;
12+ DispatchApi . around ( host . root , {
13+ dispatch ( ) {
14+ throw reason ;
15+ } ,
16+ } ) ;
17+
18+ expect ( dispatch ( host . root , { } ) ) . toEqual ( { ok : false , reason } ) ;
19+ } ) ;
20+ } ) ;
Original file line number Diff line number Diff line change 1+ import { createApi } from './core.ts' ;
2+ import type { Host } from './host.ts' ;
3+
4+ export const DispatchApi = createApi ( 'dispatch' , {
5+ dispatch ( _node , _event : unknown ) : Result {
6+ return Ok ;
7+ } ,
8+ } ) ;
9+
10+ export function useDispatch ( host : Host ) : void {
11+ DispatchApi . around ( host . root , {
12+ dispatch ( args , next ) {
13+ try {
14+ return next ( ...args ) ;
15+ } catch ( reason ) {
16+ return { ok : false , reason } ;
17+ }
18+ } ,
19+ } ) ;
20+ }
21+
22+ export type Result =
23+ | typeof Ok
24+ | {
25+ ok : false ;
26+ reason : unknown ;
27+ } ;
28+
29+ const Ok = { ok : true } as const ;
Original file line number Diff line number Diff line change 11export { rgba } from '@bomb.sh/tty' ;
2+ export { DispatchApi } from './dispatch.ts' ;
23export { box , type BoxProps } from './components/box.ts' ;
34export { text , type TextProps } from './components/text.ts' ;
45export { layout , LayoutApi } from './layout.ts' ;
You can’t perform that action at this time.
0 commit comments