From 0d8f20ad855c32fbe0052893e71229ed276e9dea Mon Sep 17 00:00:00 2001 From: Stefan Zerkalica Date: Tue, 3 Oct 2023 13:11:43 +0300 Subject: [PATCH 1/8] $mol_wire_async, $mol_wire_sync simplify types --- wire/async/async.ts | 24 ++++++++++-------------- wire/sync/sync.ts | 18 +++++++----------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/wire/async/async.ts b/wire/async/async.ts index 2dd3c0124f5..1b69890af1b 100644 --- a/wire/async/async.ts +++ b/wire/async/async.ts @@ -30,20 +30,16 @@ namespace $ { return fiber.async() }, - } ) as any as ( - Host extends ( ... args: infer Args )=> infer Res - ? Res extends Promise - ? Host - : ( ... args: Args )=> Promise< Res > - : {} - ) & { - [ key in keyof Host ]: Host[ key ] extends ( ... args: infer Args )=> infer Res - ? Res extends Promise - ? Host[ key ] - : ( ... args: Args )=> Promise< Res > - : Host[ key ] - } + } ) as any as MethodsResultPromisify } - + + type PromiseWrap

= P extends PromiseLike ? P : Promise

+ + type MethodsResultPromisify = { + [K in keyof Host]: Host[K] extends (...args: infer Args) => infer Res + ? (...args: Args) => PromiseWrap + : Host[K] + } + } diff --git a/wire/sync/sync.ts b/wire/sync/sync.ts index 9b8de135b2a..50e368db835 100644 --- a/wire/sync/sync.ts +++ b/wire/sync/sync.ts @@ -26,17 +26,13 @@ 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 any as MethodsResultAwaited + } + + type MethodsResultAwaited = { + [K in keyof Host]: Host[K] extends (...args: infer Args) => infer Res + ? (...args: Args) => Awaited + : Host[K] } } From f6c5b405252cde3ecac50395477555d247946348 Mon Sep 17 00:00:00 2001 From: Stefan Zerkalica Date: Tue, 3 Oct 2023 13:27:43 +0300 Subject: [PATCH 2/8] $mol_wire_async, $mol_wire_sync fix types --- wire/async/async.ts | 14 +++++++++----- wire/sync/sync.ts | 14 ++++++++++---- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/wire/async/async.ts b/wire/async/async.ts index 1b69890af1b..7d8b03fcbfd 100644 --- a/wire/async/async.ts +++ b/wire/async/async.ts @@ -30,16 +30,20 @@ namespace $ { return fiber.async() }, - } ) as any as MethodsResultPromisify + } ) as any as ObjectOrFunctionResultPromisify } - type PromiseWrap

= P extends PromiseLike ? P : Promise

