dolphindb 3.0.225 → 3.0.229

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/browser.d.ts CHANGED
@@ -423,6 +423,10 @@ export declare class DDB {
423
423
  pinvoke: Promise<DdbVoid>;
424
424
  /** 首次定义 jsrpc 的 promise,保证并发调用 rpc 时只定义一次 jsrpc */
425
425
  pjsrpc: Promise<DdbVoid>;
426
+ /** 函数定义缓存 */
427
+ definitions: Map<string, string>;
428
+ /** 函数定义锁,保证并发调用时不重复定义,直接返回第一次定义的结果 */
429
+ pdefinitions: Map<string, Promise<string>>;
426
430
  get connected(): boolean;
427
431
  /**
428
432
  使用 WebSocket URL 初始化连接到 DolphinDB 的实例(不建立实际的网络连接)
@@ -574,6 +578,17 @@ export declare class DDB {
574
578
  parse_message(buf: Uint8Array, error: DdbDatabaseError): DdbMessage;
575
579
  /** 内部的流订阅方法 Internal stream subscription method */
576
580
  subscribe(): Promise<void>;
581
+ /** 定义函数并保存,避免下次重复执行定义,会自动提取脚本中的函数名,作为缓存 key,
582
+ 也同时作为返回值
583
+ - definition: 函数完整定义
584
+ @example
585
+ await ddb.invoke(
586
+ await ddb.define(
587
+ 'def foo (bar) {\n' +
588
+ ' print(bar)\n' +
589
+ '}\n'),
590
+ ['hello']) */
591
+ define(definition: string): Promise<string>;
577
592
  }
578
593
  export interface DdbMessageListener {
579
594
  (message: DdbMessage, _this: DDB): any;
package/browser.js CHANGED
@@ -2443,6 +2443,10 @@ export class DDB {
2443
2443
  pinvoke;
2444
2444
  /** 首次定义 jsrpc 的 promise,保证并发调用 rpc 时只定义一次 jsrpc */
2445
2445
  pjsrpc;
2446
+ /** 函数定义缓存 */
2447
+ definitions = new Map();
2448
+ /** 函数定义锁,保证并发调用时不重复定义,直接返回第一次定义的结果 */
2449
+ pdefinitions = new Map();
2446
2450
  get connected() {
2447
2451
  return !this.error && this.lwebsocket.resource?.readyState === WebSocket.OPEN;
2448
2452
  }
@@ -3159,6 +3163,35 @@ export class DDB {
3159
3163
  }
3160
3164
  })).data());
3161
3165
  }
3166
+ /** 定义函数并保存,避免下次重复执行定义,会自动提取脚本中的函数名,作为缓存 key,
3167
+ 也同时作为返回值
3168
+ - definition: 函数完整定义
3169
+ @example
3170
+ await ddb.invoke(
3171
+ await ddb.define(
3172
+ 'def foo (bar) {\n' +
3173
+ ' print(bar)\n' +
3174
+ '}\n'),
3175
+ ['hello']) */
3176
+ async define(definition) {
3177
+ const matches = /\bdef (\w+) \(/.exec(definition);
3178
+ if (!matches)
3179
+ throw new Error(t('DDB.define 方法传入的 definition 不符合函数定义格式 def xxx ()'));
3180
+ const func_name = matches[1];
3181
+ // 已定义成功
3182
+ if (this.definitions.has(func_name))
3183
+ return func_name;
3184
+ // 防止并发调用 define 时多次重复定义
3185
+ let promise = this.pdefinitions.get(func_name);
3186
+ if (!promise)
3187
+ this.pdefinitions.set(func_name, promise = this.execute(definition));
3188
+ await promise;
3189
+ this.definitions.set(func_name, definition);
3190
+ // 成功定义之后,definitions 中有了,就不需要通过 promise 来防止并发了,清理掉
3191
+ // 定义失败时 rejected promise 直接作为后续调用的结果返回
3192
+ this.pdefinitions.delete(func_name);
3193
+ return func_name;
3194
+ }
3162
3195
  }
3163
3196
  /** https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements
3164
3197
  SharedArrayBuffer is disabled by default in most browsers as a security precaution to avoid Spectre attacks.