dolphindb 3.0.225 → 3.0.227

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.
package/i18n/dict.json CHANGED
@@ -109,5 +109,8 @@
109
109
  },
110
110
  "value 不能转换为 {{classname}}": {
111
111
  "en": "value cannot be converted to {{classname}}"
112
+ },
113
+ "DDB.define 方法传入的 definition 不符合函数定义格式": {
114
+ "en": "The definition passed in by the DDB.define method does not conform to the function definition format"
112
115
  }
113
116
  }
package/index.d.ts CHANGED
@@ -414,6 +414,10 @@ export declare class DDB {
414
414
  pinvoke: Promise<DdbVoid>;
415
415
  /** 首次定义 jsrpc 的 promise,保证并发调用 rpc 时只定义一次 jsrpc */
416
416
  pjsrpc: Promise<DdbVoid>;
417
+ /** 函数定义缓存 */
418
+ definitions: Map<string, string>;
419
+ /** 函数定义锁,保证并发调用时不重复定义,直接返回第一次定义的结果 */
420
+ pdefinitions: Map<string, Promise<string>>;
417
421
  get connected(): boolean;
418
422
  /**
419
423
  使用 WebSocket URL 初始化连接到 DolphinDB 的实例(不建立实际的网络连接)
@@ -564,6 +568,17 @@ export declare class DDB {
564
568
  parse_message(buf: Uint8Array, error: DdbDatabaseError): DdbMessage;
565
569
  /** 内部的流订阅方法 Internal stream subscription method */
566
570
  subscribe(): Promise<void>;
571
+ /** 定义函数并保存,避免下次重复执行定义,会自动提取脚本中的函数名,作为缓存 key,
572
+ 也同时作为返回值
573
+ - definition: 函数完整定义
574
+ @example
575
+ await ddb.invoke(
576
+ await ddb.define(
577
+ 'def foo (bar) {\n' +
578
+ ' print(bar)\n' +
579
+ '}\n'),
580
+ ['hello']) */
581
+ define(definition: string): Promise<string>;
567
582
  }
568
583
  export interface DdbMessageListener {
569
584
  (message: DdbMessage, _this: DDB): any;
package/index.js CHANGED
@@ -2419,6 +2419,10 @@ export class DDB {
2419
2419
  pinvoke;
2420
2420
  /** 首次定义 jsrpc 的 promise,保证并发调用 rpc 时只定义一次 jsrpc */
2421
2421
  pjsrpc;
2422
+ /** 函数定义缓存 */
2423
+ definitions = new Map();
2424
+ /** 函数定义锁,保证并发调用时不重复定义,直接返回第一次定义的结果 */
2425
+ pdefinitions = new Map();
2422
2426
  get connected() {
2423
2427
  return !this.error && this.lwebsocket.resource?.readyState === WebSocketOpen;
2424
2428
  }
@@ -3137,6 +3141,35 @@ export class DDB {
3137
3141
  }
3138
3142
  })).data());
3139
3143
  }
3144
+ /** 定义函数并保存,避免下次重复执行定义,会自动提取脚本中的函数名,作为缓存 key,
3145
+ 也同时作为返回值
3146
+ - definition: 函数完整定义
3147
+ @example
3148
+ await ddb.invoke(
3149
+ await ddb.define(
3150
+ 'def foo (bar) {\n' +
3151
+ ' print(bar)\n' +
3152
+ '}\n'),
3153
+ ['hello']) */
3154
+ async define(definition) {
3155
+ const matches = /\bdef (\w+)\s?\(/.exec(definition);
3156
+ if (!matches)
3157
+ throw new Error(t('DDB.define 方法传入的 definition 不符合函数定义格式'));
3158
+ const func_name = matches[1];
3159
+ // 已定义成功
3160
+ if (this.definitions.has(func_name))
3161
+ return func_name;
3162
+ // 防止并发调用 define 时多次重复定义
3163
+ let promise = this.pdefinitions.get(func_name);
3164
+ if (!promise)
3165
+ this.pdefinitions.set(func_name, promise = this.execute(definition));
3166
+ await promise;
3167
+ this.definitions.set(func_name, definition);
3168
+ // 成功定义之后,definitions 中有了,就不需要通过 promise 来防止并发了,清理掉
3169
+ // 定义失败时 rejected promise 直接作为后续调用的结果返回
3170
+ this.pdefinitions.delete(func_name);
3171
+ return func_name;
3172
+ }
3140
3173
  }
3141
3174
  export class BigInt128Array {
3142
3175
  static of(...items) {