Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$mol_wire_async, $mol_wire_sync simplify types #640

Merged
merged 8 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 )

}
Loading