codegenNativeCommands.js 717 B

12345678910111213141516171819202122232425262728293031
  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. import {dispatchCommand} from '../../Libraries/Renderer/shims/ReactNative';
  12. type Options<T = string> = $ReadOnly<{|
  13. supportedCommands: $ReadOnlyArray<T>,
  14. |}>;
  15. function codegenNativeCommands<T: {...}>(options: Options<$Keys<T>>): T {
  16. const commandObj = {};
  17. options.supportedCommands.forEach(command => {
  18. commandObj[command] = (ref, ...args) => {
  19. dispatchCommand(ref, command, args);
  20. };
  21. });
  22. return ((commandObj: any): T);
  23. }
  24. export default codegenNativeCommands;