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_sync: idempotent new operator support #711

Merged
merged 6 commits into from
Nov 4, 2024
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
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
43 changes: 32 additions & 11 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,21 +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(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 @@ -33,12 +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 ObjectOrFunctionResultAwaited<Some> = (
Some extends (...args: any) => unknown ? FunctionResultAwaited<Some> : {}
) & ( Some extends Object ? MethodsResultAwaited<Some> : Some )
) & ( Some extends Object ? MethodsResultAwaited<Some> & ConstructorResultAwaited<Some> : Some )

}
Loading