Skip to content

Commit

Permalink
$mol_wire_sync, $mol_wire_async types tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Oct 3, 2023
1 parent 1112030 commit 91f1088
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
15 changes: 15 additions & 0 deletions wire/async/async.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
namespace $ {

$mol_test({

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

const a = new A()
const b = a.b()
type Check = $mol_type_assert<typeof b, Promise<string>>
},

async 'Latest method calls wins'( $ ) {

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

} ) as any as ObjectOrFunctionResultPromisify<Host>
} ) 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 ObjectOrFunctionResultPromisify<Host>

}

Expand All @@ -43,7 +56,7 @@ namespace $ {
}

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

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

$mol_test({

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

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

const a = new A()
const b = a.b()
type Check = $mol_type_assert<typeof b, string>
},
})

}

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

} ) as any as ObjectOrFunctionResultAwaited<Host>
} ) 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 ObjectOrFunctionResultAwaited<Host>
}

type FunctionResultAwaited<Some> = Some extends (...args: infer Args) => infer Res
Expand All @@ -38,7 +50,7 @@ namespace $ {
}

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

}

0 comments on commit 91f1088

Please sign in to comment.