You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExternalAPI.m 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright @ 2017-present Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <React/RCTBridgeModule.h>
  17. #import "JitsiMeetView+Private.h"
  18. @interface ExternalAPI : NSObject<RCTBridgeModule>
  19. @end
  20. @implementation ExternalAPI
  21. RCT_EXPORT_MODULE();
  22. /**
  23. * Make sure all methods in this module are invoked on the main/UI thread.
  24. */
  25. - (dispatch_queue_t)methodQueue {
  26. return dispatch_get_main_queue();
  27. }
  28. /**
  29. * Dispatches an event that occurred on JavaScript to the view's delegate.
  30. *
  31. * @param name The name of the event.
  32. * @param data The details/specifics of the event to send determined
  33. * by/associated with the specified `name`.
  34. * @param scope
  35. */
  36. RCT_EXPORT_METHOD(sendEvent:(NSString *)name
  37. data:(NSDictionary *)data
  38. scope:(NSString *)scope) {
  39. // The JavaScript App needs to provide uniquely identifying information to
  40. // the native ExternalAPI module so that the latter may match the former
  41. // to the native JitsiMeetView which hosts it.
  42. JitsiMeetView *view = [JitsiMeetView viewForExternalAPIScope:scope];
  43. if (!view) {
  44. return;
  45. }
  46. id delegate = view.delegate;
  47. if (!delegate) {
  48. return;
  49. }
  50. SEL sel = NSSelectorFromString([self methodNameFromEventName:name]);
  51. if (sel && [delegate respondsToSelector:sel]) {
  52. [delegate performSelector:sel withObject:data];
  53. }
  54. }
  55. /**
  56. * Converts a specific event name i.e. redux action type description to a
  57. * method name.
  58. *
  59. * @param eventName The event name to convert to a method name.
  60. * @return A method name constructed from the specified `eventName`.
  61. */
  62. - (NSString *)methodNameFromEventName:(NSString *)eventName {
  63. NSMutableString *methodName
  64. = [NSMutableString stringWithCapacity:eventName.length];
  65. for (NSString *c in [eventName componentsSeparatedByString:@"_"]) {
  66. if (c.length) {
  67. [methodName appendString:
  68. methodName.length ? c.capitalizedString : c.lowercaseString];
  69. }
  70. }
  71. [methodName appendString:@":"];
  72. return methodName;
  73. }
  74. @end