Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Vibration.vibrate compatible with TurboModules #27951

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:(nonnull NSNumber *)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://facebook.github.io/react-native/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 @@ -18,6 +18,7 @@
import com.facebook.react.bridge.ReactModuleWithSpec;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.turbomodule.core.interfaces.TurboModule;
import javax.annotation.Nonnull;

public abstract class NativeVibrationSpec extends ReactContextBaseJavaModule implements ReactModuleWithSpec, TurboModule {
public NativeVibrationSpec(ReactApplicationContext reactContext) {
Expand All @@ -31,5 +32,5 @@ public NativeVibrationSpec(ReactApplicationContext reactContext) {
public abstract void vibrateByPattern(ReadableArray pattern, double repeat);

@ReactMethod
public abstract void vibrate(Double pattern);
public abstract void vibrate(@Nonnull Double pattern);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package com.facebook.react.modules.vibration;

import android.annotation.SuppressLint;
import androidx.annotation.NonNull;
import android.content.Context;
import android.os.Vibrator;
import com.facebook.fbreact.specs.NativeVibrationSpec;
Expand All @@ -31,7 +32,7 @@ public String getName() {
}

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

Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
Expand Down