From f97061f1c67d8691374f86efc8d5882aab214b14 Mon Sep 17 00:00:00 2001 From: RodrigoDornelles Date: Wed, 1 Jan 2025 23:22:12 -0300 Subject: [PATCH] fix: Blocked call to navigator.vibrate #38 --- src/event_feedback.ts | 14 +++++++++++--- tests/event_feedback.test.ts | 4 +++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/event_feedback.ts b/src/event_feedback.ts index 4b034b1..abd7deb 100644 --- a/src/event_feedback.ts +++ b/src/event_feedback.ts @@ -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) @@ -12,9 +12,17 @@ function vibration(self: ObjectGpz, device: Navigator) { function InstallEventFeedback(device: Window, pads: Array) { + 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)) } }) } diff --git a/tests/event_feedback.test.ts b/tests/event_feedback.test.ts index aab96b6..cfae7da 100644 --- a/tests/event_feedback.test.ts +++ b/tests/event_feedback.test.ts @@ -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", () => { @@ -44,6 +44,7 @@ test("vibration: calls device.vibrate without parameters", () => { test("InstallEventFeedback: install success", () => { const device = { + addEventListener: () => {}, navigator: { vibrate: jest.fn(), }, @@ -62,6 +63,7 @@ test("InstallEventFeedback: install success", () => { test("InstallEventFeedback: install failed", () => { const device = { + addEventListener: () => {}, navigator: {}, };