fix(@capacitor/haptics): prevent thread overload by reusing CHHapticEngine#2340
fix(@capacitor/haptics): prevent thread overload by reusing CHHapticEngine#2340GorkaMinus wants to merge 2 commits intoionic-team:7.xfrom
Conversation
|
Hey @GorkaMinus, sorry for the delay, I changed the branch to 7.x, as since 8.x, the capacitor haptics plugin now lives in its own repository - https://github.com/ionic-team/capacitor-haptics. If you can, open an equivalent PR to that repository in order to be able to merge it into the latest version. If you have any questions feel free to ask, thank you! |
| // Haptic Engine Management | ||
| private var hapticEngine: CHHapticEngine? | ||
| private var idleTimer: Timer? | ||
| private let idleInterval: TimeInterval = 5.0 // Engine will shut down after 5 seconds of inactivity |
There was a problem hiding this comment.
This limits the duration to 5 seconds as it doesn't really tracks inactivity (when it stops) but from when it was last started.
Maybe you can add a duration param in resetIdleTimer to pass the vibration duration and add the 5 seconds after it stoped instead?
| * Initializes the haptic engine and starts it. | ||
| */ | ||
| private func initializeEngine() { | ||
| if CHHapticEngine.capabilitiesForHardware().supportsHaptics { |
There was a problem hiding this comment.
initializeEngine is only called inside a supportsHaptics check inside vibrate, so no need to double check here, the supportsHaptics if can be removed.
Fix: Prevent iOS app freeze by reusing
CHHapticEngineinstanceThis PR addresses an issue where calling
Haptics.vibrate()rapidly (e.g., from a fast-scrolling picker in my case) causes the app to hang or crash on iOS. The problem stems from creating a newCHHapticEngineinstance on each call, which leads to excessive thread creation and eventual system overload.Solution
CHHapticEngineinstance across multiple vibration calls.This fix improves performance and stability, especially in high-frequency vibration scenarios.
Related Issue #1960