Skip to content

Commit

Permalink
Merge pull request #640 from hyoo-ru/wire-types
Browse files Browse the repository at this point in the history
$mol_wire_async, $mol_wire_sync simplify types
  • Loading branch information
zerkalica authored Oct 4, 2023
2 parents 86545ee + b892723 commit 861d272
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 27 deletions.
5 changes: 3 additions & 2 deletions build/server/server.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ namespace $ {

expressGenerator() {
const self = $mol_wire_async( this )
return function( ... args: any[] ) {
return self.handleRequest.apply( self, args )

return function( req : any , res : any , next : () => void ) {
return self.handleRequest.call( self, req, res, next )
}
}

Expand Down
12 changes: 12 additions & 0 deletions wire/async/async.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
namespace $ {

$mol_test({
'test types'( $ ) {
class A {
static a() {
return ''
}
static b() {
return $mol_wire_async(this).a()
}
}

type Check = $mol_type_assert<ReturnType<typeof A['b']>, Promise<string>>
},

async 'Latest method calls wins'( $ ) {

Expand Down
28 changes: 14 additions & 14 deletions wire/async/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ namespace $ {
return fiber.async()
},

} ) as any as (
Host extends ( ... args: infer Args )=> infer Res
? Res extends Promise<any>
? Host
: ( ... args: Args )=> Promise< Res >
: {}
) & {
[ key in keyof Host ]: Host[ key ] extends ( ... args: infer Args )=> infer Res
? Res extends Promise<any>
? Host[ key ]
: ( ... args: Args )=> Promise< Res >
: Host[ key ]
}
} ) as unknown as ObjectOrFunctionResultPromisify<Host>

}


type FunctionResultPromisify<Some> = Some extends (...args: infer Args) => infer Res
? Res extends PromiseLike<unknown> ? Some : (...args: Args) => Promise<Res>
: Some

type MethodsResultPromisify<Host extends Object> = {
[K in keyof Host]: FunctionResultPromisify<Host[K]>
}

type ObjectOrFunctionResultPromisify<Some> = (
Some extends (...args: any) => unknown ? FunctionResultPromisify<Some> : {}
) & ( Some extends Object ? MethodsResultPromisify<Some> : Some )

}
21 changes: 21 additions & 0 deletions wire/sync/sync.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace $ {

$mol_test({

'test types'( $ ) {
class A {
static a() {
return Promise.resolve('')
}

static b() {
return $mol_wire_sync(this).a()
}
}

type Check = $mol_type_assert<ReturnType<typeof A['b']>, string>
},
})

}

24 changes: 13 additions & 11 deletions wire/sync/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ namespace $ {
return fiber.sync()
},

} ) as any as (
Host extends ( ... args: infer Args )=> infer Res
? Res extends Promise< infer Res2 >
? ( ... args: Args )=> Res2
: Host
: {}
) & {
[ key in keyof Host ]: Host[ key ] extends ( ... args: infer Args )=> Promise< infer Res >
? ( ... args: Args )=> Res
: Host[ key ]
}
} ) as unknown as ObjectOrFunctionResultAwaited<Host>
}

type FunctionResultAwaited<Some> = Some extends (...args: infer Args) => infer Res
? (...args: Args) => Awaited<Res>
: Some

type MethodsResultAwaited<Host extends Object> = {
[K in keyof Host]: FunctionResultAwaited<Host[K]>
}

type ObjectOrFunctionResultAwaited<Some> = (
Some extends (...args: any) => unknown ? FunctionResultAwaited<Some> : {}
) & ( Some extends Object ? MethodsResultAwaited<Some> : Some )

}

0 comments on commit 861d272

Please sign in to comment.