BridgeSpyStallHandler.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow
  9. */
  10. 'use strict';
  11. const JSEventLoopWatchdog = require('./JSEventLoopWatchdog');
  12. const MessageQueue = require('../BatchedBridge/MessageQueue');
  13. const infoLog = require('../Utilities/infoLog');
  14. const BridgeSpyStallHandler = {
  15. register: function() {
  16. let spyBuffer = [];
  17. MessageQueue.spy(data => {
  18. spyBuffer.push(data);
  19. });
  20. const TO_JS = 0;
  21. JSEventLoopWatchdog.addHandler({
  22. onStall: () => {
  23. infoLog(
  24. spyBuffer.length + ' bridge messages during stall: ',
  25. spyBuffer.map(info => {
  26. let args = '<args>';
  27. try {
  28. args = JSON.stringify(info.args);
  29. } catch (e1) {
  30. if (Array.isArray(info.args)) {
  31. args = info.args.map(arg => {
  32. try {
  33. return JSON.stringify(arg);
  34. } catch (e2) {
  35. return '?';
  36. }
  37. });
  38. } else {
  39. args = 'keys:' + JSON.stringify(Object.keys(info.args));
  40. }
  41. }
  42. return (
  43. `${info.type === TO_JS ? 'N->JS' : 'JS->N'} : ` +
  44. `${info.module ? info.module + '.' : ''}${
  45. info.method
  46. }(${JSON.stringify(args)})`
  47. );
  48. }),
  49. );
  50. },
  51. onIterate: () => {
  52. spyBuffer = [];
  53. },
  54. });
  55. },
  56. };
  57. module.exports = BridgeSpyStallHandler;