(hookName, tracer, { name, type, fn })
| 533 | * @returns {PluginFunction} Chainable hooked function. |
| 534 | */ |
| 535 | const makeNewProfiledTapFn = (hookName, tracer, { name, type, fn }) => { |
| 536 | const defaultCategory = ["blink.user_timing"]; |
| 537 | |
| 538 | switch (type) { |
| 539 | case "promise": |
| 540 | return (...args) => { |
| 541 | const id = ++tracer.counter; |
| 542 | tracer.trace.begin({ |
| 543 | name, |
| 544 | id, |
| 545 | cat: defaultCategory |
| 546 | }); |
| 547 | const promise = |
| 548 | /** @type {Promise<(...args: EXPECTED_ANY[]) => EXPECTED_ANY>} */ |
| 549 | (fn(...args)); |
| 550 | return promise.then((r) => { |
| 551 | tracer.trace.end({ |
| 552 | name, |
| 553 | id, |
| 554 | cat: defaultCategory |
| 555 | }); |
| 556 | return r; |
| 557 | }); |
| 558 | }; |
| 559 | case "async": |
| 560 | return (...args) => { |
| 561 | const id = ++tracer.counter; |
| 562 | tracer.trace.begin({ |
| 563 | name, |
| 564 | id, |
| 565 | cat: defaultCategory |
| 566 | }); |
| 567 | const callback = args.pop(); |
| 568 | fn( |
| 569 | ...args, |
| 570 | /** |
| 571 | * Handles the cat callback for this hook. |
| 572 | * @param {...EXPECTED_ANY[]} r result |
| 573 | */ |
| 574 | (...r) => { |
| 575 | tracer.trace.end({ |
| 576 | name, |
| 577 | id, |
| 578 | cat: defaultCategory |
| 579 | }); |
| 580 | callback(...r); |
| 581 | } |
| 582 | ); |
| 583 | }; |
| 584 | case "sync": |
| 585 | return (...args) => { |
| 586 | const id = ++tracer.counter; |
| 587 | // Do not instrument ourself due to the CPU |
| 588 | // profile needing to be the last event in the trace. |
| 589 | if (name === PLUGIN_NAME) { |
| 590 | return fn(...args); |
| 591 | } |
| 592 |
no test coverage detected