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

Don't call navigator.vibrate if it isn't supported #463

Merged
merged 2 commits into from
May 21, 2019
Merged
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
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;
}