UNPKG

1.83 kBPlain TextView Raw
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
8#import <React/RCTPackagerClient.h>
9
10#import <React/RCTLog.h>
11#import <React/RCTReconnectingWebSocket.h>
12#import <React/RCTUtils.h>
13
14#if RCT_DEV // Only supported in dev mode
15
16const int RCT_PACKAGER_CLIENT_PROTOCOL_VERSION = 2;
17
18@implementation RCTPackagerClientResponder {
19 id _msgId;
20 __weak RCTReconnectingWebSocket *_socket;
21}
22
23- (instancetype)initWithId:(id)msgId socket:(RCTReconnectingWebSocket *)socket
24{
25 if (self = [super init]) {
26 _msgId = msgId;
27 _socket = socket;
28 }
29 return self;
30}
31
32- (void)respondWithResult:(id)result
33{
34 NSDictionary<NSString *, id> *msg = @{
35 @"version": @(RCT_PACKAGER_CLIENT_PROTOCOL_VERSION),
36 @"id": _msgId,
37 @"result": result,
38 };
39 NSError *jsError = nil;
40 NSString *message = RCTJSONStringify(msg, &jsError);
41 if (jsError) {
42 RCTLogError(@"%@ failed to stringify message with error %@", [self class], jsError);
43 } else {
44 [_socket send:message];
45 }
46}
47
48- (void)respondWithError:(id)error
49{
50 NSDictionary<NSString *, id> *msg = @{
51 @"version": @(RCT_PACKAGER_CLIENT_PROTOCOL_VERSION),
52 @"id": _msgId,
53 @"error": error,
54 };
55 NSError *jsError = nil;
56 NSString *message = RCTJSONStringify(msg, &jsError);
57 if (jsError) {
58 RCTLogError(@"%@ failed to stringify message with error %@", [self class], jsError);
59 } else {
60 [_socket send:message];
61 }
62}
63@end
64
65#endif