Skip to content

Commit

Permalink
fix: Blocked call to navigator.vibrate #38
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoDornelles committed Jan 2, 2025
1 parent 31f861f commit f97061f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/event_feedback.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ObjectGpz } from "./interface";

function vibration(self: ObjectGpz, device: Navigator) {
function vibration(self: ObjectGpz, device: Navigator, first_interact: {click: boolean}) {
const find = (state, index) => state && self.stateOld[index] == false
const action = self.stateNew.findIndex(find)
if (action !== -1) {
if (action !== -1 && first_interact.click) {
const vibration = self.canvas.dataset.gpzVibrate ?? '200'
const animation = vibration.split(' ').map(intensity => Number(intensity))
device.vibrate(animation)
Expand All @@ -12,9 +12,17 @@ function vibration(self: ObjectGpz, device: Navigator) {

function InstallEventFeedback(device: Window, pads: Array<ObjectGpz>)
{
let first_interact = {
click: false
}

device.addEventListener('click', () => {
first_interact.click = true
})

pads.forEach(self => {
if ('navigator' in device && 'vibrate' in device.navigator) {
self.chain.push(pad => vibration(pad, device.navigator))
self.chain.push(pad => vibration(pad, device.navigator, first_interact))
}
})
}
Expand Down
4 changes: 3 additions & 1 deletion tests/event_feedback.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, test, jest } from "bun:test";
import { InstallEventFeedback, vibration } from "../src/event_feedback";

const vibrationMock = (a: any, b: any) => vibration(a, b);
const vibrationMock = (a: any, b: any) => vibration(a, b, {click: true});
const InstallEventFeedbackMock = (a: any, b: any) => InstallEventFeedback(a, b);

test("vibration: calls device.vibrate with correct parameters", () => {
Expand Down Expand Up @@ -44,6 +44,7 @@ test("vibration: calls device.vibrate without parameters", () => {

test("InstallEventFeedback: install success", () => {
const device = {
addEventListener: () => {},
navigator: {
vibrate: jest.fn(),
},
Expand All @@ -62,6 +63,7 @@ test("InstallEventFeedback: install success", () => {

test("InstallEventFeedback: install failed", () => {
const device = {
addEventListener: () => {},
navigator: {},
};

Expand Down

0 comments on commit f97061f

Please sign in to comment.