Skip to content

Commit 03e2788

Browse files
committed
perf: adding inner cache for the impact method
1 parent bcf33b3 commit 03e2788

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

ios/Haptics.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ NS_ASSUME_NONNULL_BEGIN
77

88
@property (nonatomic, strong) UISelectionFeedbackGenerator *selectionGenerator;
99
@property (nonatomic, strong) UINotificationFeedbackGenerator *notificationGenerator;
10+
@property (nonatomic, strong) NSMutableDictionary<NSString *, UIImpactFeedbackGenerator *> *impactGenerators;
1011

1112
@property (nonatomic, strong) NSDictionary<NSString *, NSNumber *> *impactStyles;
1213
@property (nonatomic, strong) NSDictionary<NSString *, NSNumber *> *notificationTypes;

ios/Haptics.mm

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,29 @@ + (BOOL)requiresMainQueueSetup
1313
- (instancetype)init
1414
{
1515
self = [super init];
16+
1617
if (self) {
17-
_notificationGenerator = [UINotificationFeedbackGenerator new];
18-
[_notificationGenerator prepare];
19-
20-
_selectionGenerator = [UISelectionFeedbackGenerator new];
21-
[_selectionGenerator prepare];
22-
23-
_impactStyles = @{
24-
@"light": @(UIImpactFeedbackStyleLight),
25-
@"medium": @(UIImpactFeedbackStyleMedium),
26-
@"heavy": @(UIImpactFeedbackStyleHeavy),
27-
@"soft": @(UIImpactFeedbackStyleSoft),
28-
@"rigid": @(UIImpactFeedbackStyleRigid)
29-
};
30-
31-
_notificationTypes = @{
32-
@"success": @(UINotificationFeedbackTypeSuccess),
33-
@"warning": @(UINotificationFeedbackTypeWarning),
34-
@"error": @(UINotificationFeedbackTypeError)
35-
};
18+
_notificationGenerator = [UINotificationFeedbackGenerator new];
19+
[_notificationGenerator prepare];
20+
21+
_selectionGenerator = [UISelectionFeedbackGenerator new];
22+
[_selectionGenerator prepare];
23+
24+
_impactGenerators = [NSMutableDictionary new];
25+
26+
_impactStyles = @{
27+
@"light": @(UIImpactFeedbackStyleLight),
28+
@"medium": @(UIImpactFeedbackStyleMedium),
29+
@"heavy": @(UIImpactFeedbackStyleHeavy),
30+
@"soft": @(UIImpactFeedbackStyleSoft),
31+
@"rigid": @(UIImpactFeedbackStyleRigid)
32+
};
33+
34+
_notificationTypes = @{
35+
@"success": @(UINotificationFeedbackTypeSuccess),
36+
@"warning": @(UINotificationFeedbackTypeWarning),
37+
@"error": @(UINotificationFeedbackTypeError)
38+
};
3639
}
3740
return self;
3841
}
@@ -47,11 +50,15 @@ - (void)impact:(NSString *)style
4750
reject(@"E_INVALID_STYLE", [NSString stringWithFormat:@"Invalid impact style '%@'", style], nil);
4851
return;
4952
}
50-
UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc]
51-
initWithStyle:(UIImpactFeedbackStyle)styleValue.integerValue];
52-
[generator prepare];
53-
[generator impactOccurred];
53+
UIImpactFeedbackGenerator *generator = self.impactGenerators[style];
5454

55+
if (!generator) {
56+
generator = [[UIImpactFeedbackGenerator alloc]
57+
initWithStyle:(UIImpactFeedbackStyle)styleValue.integerValue];
58+
[generator prepare];
59+
self.impactGenerators[style] = generator;
60+
}
61+
[generator impactOccurred];
5562
resolve(nil);
5663
}
5764

@@ -71,7 +78,6 @@ - (void)notification:(NSString *)type
7178

7279
- (void)selection:(nonnull RCTPromiseResolveBlock)resolve
7380
reject:(nonnull RCTPromiseRejectBlock)reject {
74-
[self.selectionGenerator prepare];
7581
[self.selectionGenerator selectionChanged];
7682
resolve(nil);
7783
}

0 commit comments

Comments
 (0)