Skip to content

Commit f89c509

Browse files
brunobar79alloy
authored andcommitted
Make Vibration.vibrate compatible with TurboModules (#27951)
Summary: This PR fixes a compatibility issue with the Vibration module and TurboModules. The TurboModules spec doesn't allow nullable arguments of type Number, causing the following problem: ![IMG_3758](https://user-images.githubusercontent.com/1247834/73803879-10be6f80-4790-11ea-92d4-a008f0007681.PNG) [iOS] [Fixed] - Make Vibration library compatible with TurboModules. Pull Request resolved: #27951 Test Plan: Just submitted a PR to my own app to fix the issue [here](rainbow-me/rainbow#340) The problem should be reproducible on RNTester due to this line: https://github.com/facebook/react-native/blob/91f139b94118fe8db29728ea8ad855fc4a13f743/RNTester/js/examples/Vibration/VibrationExample.js#L66 and should be working on this branch. Reviewed By: TheSavior Differential Revision: D19761064 Pulled By: hramos fbshipit-source-id: 84f6b62a2734cc09d450e906b5866d4e9ce61124
1 parent 8858d87 commit f89c509

File tree

6 files changed

+12
-9
lines changed

6 files changed

+12
-9
lines changed

Libraries/FBReactNativeSpec/FBReactNativeSpec/FBReactNativeSpec.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -2785,7 +2785,7 @@ namespace facebook {
27852785
} // namespace facebook
27862786
@protocol NativeVibrationSpec <RCTBridgeModule, RCTTurboModule>
27872787

2788-
- (void)vibrate:(NSNumber *)pattern;
2788+
- (void)vibrate:(double)pattern;
27892789
- (void)vibrateByPattern:(NSArray *)pattern
27902790
repeat:(double)repeat;
27912791
- (void)cancel;

Libraries/Vibration/NativeVibration.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry';
1515

1616
export interface Spec extends TurboModule {
1717
+getConstants: () => {||};
18-
+vibrate: (pattern?: ?number) => void;
18+
+vibrate: (pattern: number) => void;
1919

2020
// Android only
2121
+vibrateByPattern: (pattern: Array<number>, repeat: number) => void;

Libraries/Vibration/RCTVibration.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ - (void)vibrate
2525
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
2626
}
2727

28-
RCT_EXPORT_METHOD(vibrate:(NSNumber *)pattern)
28+
RCT_EXPORT_METHOD(vibrate:(double)pattern)
2929
{
3030
[self vibrate];
3131
}

Libraries/Vibration/Vibration.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ const Platform = require('../Utilities/Platform');
2222

2323
let _vibrating: boolean = false;
2424
let _id: number = 0; // _id is necessary to prevent race condition.
25+
const _default_vibration_length = 400;
2526

2627
function vibrateByPattern(pattern: Array<number>, repeat: boolean = false) {
2728
if (_vibrating) {
2829
return;
2930
}
3031
_vibrating = true;
3132
if (pattern[0] === 0) {
32-
NativeVibration.vibrate();
33+
NativeVibration.vibrate(_default_vibration_length);
3334
pattern = pattern.slice(1);
3435
}
3536
if (pattern.length === 0) {
@@ -48,7 +49,7 @@ function vibrateScheduler(
4849
if (!_vibrating || id !== _id) {
4950
return;
5051
}
51-
NativeVibration.vibrate();
52+
NativeVibration.vibrate(_default_vibration_length);
5253
if (nextIndex >= pattern.length) {
5354
if (repeat) {
5455
nextIndex = 0;
@@ -70,7 +71,7 @@ const Vibration = {
7071
* See https://facebook.github.io/react-native/docs/vibration.html#vibrate
7172
*/
7273
vibrate: function(
73-
pattern: number | Array<number> = 400,
74+
pattern: number | Array<number> = _default_vibration_length,
7475
repeat: boolean = false,
7576
) {
7677
if (Platform.OS === 'android') {
@@ -86,7 +87,7 @@ const Vibration = {
8687
return;
8788
}
8889
if (typeof pattern === 'number') {
89-
NativeVibration.vibrate();
90+
NativeVibration.vibrate(pattern);
9091
} else if (Array.isArray(pattern)) {
9192
vibrateByPattern(pattern, repeat);
9293
} else {

ReactAndroid/src/main/java/com/facebook/fbreact/specs/NativeVibrationSpec.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ public NativeVibrationSpec(ReactApplicationContext reactContext) {
3131
public abstract void vibrateByPattern(ReadableArray pattern, double repeat);
3232

3333
@ReactMethod
34-
public abstract void vibrate(Double pattern);
34+
public abstract void vibrate(double pattern);
3535
}

ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public String getName() {
3232
}
3333

3434
@ReactMethod
35-
public void vibrate(int duration) {
35+
public void vibrate(double durationDouble) {
36+
int duration = (int) durationDouble;
37+
3638
Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
3739
if (v != null) {
3840
v.vibrate(duration);

0 commit comments

Comments
 (0)