(method: Extract<unknown, `${S}/${M}`> | string, args: P, options: InvokeOptions<R> = {})
| 413 | |
| 414 | // 实际实现 |
| 415 | invoke< |
| 416 | R = any, |
| 417 | S extends keyof NTService = any, |
| 418 | M extends keyof NTService[S] & string = any, |
| 419 | P extends Parameters<Extract<NTService[S][M], (...args: any) => unknown>> = any |
| 420 | >(method: Extract<unknown, `${S}/${M}`> | string, args: P, options: InvokeOptions<R> = {}): Promise<R> { |
| 421 | const splitMethod = method.split('/') |
| 422 | const serviceName = splitMethod[0] as keyof NTService |
| 423 | const methodName = splitMethod.slice(1).join('/') |
| 424 | const pmhqService = NT_SERVICE_TO_PMHQ[serviceName] |
| 425 | let funcName = '' |
| 426 | if (pmhqService) { |
| 427 | if (NOT_SESSION_SERVICES.includes(serviceName)) |
| 428 | funcName = `${pmhqService}.${methodName}` |
| 429 | else { |
| 430 | funcName = `wrapperSession.${pmhqService}().${methodName}` |
| 431 | } |
| 432 | } |
| 433 | else { |
| 434 | funcName = method |
| 435 | } |
| 436 | const timeout = options.timeout ?? 15000 |
| 437 | |
| 438 | return new Promise<R>((resolve, reject) => { |
| 439 | let timeoutId = null |
| 440 | let hookId: string = '' |
| 441 | if (timeout) { |
| 442 | timeoutId = setTimeout(() => { |
| 443 | this.removeReceiveHook(hookId) |
| 444 | const display = inspect(args, { |
| 445 | depth: 10, |
| 446 | compact: true, |
| 447 | breakLength: Infinity, |
| 448 | maxArrayLength: 220 |
| 449 | }) |
| 450 | reject(new Error(`invoke timeout, ${funcName}, ${display}`)) |
| 451 | }, timeout) |
| 452 | } |
| 453 | if (options.resultCmd) { |
| 454 | let firstResult: unknown = undefined |
| 455 | hookId = this.registerReceiveHook<R>(options.resultCmd as string, (data: R) => { |
| 456 | if (options.resultCb && !options.resultCb(data, firstResult)) { |
| 457 | return |
| 458 | } |
| 459 | resolve(data) |
| 460 | this.removeReceiveHook(hookId) |
| 461 | if (timeoutId) { |
| 462 | clearTimeout(timeoutId) |
| 463 | } |
| 464 | }) |
| 465 | this.call(funcName, args, timeout).then(r => { |
| 466 | firstResult = r |
| 467 | if (options.onCallResult) { |
| 468 | const result = options.onCallResult(r) |
| 469 | if (result !== undefined) { |
| 470 | resolve(result) |
| 471 | this.removeReceiveHook(hookId) |
| 472 | if (timeoutId) clearTimeout(timeoutId) |
no test coverage detected