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 3 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
73 changes: 73 additions & 0 deletions wire/sync/sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,79 @@ namespace $ {

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 from scope'( $ ) {
class A {
static instances = [] as A[]

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

static b() { return Promise.resolve() }
}
const scope = { A }

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])

},

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

static a() {
const a = new ($mol_wire_sync_make(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: 43 additions & 13 deletions wire/sync/sync.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
namespace $ {

const constructors = new WeakMap<Function, Function>()
zerkalica marked this conversation as resolved.
Show resolved Hide resolved

function $mol_wire_sync_factory<Args extends unknown[], Result>(
zerkalica marked this conversation as resolved.
Show resolved Hide resolved
val: new (...args: Args) => Result
) {
let make = constructors.get(val) as null | ((...args: Args) => Result)

if (! make) {
zerkalica marked this conversation as resolved.
Show resolved Hide resolved
make = $mol_func_name_from((...args: Args) => new val(...args), val)

constructors.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,26 +24,41 @@ 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 fiber = temp( obj, args )
return fiber.sync()
}

const temp = $mol_wire_task.getter(val)

return new Proxy( val, {
apply( target, self, args ) {
return temp( obj, args ).sync()
},
construct(target, args) {
const temp = $mol_wire_task.getter($mol_wire_sync_factory(target))
return temp( obj, args ).sync() as object
},
zerkalica marked this conversation as resolved.
Show resolved Hide resolved

})
},


construct(obj, args) {
const temp = $mol_wire_task.getter($mol_wire_sync_factory(obj as new ( ... args: unknown[] )=> unknown))
zerkalica marked this conversation as resolved.
Show resolved Hide resolved
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>
}

export function $mol_wire_sync_make< Constructor extends (new (...args: any[]) => unknown) > (
obj: Constructor
) {
return $mol_wire_sync(obj) as typeof obj
zerkalica marked this conversation as resolved.
Show resolved Hide resolved
}

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