Skip to content

Commit

Permalink
Support kwargs in js (#203)
Browse files Browse the repository at this point in the history
* Support kwargs in js

* Fix format
  • Loading branch information
oeway authored Oct 21, 2021
1 parent 6a81faa commit 51c3da6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ The `encoder` function take an object as input and you need to return the repres

The `decoder` function converts the encoded object into the actual object. It will only be called when the `_rtype` of an object matches the `name` of the codec.

### Remote function calls and arguments
Remote function call is almost the same as calling a local function. The arguments are mapped directly, for example, you can call a Python function `foo(a, b, c)` from javascript or vise versa. However, since Javascript does not support named arguments as Python does, ImJoy does the following conversion:
* For functions defined in Javascript, there is no difference when calling from Python
* For functions defined in Python, when calling from Javascript and if the last argument is an object, then it will be automatically converted into keyword arguments when calling the Python function. For example, if you have a Python function defined as `def foo(a, b, c=None):`, in Javascript, you should call it as `foo(9, 10, {c: 33})`.

### Support Zarr Array encoding
[Zarr](https://zarr.readthedocs.io/en/stable/) is a more scalable n-dimensional array format that has a numpy-like api and can be used with multiple backends. It is ideally suited for sending large n-dimensional array between imjoy-rpc peers in a lazy fashion. We have an [internal implementation] of codec that can support sending.

Expand Down
2 changes: 1 addition & 1 deletion javascript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion javascript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "imjoy-rpc",
"version": "0.3.31",
"version": "0.3.32",
"description": "Remote procedure calls for ImJoy.",
"module": "index.js",
"scripts": {
Expand Down
27 changes: 24 additions & 3 deletions javascript/src/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,12 @@ export class RPC extends MessageEmitter {
wrapped_reject.__promise_pair = encodedPromise[0]._rvalue;

let args = Array.prototype.slice.call(arguments);
const argLength = args.length;
// if the last argument is an object, mark it as kwargs
const withKwargs =
argLength > 0 &&
typeof args[argLength - 1] === "object" &&
args[argLength - 1] !== null;
if (
name === "register" ||
name === "registerService" ||
Expand All @@ -411,7 +417,8 @@ export class RPC extends MessageEmitter {
name: name,
object_id: objectId,
args: args,
promise: encodedPromise
promise: encodedPromise,
with_kwargs: withKwargs
},
transferables
);
Expand Down Expand Up @@ -865,6 +872,12 @@ export class RPC extends MessageEmitter {
remoteCallback = function() {
return new Promise(async (resolve, reject) => {
const args = await me._wrap(Array.prototype.slice.call(arguments));
const argLength = args.length;
// if the last argument is an object, mark it as kwargs
const withKwargs =
argLength > 0 &&
typeof args[argLength - 1] === "object" &&
args[argLength - 1] !== null;
const transferables = args.__transferables__;
if (transferables) delete args.__transferables__;

Expand All @@ -879,7 +892,8 @@ export class RPC extends MessageEmitter {
target_id: targetId,
id: cid,
args: args,
promise: encodedPromise
promise: encodedPromise,
with_kwargs: withKwargs
},
transferables
);
Expand All @@ -892,14 +906,21 @@ export class RPC extends MessageEmitter {
} else {
remoteCallback = async function() {
const args = await me._wrap(Array.prototype.slice.call(arguments));
const argLength = args.length;
// if the last argument is an object, mark it as kwargs
const withKwargs =
argLength > 0 &&
typeof args[argLength - 1] === "object" &&
args[argLength - 1] !== null;
const transferables = args.__transferables__;
if (transferables) delete args.__transferables__;
return me._connection.emit(
{
type: "callback",
target_id: targetId,
id: cid,
args: args
args: args,
with_kwargs: withKwargs
},
transferables
);
Expand Down
2 changes: 1 addition & 1 deletion python/imjoy_rpc/VERSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.3.31"
"version": "0.3.32"
}

0 comments on commit 51c3da6

Please sign in to comment.