From 66d688291cbe8202cb3c50cde51f3d145cf67f43 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Sun, 3 Jan 2021 12:51:50 +0530 Subject: [PATCH] Add locking mechanism around sendNotification Workaround for multiple messages being sent --- src/plugins/Accident.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/plugins/Accident.js b/src/plugins/Accident.js index c4a168a..9f31645 100644 --- a/src/plugins/Accident.js +++ b/src/plugins/Accident.js @@ -7,12 +7,21 @@ import Telegram from "../utils/telegram"; import { TG_API, TG_USERID } from "../config"; function sendNotification(close) { + if (window.sendingAccidentAlert) + return; + window.sending = true; const bot = new Telegram(TG_API); bot.sendMessage(TG_USERID, "User detected in an accident. Location has been attached below."); navigator.geolocation.getCurrentPosition( - position => bot.sendLocation( TG_USERID, - position.coords.latitude, position.coords.longitude), - () => bot.sendMessage(TG_USERID, "Error retrieving location") + position => { + bot.sendLocation( TG_USERID, + position.coords.latitude, position.coords.longitude); + window.sendingAccidentAlert = false; + }, + () => { + bot.sendMessage(TG_USERID, "Error retrieving location"); + window.sendingAccidentAlert = false; + } ); setTimeout(close, 2000); }