|
15 | 15 |
|
16 | 16 | NS_ASSUME_NONNULL_BEGIN
|
17 | 17 |
|
18 |
| -@class RCTBridge; |
19 | 18 | @protocol RCTPackagerClientMethod;
|
20 |
| -@protocol RCTPackagerConnectionConfig; |
| 19 | +@class RCTPackagerClientResponder; |
| 20 | + |
| 21 | +typedef uint32_t RCTHandlerToken; |
| 22 | +typedef void (^RCTNotificationHandler)(NSDictionary<NSString *, id> *); |
| 23 | +typedef void (^RCTRequestHandler)(NSDictionary<NSString *, id> *, RCTPackagerClientResponder *); |
| 24 | +typedef void (^RCTConnectedHandler)(void); |
| 25 | + |
| 26 | +/** Encapsulates singleton connection to React Native packager. */ |
| 27 | +@interface RCTPackagerConnection : NSObject |
| 28 | + |
| 29 | ++ (instancetype)sharedPackagerConnection; |
21 | 30 |
|
22 | 31 | /**
|
23 |
| - * Encapsulates connection to React Native packager. |
24 |
| - * Dispatches messages from websocket to message handlers that must implement |
25 |
| - * <RCTPackagerClientMethod> protocol. |
26 |
| - * Message dispatch is performed on the main queue, unless message handler |
27 |
| - * provides its own queue by overriding "methodQueue" method. |
| 32 | + * Registers a handler for a notification broadcast from the packager. An |
| 33 | + * example is "reload" - an instruction to reload from the packager. |
| 34 | + * If multiple notification handlers are registered for the same method, they |
| 35 | + * will all be invoked sequentially. |
28 | 36 | */
|
29 |
| -@interface RCTPackagerConnection : NSObject |
| 37 | +- (RCTHandlerToken)addNotificationHandler:(RCTNotificationHandler)handler |
| 38 | + queue:(dispatch_queue_t)queue |
| 39 | + forMethod:(NSString *)method; |
| 40 | + |
| 41 | +/** |
| 42 | + * Registers a handler for a request from the packager. An example is |
| 43 | + * pokeSamplingProfiler; it asks for profile data from the client. |
| 44 | + * Only one handler can be registered for a given method; calling this |
| 45 | + * displaces any previous request handler registered for that method. |
| 46 | + */ |
| 47 | +- (RCTHandlerToken)addRequestHandler:(RCTRequestHandler)handler |
| 48 | + queue:(dispatch_queue_t)queue |
| 49 | + forMethod:(NSString *)method; |
| 50 | + |
| 51 | +/** |
| 52 | + * Registers a handler that runs at most once, when the connection to the |
| 53 | + * packager has been established. The handler will be dispatched immediately |
| 54 | + * if the connection is already established. |
| 55 | + */ |
| 56 | +- (RCTHandlerToken)addConnectedHandler:(RCTConnectedHandler)handler |
| 57 | + queue:(dispatch_queue_t)queue; |
30 | 58 |
|
31 |
| -+ (void)checkDefaultConnectionWithCallback:(void (^)(BOOL isRunning))callback |
32 |
| - queue:(dispatch_queue_t)queue; |
| 59 | +/** Removes a handler. Silently does nothing if the token is not valid. */ |
| 60 | +- (void)removeHandler:(RCTHandlerToken)token; |
33 | 61 |
|
34 |
| -+ (instancetype)connectionForBridge:(RCTBridge *)bridge; |
35 |
| -- (instancetype)initWithConfig:(id<RCTPackagerConnectionConfig>)config; |
36 |
| -- (void)addHandler:(id<RCTPackagerClientMethod>)handler forMethod:(NSString *)name; |
| 62 | +/** Disconnects and removes all handlers. */ |
37 | 63 | - (void)stop;
|
38 | 64 |
|
| 65 | +/** |
| 66 | + * Historically no distinction was made between notification and request |
| 67 | + * handlers. If you use this method, it will be registered as *both* a |
| 68 | + * notification handler *and* a request handler. You should migrate to the |
| 69 | + * new block-based API instead. |
| 70 | + */ |
| 71 | +- (void)addHandler:(id<RCTPackagerClientMethod>)handler |
| 72 | + forMethod:(NSString *)method __deprecated_msg("Use addRequestHandler or addNotificationHandler instead"); |
| 73 | + |
39 | 74 | @end
|
40 | 75 |
|
41 | 76 | NS_ASSUME_NONNULL_END
|
|
0 commit comments