hypha-rpc 0.1.12 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -54,6 +54,28 @@ function indexObject(obj, is) {
54
54
  else if (is.length === 0) return obj;
55
55
  else return indexObject(obj[is[0]], is.slice(1));
56
56
  }
57
+
58
+ function _get_schema(obj, prefix = "") {
59
+ if (obj instanceof Object) {
60
+ let schema = {};
61
+ for (let k in obj) {
62
+ schema[k] = _get_schema(obj[k], prefix + "." + k);
63
+ }
64
+ return schema;
65
+ }
66
+ if (obj instanceof Array) {
67
+ return obj.map((v, i) => _get_schema(v, prefix + "." + i));
68
+ }
69
+ if (typeof obj === "function") {
70
+ if (obj.__schema__) {
71
+ return { type: "function", function: obj.__schema__ };
72
+ } else {
73
+ return { type: "function" };
74
+ }
75
+ }
76
+ return obj;
77
+ }
78
+
57
79
  function getFunctionInfo(func) {
58
80
  const funcString = func.toString();
59
81
 
@@ -212,7 +234,6 @@ class RPC extends _utils_js__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
212
234
  config: { require_context: true, visibility: "public" },
213
235
  ping: this._ping.bind(this),
214
236
  get_service: this.get_local_service.bind(this),
215
- register_service: this.register_service.bind(this),
216
237
  message_cache: {
217
238
  create: this._create_message.bind(this),
218
239
  append: this._append_message.bind(this),
@@ -285,7 +306,6 @@ class RPC extends _utils_js__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
285
306
  _rmethod: "services.built-in.ping",
286
307
  _rpromise: true,
287
308
  _rdoc: "Ping a remote client",
288
- _rsig: "ping(msg)",
289
309
  });
290
310
  (0,_utils_js__WEBPACK_IMPORTED_MODULE_0__.assert)((await method("ping", timeout)) == "pong");
291
311
  }
@@ -469,7 +489,6 @@ class RPC extends _utils_js__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
469
489
  _rmethod: "services.built-in.get_service",
470
490
  _rpromise: true,
471
491
  _rdoc: "Get a remote service",
472
- _rsig: "get_service(service_id)",
473
492
  });
474
493
  const svc = await (0,_utils_js__WEBPACK_IMPORTED_MODULE_0__.waitFor)(
475
494
  method(service_id),
@@ -597,27 +616,15 @@ class RPC extends _utils_js__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
597
616
  }
598
617
 
599
618
  _extract_service_info(service) {
600
- return {
601
- id: `${this._client_id}:${service["id"]}`,
602
- type: service["type"],
603
- name: service["name"],
604
- description: service["description"] || "",
605
- config: service["config"],
606
- app_id: this._app_id,
607
- };
619
+ const serviceInfo = _get_schema(service);
620
+ (serviceInfo.id = `${this._client_id}:${service["id"]}`),
621
+ (serviceInfo.description = service["description"] || ""),
622
+ (serviceInfo.app_id = this._app_id);
623
+ return serviceInfo;
608
624
  }
609
625
 
610
- async register_service(api, overwrite, notify, context) {
626
+ async register_service(api, overwrite, notify) {
611
627
  if (notify === undefined) notify = true;
612
- if (context) {
613
- // If this function is called from remote, we need to make sure
614
- const [workspace, client_id] = context["to"].split("/");
615
- (0,_utils_js__WEBPACK_IMPORTED_MODULE_0__.assert)(client_id === this._client_id);
616
- (0,_utils_js__WEBPACK_IMPORTED_MODULE_0__.assert)(
617
- workspace === context["ws"],
618
- "Services can only be registered from the same workspace",
619
- );
620
- }
621
628
  const service = this.add_service(api, overwrite);
622
629
  const serviceInfo = this._extract_service_info(service);
623
630
  if (notify) {
@@ -959,8 +966,9 @@ class RPC extends _utils_js__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
959
966
  remote_method.__rpc_object__ = encoded_method;
960
967
  const parts = method_id.split(".");
961
968
  remote_method.__name__ = parts[parts.length - 1];
962
- remote_method.__doc__ = encoded_method._rdoc;
963
- remote_method.__sig__ = encoded_method._rsig;
969
+ remote_method.__doc__ =
970
+ encoded_method._rdoc || `Remote method: ${method_id}`;
971
+ remote_method.__schema__ = encoded_method._rschema;
964
972
  return remote_method;
965
973
  }
966
974
 
@@ -1266,21 +1274,17 @@ class RPC extends _utils_js__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
1266
1274
  store[object_id] = aObject;
1267
1275
  }
1268
1276
  bObject._rdoc = aObject.__doc__;
1269
- bObject._rsig = aObject.__sig__;
1270
- if (!bObject._rdoc || !bObject._rsig) {
1277
+ if (!bObject._rdoc) {
1271
1278
  try {
1272
1279
  const funcInfo = getFunctionInfo(aObject);
1273
1280
  if (funcInfo && !bObject._rdoc) {
1274
1281
  bObject._rdoc = `${funcInfo.doc}`;
1275
1282
  }
1276
- if (funcInfo && !bObject._rsig) {
1277
- bObject._rsig = `${funcInfo.name}(${funcInfo.sig})`;
1278
- }
1279
1283
  } catch (e) {
1280
1284
  console.error("Failed to extract function docstring:", aObject);
1281
1285
  }
1282
1286
  }
1283
-
1287
+ bObject._rschema = aObject.__schema__;
1284
1288
  return bObject;
1285
1289
  }
1286
1290
  const isarray = Array.isArray(aObject);
@@ -4419,7 +4423,7 @@ class WebsocketRPCConnection {
4419
4423
  return;
4420
4424
  } else if (`${e}`.includes("NotImplementedError:")) {
4421
4425
  console.error(
4422
- "It appears that you are trying to connect to a hypha server that is older than 0.20.0, please upgrade the hypha server or use `from imjoy_rpc.hypha import connect_to_sever` instead",
4426
+ `${e}\nIt appears that you are trying to connect to a hypha server that is older than 0.20.0, please upgrade the hypha server or use the websocket client in imjoy-rpc(https://www.npmjs.com/package/imjoy-rpc) instead`,
4423
4427
  );
4424
4428
  return;
4425
4429
  }
@@ -4576,6 +4580,10 @@ async function connectToServer(config) {
4576
4580
  wm.registerCodec = rpc.register_codec.bind(rpc);
4577
4581
  wm.emit = rpc.emit;
4578
4582
  wm.on = rpc.on;
4583
+ wm.registerService = rpc.register_service.bind(rpc);
4584
+ wm.register_service = wm.registerService;
4585
+ wm.unregisterService = rpc.unregister_service.bind(rpc);
4586
+ wm.unregister_service = wm.unregisterService;
4579
4587
  if (connection.manager_id) {
4580
4588
  rpc.on("force-exit", async (message) => {
4581
4589
  if (message.from === "*/" + connection.manager_id) {