Home Do not allow call of a subscriber with the same argument twice until the first is done

# Do not allow call of a subscriber with the same argument twice until the first is done

user5376
1#
user5376 Published in September 21, 2018, 8:07 am

I am currently experimenting with beacons and I want to get the rssi measurements of the beacons in order to trigger an alarm function.

My problem is the following:

alarm function is triggered multiple times once the subscriber is called

What I want:

Do not allow alarm to execute more than once with the previous argument. For example if the alarm function is called with argument 'x' then someone cannot call alarm with 'x' but can call it with 'y' in order to avoid multiple pushes to the database.

My code (I am using noble for beacons and firebase for my db):

// alarm script

import { config, Device, db, auth, productsRef } from './index';
import * as noble from 'noble';
import * as firebase from 'firebase';

var acquiredRef = db.ref('acquired');

function condition(p) {
// a boolean function that does something

}

console.log('Alarm!');
let incident = {
date : Date(),
}
db.ref('alarms').push(incident);

}

if (require.main === module) {

noble.on('stateChange', function (state) {
if (state === 'poweredOn') noble.startScanning([], true);
else noble.stopScanning();
});

noble.on('discover', function(peripheral) {

if (condition(peripheral)) {
// query
.then(snap => {
}).catch(err => console.log(err))
}

});

}


I tried using queues and async-lock without success. It works the first time alarm is called and then has the same behaviour as above.