RCTPackagerConnection.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #import <Foundation/Foundation.h>
  8. #import <React/RCTDefines.h>
  9. #if RCT_DEV
  10. NS_ASSUME_NONNULL_BEGIN
  11. @protocol RCTPackagerClientMethod;
  12. @class RCTPackagerClientResponder;
  13. typedef uint32_t RCTHandlerToken;
  14. typedef void (^RCTNotificationHandler)(NSDictionary<NSString *, id> *);
  15. typedef void (^RCTRequestHandler)(NSDictionary<NSString *, id> *, RCTPackagerClientResponder *);
  16. typedef void (^RCTConnectedHandler)(void);
  17. /** Encapsulates singleton connection to React Native packager. */
  18. @interface RCTPackagerConnection : NSObject
  19. + (instancetype)sharedPackagerConnection;
  20. /**
  21. * Registers a handler for a notification broadcast from the packager. An
  22. * example is "reload" - an instruction to reload from the packager.
  23. * If multiple notification handlers are registered for the same method, they
  24. * will all be invoked sequentially.
  25. */
  26. - (RCTHandlerToken)addNotificationHandler:(RCTNotificationHandler)handler
  27. queue:(dispatch_queue_t)queue
  28. forMethod:(NSString *)method;
  29. /**
  30. * Registers a handler for a request from the packager. An example is
  31. * pokeSamplingProfiler; it asks for profile data from the client.
  32. * Only one handler can be registered for a given method; calling this
  33. * displaces any previous request handler registered for that method.
  34. */
  35. - (RCTHandlerToken)addRequestHandler:(RCTRequestHandler)handler
  36. queue:(dispatch_queue_t)queue
  37. forMethod:(NSString *)method;
  38. /**
  39. * Registers a handler that runs at most once, when the connection to the
  40. * packager has been established. The handler will be dispatched immediately
  41. * if the connection is already established.
  42. */
  43. - (RCTHandlerToken)addConnectedHandler:(RCTConnectedHandler)handler queue:(dispatch_queue_t)queue;
  44. /** Removes a handler. Silently does nothing if the token is not valid. */
  45. - (void)removeHandler:(RCTHandlerToken)token;
  46. /** Disconnects and removes all handlers. */
  47. - (void)stop;
  48. /**
  49. * Historically no distinction was made between notification and request
  50. * handlers. If you use this method, it will be registered as *both* a
  51. * notification handler *and* a request handler. You should migrate to the
  52. * new block-based API instead.
  53. */
  54. - (void)addHandler:(id<RCTPackagerClientMethod>)handler
  55. forMethod:(NSString *)method __deprecated_msg("Use addRequestHandler or addNotificationHandler instead");
  56. @end
  57. NS_ASSUME_NONNULL_END
  58. #endif