Skip to content

Commit

Permalink
py: Support Node.js 14+ or newer (#16)
Browse files Browse the repository at this point in the history
* py: Support Node.js 14+, fix AsyncTask

* py: emitter patches

* py: update docs
  • Loading branch information
extremeheat authored Jul 18, 2021
1 parent ffddf83 commit 3ccea5c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.2.4
* py: Support Node versions 14+

## 0.2.3
* py: Add notebook and Google Colab support [#15](https://github.com/extremeheat/JSPyBridge/pull/15)
* py: CLI now has a new --update flag to update internal, Node.js dependencies. Now use --clean to reset the package store.
Expand Down
29 changes: 22 additions & 7 deletions src/javascript/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def init():
config.global_jsi = proxy.Proxy(config.executor, 0)
atexit.register(config.event_loop.on_exit)

if config.global_jsi.needsNodePatches():
config.node_emitter_patches = True


init()

Expand All @@ -34,11 +37,10 @@ def require(name, version=None):
globalThis = config.global_jsi.globalThis


def AsyncTask(fn):
fn.is_async_task = True

def decor(start):
t = config.event_loop.startThread(fn)
def AsyncTask(start=False):
def decor(fn):
fn.is_async_task = True
t = config.event_loop.newTaskThread(fn)
if start:
t.start()

Expand All @@ -53,7 +55,17 @@ def decor(start):
# you will not be able to off an emitter.
def On(emitter, event):
# print("On", emitter, event,onEvent)
def decor(fn):
def decor(_fn):
# Once Colab updates to Node 16, we can remove this.
# Here we need to manually add in the `this` argument for consistency in Node versions.
# In JS we could normally just bind `this` but there is no bind in Python.
if config.node_emitter_patches:
def handler(*args, **kwargs):
_fn(emitter, *args, **kwargs)
fn = handler
else:
fn = _fn

emitter.on(event, fn)
# We need to do some special things here. Because each Python object
# on the JS side is unique, EventEmitter is unable to equality check
Expand All @@ -77,7 +89,10 @@ def decor(fn):
i = hash(fn)

def handler(*args, **kwargs):
fn(*args, **kwargs)
if config.node_emitter_patches:
fn(emitter, *args, **kwargs)
else:
fn(*args, **kwargs)
del config.event_loop.callbacks[i]

emitter.once(event, handler)
Expand Down
2 changes: 2 additions & 0 deletions src/javascript/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
global_jsi = None
# Currently this breaks GC
fast_mode = False
# Whether we need patches for legacy node versions
node_emitter_patches = False


if ("DEBUG" in os.environ) and ("jspybridge" in os.getenv("DEBUG")):
Expand Down
2 changes: 1 addition & 1 deletion src/javascript/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def com_io():
)
except Exception as e:
print(
"--====--\t--====--\n\nBridge failed to spawn JS process!\n\nDo you have Node.js 15 or newer installed? Get it at https://nodejs.org/\n\n--====--\t--====--"
"--====--\t--====--\n\nBridge failed to spawn JS process!\n\nDo you have Node.js 16 or newer installed? Get it at https://nodejs.org/\n\n--====--\t--====--"
)
stop()
raise e
Expand Down
9 changes: 8 additions & 1 deletion src/javascript/js/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ class Bridge {
console,
require: $require,
_require: require,
globalThis
globalThis,
needsNodePatches: () => {
const [major, minor] = process.versions.node.split('.')
if ((major == 14 && minor < 17) || (major == 15)) { // eslint-disable-line
return true
}
return false
}
}
}
this.ipc = ipc
Expand Down

0 comments on commit 3ccea5c

Please sign in to comment.