Skip to content

Commit

Permalink
Make Vibration.vibrate compatible with TurboModules (#27951)
Browse files Browse the repository at this point in the history
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)

## Changelog

[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
  • Loading branch information
brunobar79 authored and facebook-github-bot committed Apr 8, 2020
1 parent 8e48dc0 commit 3904228
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2772,7 +2772,7 @@ namespace facebook {
} // namespace facebook
@protocol NativeVibrationSpec <RCTBridgeModule, RCTTurboModule>

- (void)vibrate:(NSNumber *)pattern;
- (void)vibrate:(double)pattern;
- (void)vibrateByPattern:(NSArray *)pattern
repeat:(double)repeat;
- (void)cancel;
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Vibration/NativeVibration.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry';

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

// Android only
+vibrateByPattern: (pattern: Array<number>, repeat: number) => void;
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Vibration/RCTVibration.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ - (void)vibrate
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}

RCT_EXPORT_METHOD(vibrate:(NSNumber *)pattern)
RCT_EXPORT_METHOD(vibrate:(double)pattern)
{
[self vibrate];
}
Expand Down
9 changes: 5 additions & 4 deletions Libraries/Vibration/Vibration.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ const Platform = require('../Utilities/Platform');

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

function vibrateByPattern(pattern: Array<number>, repeat: boolean = false) {
if (_vibrating) {
return;
}
_vibrating = true;
if (pattern[0] === 0) {
NativeVibration.vibrate();
NativeVibration.vibrate(_default_vibration_length);
pattern = pattern.slice(1);
}
if (pattern.length === 0) {
Expand All @@ -48,7 +49,7 @@ function vibrateScheduler(
if (!_vibrating || id !== _id) {
return;
}
NativeVibration.vibrate();
NativeVibration.vibrate(_default_vibration_length);
if (nextIndex >= pattern.length) {
if (repeat) {
nextIndex = 0;
Expand All @@ -70,7 +71,7 @@ const Vibration = {
* See https://reactnative.dev/docs/vibration.html#vibrate
*/
vibrate: function(
pattern: number | Array<number> = 400,
pattern: number | Array<number> = _default_vibration_length,
repeat: boolean = false,
) {
if (Platform.OS === 'android') {
Expand All @@ -86,7 +87,7 @@ const Vibration = {
return;
}
if (typeof pattern === 'number') {
NativeVibration.vibrate();
NativeVibration.vibrate(pattern);
} else if (Array.isArray(pattern)) {
vibrateByPattern(pattern, repeat);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ public NativeVibrationSpec(ReactApplicationContext reactContext) {
public abstract void vibrateByPattern(ReadableArray pattern, double repeat);

@ReactMethod
public abstract void vibrate(Double pattern);
public abstract void vibrate(double pattern);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2399,7 +2399,7 @@ namespace facebook {


static facebook::jsi::Value __hostFunction_NativeVibrationSpecJSI_vibrate(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<JavaTurboModule&>(turboModule).invokeJavaMethod(rt, VoidKind, "vibrate", "(Ljava/lang/Double;)V", args, count);
return static_cast<JavaTurboModule&>(turboModule).invokeJavaMethod(rt, VoidKind, "vibrate", "(D)V", args, count);
}

static facebook::jsi::Value __hostFunction_NativeVibrationSpecJSI_vibrateByPattern(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public String getName() {
}

@Override
public void vibrate(Double durationDouble) {
int duration = (int) durationDouble.doubleValue();
public void vibrate(double durationDouble) {
int duration = (int) durationDouble;

Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
if (v != null) {
Expand Down

0 comments on commit 3904228

Please sign in to comment.