+ type FunctionResultPromisify = Some extends (...args: infer Args) => infer Res + ? Res extends PromiseLike ? Some : (...args: Args) => Promise + : Some type MethodsResultPromisify = { - [K in keyof Host]: Host[K] extends (...args: infer Args) => infer Res - ? (...args: Args) => PromiseWrap - : Host[K] + [K in keyof Host]: FunctionResultPromisify } + type ObjectOrFunctionResultPromisify = Some extends (...args: any) => unknown + ? FunctionResultPromisify + : Some extends Object ? MethodsResultPromisify : Some + } diff --git a/wire/sync/sync.ts b/wire/sync/sync.ts index 50e368db835..e1ccb27fd29 100644 --- a/wire/sync/sync.ts +++ b/wire/sync/sync.ts @@ -26,13 +26,19 @@ namespace $ { return fiber.sync() }, - } ) as any as MethodsResultAwaited + } ) as any as ObjectOrFunctionResultAwaited } + type FunctionResultAwaited = Some extends (...args: infer Args) => infer Res + ? (...args: Args) => Awaited + : Some + type MethodsResultAwaited = { - [K in keyof Host]: Host[K] extends (...args: infer Args) => infer Res - ? (...args: Args) => Awaited - : Host[K] + [K in keyof Host]: FunctionResultAwaited } + type ObjectOrFunctionResultAwaited = Some extends (...args: any) => unknown + ? FunctionResultAwaited + : Some extends Object ? MethodsResultAwaited : Some + } From aeb3f02c079b261aee346c0588f7a92758d15e39 Mon Sep 17 00:00:00 2001 From: Stefan Zerkalica Date: Tue, 3 Oct 2023 13:38:53 +0300 Subject: [PATCH 3/8] $mol_wire_async, $mol_wire_sync fix types2 --- build/server/server.node.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/build/server/server.node.ts b/build/server/server.node.ts index f13ab935dd9..cf05e0ba594 100644 --- a/build/server/server.node.ts +++ b/build/server/server.node.ts @@ -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 ) } } From 11120309c7630ea0e10c64c1fd8d1ff3dbae747f Mon Sep 17 00:00:00 2001 From: Stefan Zerkalica Date: Tue, 3 Oct 2023 13:54:54 +0300 Subject: [PATCH 4/8] $mol_wire_async, $mol_wire_sync fix types3 --- wire/async/async.ts | 6 +++--- wire/sync/sync.ts | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/wire/async/async.ts b/wire/async/async.ts index 7d8b03fcbfd..9c4463fa6be 100644 --- a/wire/async/async.ts +++ b/wire/async/async.ts @@ -42,8 +42,8 @@ namespace $ { [K in keyof Host]: FunctionResultPromisify } - type ObjectOrFunctionResultPromisify = Some extends (...args: any) => unknown - ? FunctionResultPromisify - : Some extends Object ? MethodsResultPromisify : Some + type ObjectOrFunctionResultPromisify = ( + Some extends (...args: any) => unknown ? FunctionResultPromisify : {} + ) & ( Some extends Object ? MethodsResultPromisify : Some ) } diff --git a/wire/sync/sync.ts b/wire/sync/sync.ts index e1ccb27fd29..950e37f8f8e 100644 --- a/wire/sync/sync.ts +++ b/wire/sync/sync.ts @@ -37,8 +37,8 @@ namespace $ { [K in keyof Host]: FunctionResultAwaited } - type ObjectOrFunctionResultAwaited = Some extends (...args: any) => unknown - ? FunctionResultAwaited - : Some extends Object ? MethodsResultAwaited : Some + type ObjectOrFunctionResultAwaited = ( + Some extends (...args: any) => unknown ? FunctionResultAwaited : {} + ) & ( Some extends Object ? MethodsResultAwaited : Some ) } From 91f1088f1555903bf7205cb34b8bec70e66f6b45 Mon Sep 17 00:00:00 2001 From: Stefan Zerkalica Date: Tue, 3 Oct 2023 21:35:02 +0300 Subject: [PATCH 5/8] $mol_wire_sync, $mol_wire_async types tests --- wire/async/async.test.ts | 15 +++++++++++++++ wire/async/async.ts | 17 +++++++++++++++-- wire/sync/sync.test.ts | 23 +++++++++++++++++++++++ wire/sync/sync.ts | 16 ++++++++++++++-- 4 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 wire/sync/sync.test.ts diff --git a/wire/async/async.test.ts b/wire/async/async.test.ts index 2b7546aa81a..3ffb3e867ea 100644 --- a/wire/async/async.test.ts +++ b/wire/async/async.test.ts @@ -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> + }, async 'Latest method calls wins'( $ ) { diff --git a/wire/async/async.ts b/wire/async/async.ts index 9c4463fa6be..c347520fb47 100644 --- a/wire/async/async.ts +++ b/wire/async/async.ts @@ -30,7 +30,20 @@ namespace $ { return fiber.async() }, - } ) as any as ObjectOrFunctionResultPromisify + } ) as any as ( + Host extends ( ... args: infer Args )=> infer Res + ? Res extends Promise + ? Host + : ( ... args: Args )=> Promise< Res > + : {} + ) & { + [ key in keyof Host ]: Host[ key ] extends ( ... args: infer Args )=> infer Res + ? Res extends Promise + ? Host[ key ] + : ( ... args: Args )=> Promise< Res > + : Host[ key ] + } + // as ObjectOrFunctionResultPromisify } @@ -43,7 +56,7 @@ namespace $ { } type ObjectOrFunctionResultPromisify = ( - Some extends (...args: any) => unknown ? FunctionResultPromisify : {} + Some extends (...args: any) => unknown ? FunctionResultPromisify : {} ) & ( Some extends Object ? MethodsResultPromisify : Some ) } diff --git a/wire/sync/sync.test.ts b/wire/sync/sync.test.ts new file mode 100644 index 00000000000..912e69bf9a9 --- /dev/null +++ b/wire/sync/sync.test.ts @@ -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 + }, + }) + +} + diff --git a/wire/sync/sync.ts b/wire/sync/sync.ts index 950e37f8f8e..96ba204d0a6 100644 --- a/wire/sync/sync.ts +++ b/wire/sync/sync.ts @@ -26,7 +26,19 @@ namespace $ { return fiber.sync() }, - } ) as any as ObjectOrFunctionResultAwaited + } ) 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 } type FunctionResultAwaited = Some extends (...args: infer Args) => infer Res @@ -38,7 +50,7 @@ namespace $ { } type ObjectOrFunctionResultAwaited = ( - Some extends (...args: any) => unknown ? FunctionResultAwaited : {} + Some extends (...args: any) => unknown ? FunctionResultAwaited : {} ) & ( Some extends Object ? MethodsResultAwaited : Some ) } From 1495e24c436d97f17de42127ef9ad1c10e576f2c Mon Sep 17 00:00:00 2001 From: Stefan Zerkalica Date: Tue, 3 Oct 2023 21:40:28 +0300 Subject: [PATCH 6/8] $mol_wire_sync, $mol_wire_async new types restore --- wire/async/async.ts | 15 +-------------- wire/sync/sync.ts | 14 +------------- 2 files changed, 2 insertions(+), 27 deletions(-) diff --git a/wire/async/async.ts b/wire/async/async.ts index c347520fb47..98deb2dcbe3 100644 --- a/wire/async/async.ts +++ b/wire/async/async.ts @@ -30,20 +30,7 @@ namespace $ { return fiber.async() }, - } ) as any as ( - Host extends ( ... args: infer Args )=> infer Res - ? Res extends Promise - ? Host - : ( ... args: Args )=> Promise< Res > - : {} - ) & { - [ key in keyof Host ]: Host[ key ] extends ( ... args: infer Args )=> infer Res - ? Res extends Promise - ? Host[ key ] - : ( ... args: Args )=> Promise< Res > - : Host[ key ] - } - // as ObjectOrFunctionResultPromisify + } ) as unknown as ObjectOrFunctionResultPromisify } diff --git a/wire/sync/sync.ts b/wire/sync/sync.ts index 96ba204d0a6..90a5a328eb2 100644 --- a/wire/sync/sync.ts +++ b/wire/sync/sync.ts @@ -26,19 +26,7 @@ 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 ObjectOrFunctionResultAwaited + } ) as unknown as ObjectOrFunctionResultAwaited } type FunctionResultAwaited = Some extends (...args: infer Args) => infer Res From 669b57552f4e7d778da26b36d19bd255d92acbec Mon Sep 17 00:00:00 2001 From: Stefan Zerkalica Date: Tue, 3 Oct 2023 21:53:41 +0300 Subject: [PATCH 7/8] $mol_wire_async fix test --- wire/async/async.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wire/async/async.test.ts b/wire/async/async.test.ts index 3ffb3e867ea..06387221850 100644 --- a/wire/async/async.test.ts +++ b/wire/async/async.test.ts @@ -17,7 +17,7 @@ namespace $ { type Check = $mol_type_assert> }, - async 'Latest method calls wins'( $ ) { + 'Latest method calls wins'( $ ) { class NameLogger extends $mol_object2 { From b892723732683701a00bc9019bf954bf8afa8a4a Mon Sep 17 00:00:00 2001 From: Stefan Zerkalica Date: Tue, 3 Oct 2023 22:07:43 +0300 Subject: [PATCH 8/8] $mol_wire_async fix async test --- wire/async/async.test.ts | 13 +++++-------- wire/sync/sync.test.ts | 8 +++----- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/wire/async/async.test.ts b/wire/async/async.test.ts index 06387221850..86cf4fec124 100644 --- a/wire/async/async.test.ts +++ b/wire/async/async.test.ts @@ -1,23 +1,20 @@ namespace $ { $mol_test({ - 'test types'( $ ) { class A { - a() { + static a() { return '' } - b() { + static b() { return $mol_wire_async(this).a() } } - - const a = new A() - const b = a.b() - type Check = $mol_type_assert> + + type Check = $mol_type_assert, Promise> }, - 'Latest method calls wins'( $ ) { + async 'Latest method calls wins'( $ ) { class NameLogger extends $mol_object2 { diff --git a/wire/sync/sync.test.ts b/wire/sync/sync.test.ts index 912e69bf9a9..7197e8b949c 100644 --- a/wire/sync/sync.test.ts +++ b/wire/sync/sync.test.ts @@ -4,18 +4,16 @@ namespace $ { 'test types'( $ ) { class A { - a() { + static a() { return Promise.resolve('') } - b() { + static b() { return $mol_wire_sync(this).a() } } - const a = new A() - const b = a.b() - type Check = $mol_type_assert + type Check = $mol_type_assert, string> }, })