FLEXRuntimeController.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // FLEXRuntimeController.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 3/23/17.
  6. // Copyright © 2017 Tanner Bennett. All rights reserved.
  7. //
  8. #import "FLEXRuntimeController.h"
  9. #import "FLEXRuntimeClient.h"
  10. #import "FLEXMethod.h"
  11. @interface FLEXRuntimeController ()
  12. @property (nonatomic, readonly) NSCache *bundlePathsCache;
  13. @property (nonatomic, readonly) NSCache *bundleNamesCache;
  14. @property (nonatomic, readonly) NSCache *classNamesCache;
  15. @property (nonatomic, readonly) NSCache *methodsCache;
  16. @end
  17. @implementation FLEXRuntimeController
  18. #pragma mark Initialization
  19. static FLEXRuntimeController *controller = nil;
  20. + (instancetype)shared {
  21. static dispatch_once_t onceToken;
  22. dispatch_once(&onceToken, ^{
  23. controller = [self new];
  24. });
  25. return controller;
  26. }
  27. - (id)init {
  28. self = [super init];
  29. if (self) {
  30. _bundlePathsCache = [NSCache new];
  31. _bundleNamesCache = [NSCache new];
  32. _classNamesCache = [NSCache new];
  33. _methodsCache = [NSCache new];
  34. }
  35. return self;
  36. }
  37. #pragma mark Public
  38. + (NSArray *)dataForKeyPath:(FLEXRuntimeKeyPath *)keyPath {
  39. if (keyPath.bundleKey) {
  40. if (keyPath.classKey) {
  41. if (keyPath.methodKey) {
  42. return [[self shared] methodsForKeyPath:keyPath];
  43. } else {
  44. return [[self shared] classesForKeyPath:keyPath];
  45. }
  46. } else {
  47. return [[self shared] bundleNamesForToken:keyPath.bundleKey];
  48. }
  49. } else {
  50. return @[];
  51. }
  52. }
  53. + (NSArray<NSArray<FLEXMethod *> *> *)methodsForToken:(FLEXSearchToken *)token
  54. instance:(NSNumber *)inst
  55. inClasses:(NSArray<NSString*> *)classes {
  56. return [FLEXRuntimeClient.runtime
  57. methodsForToken:token
  58. instance:inst
  59. inClasses:classes
  60. ];
  61. }
  62. + (NSMutableArray<NSString *> *)classesForKeyPath:(FLEXRuntimeKeyPath *)keyPath {
  63. return [[self shared] classesForKeyPath:keyPath];
  64. }
  65. + (NSString *)shortBundleNameForClass:(NSString *)name {
  66. const char *imageName = class_getImageName(NSClassFromString(name));
  67. if (!imageName) {
  68. return @"(unspecified)";
  69. }
  70. return [FLEXRuntimeClient.runtime shortNameForImageName:@(imageName)];
  71. }
  72. + (NSString *)imagePathWithShortName:(NSString *)suffix {
  73. return [FLEXRuntimeClient.runtime imageNameForShortName:suffix];
  74. }
  75. + (NSArray *)allBundleNames {
  76. return FLEXRuntimeClient.runtime.imageDisplayNames;
  77. }
  78. #pragma mark Private
  79. - (NSMutableArray *)bundlePathsForToken:(FLEXSearchToken *)token {
  80. // Only cache if no wildcard
  81. BOOL shouldCache = token == TBWildcardOptionsNone;
  82. if (shouldCache) {
  83. NSMutableArray<NSString*> *cached = [self.bundlePathsCache objectForKey:token];
  84. if (cached) {
  85. return cached;
  86. }
  87. NSMutableArray<NSString*> *bundles = [FLEXRuntimeClient.runtime bundlePathsForToken:token];
  88. [self.bundlePathsCache setObject:bundles forKey:token];
  89. return bundles;
  90. }
  91. else {
  92. return [FLEXRuntimeClient.runtime bundlePathsForToken:token];
  93. }
  94. }
  95. - (NSMutableArray<NSString *> *)bundleNamesForToken:(FLEXSearchToken *)token {
  96. // Only cache if no wildcard
  97. BOOL shouldCache = token == TBWildcardOptionsNone;
  98. if (shouldCache) {
  99. NSMutableArray<NSString*> *cached = [self.bundleNamesCache objectForKey:token];
  100. if (cached) {
  101. return cached;
  102. }
  103. NSMutableArray<NSString*> *bundles = [FLEXRuntimeClient.runtime bundleNamesForToken:token];
  104. [self.bundleNamesCache setObject:bundles forKey:token];
  105. return bundles;
  106. }
  107. else {
  108. return [FLEXRuntimeClient.runtime bundleNamesForToken:token];
  109. }
  110. }
  111. - (NSMutableArray<NSString *> *)classesForKeyPath:(FLEXRuntimeKeyPath *)keyPath {
  112. FLEXSearchToken *classToken = keyPath.classKey;
  113. FLEXSearchToken *bundleToken = keyPath.bundleKey;
  114. // Only cache if no wildcard
  115. BOOL shouldCache = bundleToken.options == 0 && classToken.options == 0;
  116. NSString *key = nil;
  117. if (shouldCache) {
  118. key = [@[bundleToken.description, classToken.description] componentsJoinedByString:@"+"];
  119. NSMutableArray<NSString *> *cached = [self.classNamesCache objectForKey:key];
  120. if (cached) {
  121. return cached;
  122. }
  123. }
  124. NSMutableArray<NSString *> *bundles = [self bundlePathsForToken:bundleToken];
  125. NSMutableArray<NSString *> *classes = [FLEXRuntimeClient.runtime
  126. classesForToken:classToken inBundles:bundles
  127. ];
  128. if (shouldCache) {
  129. [self.classNamesCache setObject:classes forKey:key];
  130. }
  131. return classes;
  132. }
  133. - (NSArray<NSMutableArray<FLEXMethod *> *> *)methodsForKeyPath:(FLEXRuntimeKeyPath *)keyPath {
  134. // Only cache if no wildcard, but check cache anyway bc I'm lazy
  135. NSArray<NSMutableArray *> *cached = [self.methodsCache objectForKey:keyPath];
  136. if (cached) {
  137. return cached;
  138. }
  139. NSArray<NSString *> *classes = [self classesForKeyPath:keyPath];
  140. NSArray<NSMutableArray<FLEXMethod *> *> *methodLists = [FLEXRuntimeClient.runtime
  141. methodsForToken:keyPath.methodKey
  142. instance:keyPath.instanceMethods
  143. inClasses:classes
  144. ];
  145. for (NSMutableArray<FLEXMethod *> *methods in methodLists) {
  146. [methods sortUsingComparator:^NSComparisonResult(FLEXMethod *m1, FLEXMethod *m2) {
  147. return [m1.description caseInsensitiveCompare:m2.description];
  148. }];
  149. }
  150. // Only cache if no wildcard, otherwise the cache could grow very large
  151. if (keyPath.bundleKey.isAbsolute &&
  152. keyPath.classKey.isAbsolute) {
  153. [self.methodsCache setObject:methodLists forKey:keyPath];
  154. }
  155. return methodLists;
  156. }
  157. @end