UNPKG

@quadible/web-sdk

Version:

The web sdk for Quadible's behavioral authentication service.

63 lines 2.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const eventemitter2_1 = require("eventemitter2"); class GeolocationCollector extends eventemitter2_1.EventEmitter2 { static collectionIntervalMs = 5 * 60e3; // 5 minutes name = 'WebGeolocation'; data = []; isCollecting = false; lastCollectionAt; gestureListener = async () => { if (this.isCollecting) { if (!this.lastCollectionAt || (Date.now() - this.lastCollectionAt > GeolocationCollector.collectionIntervalMs)) { this.lastCollectionAt = Date.now(); const location = await this.getCurrentPosition().catch(error => { this.emit('error', error); return null; }); if (location) { this.data.push({ kind: "location" /* EventKind.Location */, timestamp: Date.now(), coords: { accuracy: location.coords.accuracy, altitude: location.coords.altitude, altitudeAccuracy: location.coords.altitudeAccuracy, heading: location.coords.heading, latitude: location.coords.latitude, longitude: location.coords.longitude, speed: location.coords.speed } }); } } } }; start() { this.isCollecting = true; this.registerGestureListeners(); } stop() { this.isCollecting = false; this.unregisterGestureListeners(); } flush() { return this.data.splice(0); } async isAvailable() { return Boolean(navigator?.geolocation?.getCurrentPosition); } registerGestureListeners() { window.addEventListener('click', this.gestureListener); window.addEventListener('keydown', this.gestureListener); } unregisterGestureListeners() { window.removeEventListener('click', this.gestureListener); window.removeEventListener('keydown', this.gestureListener); } async getCurrentPosition() { return await new Promise((resolve, reject) => navigator.geolocation.getCurrentPosition(resolve, reject)); } } exports.default = GeolocationCollector; //# sourceMappingURL=GeolocationCollector.js.map