diff --git a/dcp/__init__.py b/dcp/__init__.py index 584928c..d9be7be 100644 --- a/dcp/__init__.py +++ b/dcp/__init__.py @@ -1,15 +1,16 @@ # NOTE TO SELF - have to load class registry before classes that use it +from .dry import make_dcp_class, class_registry, wrap_js_obj, aio_run_wrapper, blocking_run_wrapper +from .js import dcp_client as dcp_js +from .sanity import sanity import sys from types import ModuleType as Module import pythonmonkey as pm PMDict = pm.eval('x={};x').__class__ -proto_own_prop_names = pm.eval('x=>(x?.prototype ? Object.getOwnPropertyNames(x?.prototype) : [])') +proto_own_prop_names = pm.eval( + 'x=>(x?.prototype ? Object.getOwnPropertyNames(x?.prototype) : [])') -from .sanity import sanity -from .js import dcp_client as dcp_js -from .dry import make_dcp_class, class_registry, wrap_js_obj, aio_run_wrapper, blocking_run_wrapper # state init_memo = None @@ -50,7 +51,10 @@ def init_dcp_module(py_parent, js_module, js_name): if isinstance(prop_ref, pm.JSFunctionProxy): # TODO: come up with better way to determine if class... if len(proto_own_prop_names(prop_ref)) > 1: - setattr(module, prop_name, make_dcp_class(prop_ref, name=prop_name)) + new_bfclass = make_dcp_class(prop_ref, name=prop_name) + class_registry.register(new_bfclass) + setattr(module, prop_name, new_bfclass) + # TODO: check if the function is known to return a promise... else: setattr(module, prop_name, blocking_run_wrapper(prop_ref)) diff --git a/dcp/dry/bfclass.py b/dcp/dry/bfclass.py index 17431d3..b2d7652 100644 --- a/dcp/dry/bfclass.py +++ b/dcp/dry/bfclass.py @@ -16,6 +16,7 @@ # @date June 2024 import asyncio +import inspect import types import pythonmonkey as pm @@ -43,7 +44,7 @@ def make_dcp_class(js_class, **kwargs): 'name': js_class_name(js_class), 'props': {}, 'aio_methods': [], - 'aio_ctor': True, + 'aio_ctor': True, # TODO maybe shouldn't be true 'force_blocking': [], 'js_instance': None, } @@ -70,10 +71,7 @@ def __getattr__(self, name): return js_attr def method(*args, **kwargs): - if (name in opts.aio_methods or name in opts.force_blocking): - aio_attr = getattr(self.aio, name) - return asyncio.run(aio_attr(*args, **kwargs)) - return js_attr(*args, **kwargs) + return asyncio.run(aio_run_wrapper(js_attr)(*args, **kwargs)) return method diff --git a/dcp/dry/fn.py b/dcp/dry/fn.py index b3a9692..d7ac2fa 100644 --- a/dcp/dry/fn.py +++ b/dcp/dry/fn.py @@ -9,7 +9,8 @@ async def aio_fn(*args, **kwargs): if inspect.isawaitable(return_value): return await return_value - print("AIO_RUN_WRAPPER_CASE_HIT_WHERE_FN_NOT_RETURN_CORO") # TODO??? + # TODO: check class registry if it should be wrapped instance + return return_value return aio_fn diff --git a/example.py b/example.py new file mode 100755 index 0000000..404d405 --- /dev/null +++ b/example.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +import pythonmonkey as pm +from dcp import job +import dcp +dcp.init() + + +my_j = job.Job('x=>{progress();return x+x;}', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) + +my_j.on('readystatechange', print) +my_j.on('accepted', lambda: print(my_j.id)) +my_j.on('result', print) + +my_j.computeGroups = [{'joinKey': 'joseph', 'joinSecret': 'pringle'}] +my_j.public.name = 'simple bifrost2 example' + +res = my_j.exec() + +# my_j.exec() +# res = my_j.wait() + +print(">>>>>>>>>>>>>>>>>>>>>>>>>> RESULTS ARE IN") +print(pm.eval('Array.from')(res)) + diff --git a/tests/test_init.py b/tests/test_init.py index f05ba52..3af5ad2 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -13,10 +13,9 @@ def test_init(self): self.assertEqual(ret_module, dcp) - job = job.Job('x=>{progress();return x+1', [1, 2, 3]) + job = job.Job('x=>{progress();return x+1', []) + job.on('readystatechange', print) - # TODO - maybe I should just smoke test if a few functions exist? - # How do I test this? def test_init_twice(self): dcp.init()