Skip to content

Commit

Permalink
$mol_wire_sync review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Nov 4, 2024
1 parent dea4df5 commit 3f94bae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
4 changes: 2 additions & 2 deletions wire/sync/sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace $ {
return $mol_wire_sync(this).a()
}
}

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

Expand Down Expand Up @@ -73,7 +73,7 @@ namespace $ {
static instances = [] as A[]

static a() {
const a = new ($mol_wire_sync_make(A))()
const a = new ($mol_wire_sync(A))()
this.instances.push( a )
$mol_wire_sync(this).b()
}
Expand Down
29 changes: 13 additions & 16 deletions wire/sync/sync.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
namespace $ {
const constructors = new WeakMap<Function, Function>()
const factories = new WeakMap<Function, Function>()

function $mol_wire_sync_factory<Args extends unknown[], Result>(
function factory<Args extends unknown[], Result>(
val: new (...args: Args) => Result
) {
let make = constructors.get(val) as null | ((...args: Args) => Result)
let make = factories.get(val) as null | ((...args: Args) => Result)

if (! make) {
make = $mol_func_name_from((...args: Args) => new val(...args), val)
if ( make ) return make

constructors.set(val, make)
}
make = $mol_func_name_from((...args: Args) => new val(...args), val)
factories.set(val, make)

return make
}
Expand All @@ -33,15 +32,15 @@ namespace $ {
return temp( obj, args ).sync()
},
construct(target, args) {
const temp = $mol_wire_task.getter($mol_wire_sync_factory(target))
const temp = $mol_wire_task.getter(factory(target))
return temp( obj, args ).sync() as object
},

})
},

construct(obj, args) {
const temp = $mol_wire_task.getter($mol_wire_sync_factory(obj as new ( ... args: unknown[] )=> unknown))
const temp = $mol_wire_task.getter(factory(obj as new ( ... args: unknown[] )=> unknown))
return temp( obj, args ).sync() as object
},

Expand All @@ -53,22 +52,20 @@ namespace $ {
} ) as unknown as ObjectOrFunctionResultAwaited<Host>
}

export function $mol_wire_sync_make< Constructor extends (new (...args: any[]) => unknown) > (
obj: Constructor
) {
return $mol_wire_sync(obj) as typeof obj
}

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

type ConstructorResultAwaited<Some> = Some extends new (...args: infer Args) => infer Res
? new (...args: Args) => Res
: {}

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 )
) & ( Some extends Object ? MethodsResultAwaited<Some> & ConstructorResultAwaited<Some> : Some )

}

0 comments on commit 3f94bae

Please sign in to comment.