Skip to content

Commit

Permalink
Merge branch 'master' of github.com:hyoo-ru/mam_mol into offline2
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Nov 4, 2024
2 parents a2aa4c3 + 4a8390f commit b13f25e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 26 deletions.
53 changes: 52 additions & 1 deletion wire/sync/sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,60 @@ namespace $ {
return $mol_wire_sync(this).a()
}
}

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

async 'test method from host'( $ ) {
let count = 0
class A {
static a() {
return $mol_wire_sync(this).b()
}

static b() { return Promise.resolve(++count) }
}


$mol_assert_equal(await $mol_wire_async(A).a(), 1, count)

},

async 'test function'( $ ) {
let count = 0
class A {
static a() {
return $mol_wire_sync(this.b)()
}

static b() { return Promise.resolve(++count) }
}


$mol_assert_equal(await $mol_wire_async(A).a(), 1, count)

},

async 'test construct itself'( $ ) {
class A {
static instances = [] as A[]

static a() {
const a = new ($mol_wire_sync(A))()
this.instances.push( a )
$mol_wire_sync(this).b()
}

static b() { return Promise.resolve() }
}

await $mol_wire_async(A).a()
$mol_assert_equal(A.instances.length, 2)
$mol_assert_equal(A.instances[0] instanceof A)

$mol_assert_equal(A.instances[0], A.instances[1])

}
})

}
Expand Down
56 changes: 31 additions & 25 deletions wire/sync/sync.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
namespace $ {

const factories = new WeakMap<Function, Function>()

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

if ( make ) return make

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

return make
}

/**
* Convert asynchronous (promise-based) API to synchronous by wrapping function and method calls in a fiber.
* @see https://mol.hyoo.ru/#!section=docs/=1fcpsq_1wh0h2
Expand All @@ -9,28 +23,24 @@ namespace $ {

get( obj, field ) {

const val = (obj as any)[ field ]
let val = (obj as any)[ field ]
if( typeof val !== 'function' ) return val

const temp = $mol_wire_task.getter( val )
return function $mol_wire_sync( this: Host, ... args: any[] ) {
const temp = $mol_wire_task.getter(val)

return function $mol_wire_sync( this: Host, ... args: unknown[] ) {
const fiber = temp( obj, args )
return fiber.sync()
}

},

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

apply( obj, self, args ) {
const temp = $mol_wire_task.getter( obj as ( ... args: any[] )=> any )
const fiber = temp( self, args )
return fiber.sync()
const temp = $mol_wire_task.getter(obj as ( ... args: any[] )=> any)
return temp(self, args).sync()
},

} ) as unknown as ObjectOrFunctionResultAwaited<Host>
Expand All @@ -40,20 +50,16 @@ namespace $ {
? (...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 ConstructorResultAwaited<Some> = Some extends (new (...args: infer Args) => infer Res)
? new (...args: Args) => Awaited<Res>
: Some

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

}

0 comments on commit b13f25e

Please sign in to comment.