Skip to content

Commit

Permalink
Don't call navigator.vibrate if it isn't supported (#463)
Browse files Browse the repository at this point in the history
* Don't call navigator.vibrate if it isn't supported

* Use supportsVibration
  • Loading branch information
jakearchibald authored May 21, 2019
1 parent 4a135ec commit c257ff0
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/services/preact-canvas/components/game/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Animator } from "src/rendering/animator";
import { Renderer } from "src/rendering/renderer";
import StateService from "src/services/state";
import { submitTime } from "src/services/state/best-times";
import { supportsVibration } from "src/services/state/vibration-preference";
import { bind } from "src/utils/bind";
import { GameChangeCallback } from "../..";
import { StateChange } from "../../../../gamelogic";
Expand Down Expand Up @@ -188,7 +189,7 @@ export default class Game extends Component<Props, State> {
this._tryAgainBtn
) {
this._tryAgainBtn.focus();
if (this.props.useVibration) {
if (this.props.useVibration && supportsVibration) {
navigator.vibrate(vibrationLength);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/services/preact-canvas/components/settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import { Component, h } from "preact";
import { bind } from "src/utils/bind.js";
import { isFeaturePhone } from "src/utils/static-display";
import { supportsVibration } from "../../../state/vibration-preference";
import About from "../about";
import { Close } from "../icons/additional";
import {
Expand Down Expand Up @@ -94,6 +95,7 @@ export default class Settings extends Component<Props, State> {
<button
class={useVibration ? btnOnStyle : btnOffStyle}
onClick={onVibrationPrefChange}
disabled={!supportsVibration}
>
Vibrate {useVibration ? "on" : "off"}
</button>
Expand Down
4 changes: 2 additions & 2 deletions src/services/state/vibration-preference.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { get, set } from "idb-keyval";

const DEFAULT: boolean = true;
export const supportsVibration = "vibrate" in navigator;

/**
* Set vibration preference (true means will vibrate)
Expand All @@ -17,5 +17,5 @@ export async function getVibrationPreference(): Promise<boolean> {
return vibrate;
}
// if no value is assigned to "vibrate", return default value
return DEFAULT;
return supportsVibration;
}

0 comments on commit c257ff0

Please sign in to comment.