warnOnce.js 784 B

12345678910111213141516171819202122232425262728293031323334
  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. const warning = require('fbjs/lib/warning');
  12. const warnedKeys: {[string]: boolean, ...} = {};
  13. /**
  14. * A simple function that prints a warning message once per session.
  15. *
  16. * @param {string} key - The key used to ensure the message is printed once.
  17. * This should be unique to the callsite.
  18. * @param {string} message - The message to print
  19. */
  20. function warnOnce(key: string, message: string) {
  21. if (warnedKeys[key]) {
  22. return;
  23. }
  24. warning(false, message);
  25. warnedKeys[key] = true;
  26. }
  27. module.exports = warnOnce;