FrameRateLogger.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 strict-local
  9. */
  10. 'use strict';
  11. import NativeFrameRateLogger from './NativeFrameRateLogger';
  12. const invariant = require('invariant');
  13. /**
  14. * Flow API for native FrameRateLogger module. If the native module is not installed, function calls
  15. * are just no-ops.
  16. *
  17. * Typical behavior is that `setContext` is called when a new screen is loaded (e.g. via a
  18. * navigation integration), and then `beginScroll` is called by `ScrollResponder` at which point the
  19. * native module then begins tracking frame drops. When `ScrollResponder` calls `endScroll`, the
  20. * native module gathers up all it's frame drop data and reports it via an analytics pipeline for
  21. * analysis.
  22. *
  23. * Note that `beginScroll` may be called multiple times by `ScrollResponder` - unclear if that's a
  24. * bug, but the native module should be robust to that.
  25. *
  26. * In the future we may add support for tracking frame drops in other types of interactions beyond
  27. * scrolling.
  28. */
  29. const FrameRateLogger = {
  30. /**
  31. * Enable `debug` to see local logs of what's going on. `reportStackTraces` will grab stack traces
  32. * during UI thread stalls and upload them if the native module supports it.
  33. */
  34. setGlobalOptions: function(options: {
  35. debug?: boolean,
  36. reportStackTraces?: boolean,
  37. ...
  38. }) {
  39. if (options.debug !== undefined) {
  40. invariant(
  41. NativeFrameRateLogger,
  42. 'Trying to debug FrameRateLogger without the native module!',
  43. );
  44. }
  45. if (NativeFrameRateLogger) {
  46. // Needs to clone the object first to avoid modifying the argument.
  47. const optionsClone = {
  48. debug: !!options.debug,
  49. reportStackTraces: !!options.reportStackTraces,
  50. };
  51. NativeFrameRateLogger.setGlobalOptions(optionsClone);
  52. }
  53. },
  54. /**
  55. * Must call `setContext` before any events can be properly tracked, which is done automatically
  56. * in `AppRegistry`, but navigation is also a common place to hook in.
  57. */
  58. setContext: function(context: string) {
  59. NativeFrameRateLogger && NativeFrameRateLogger.setContext(context);
  60. },
  61. /**
  62. * Called in `ScrollResponder` so any component that uses that module will handle this
  63. * automatically.
  64. */
  65. beginScroll() {
  66. NativeFrameRateLogger && NativeFrameRateLogger.beginScroll();
  67. },
  68. /**
  69. * Called in `ScrollResponder` so any component that uses that module will handle this
  70. * automatically.
  71. */
  72. endScroll() {
  73. NativeFrameRateLogger && NativeFrameRateLogger.endScroll();
  74. },
  75. };
  76. module.exports = FrameRateLogger;