UNPKG

react-native-avo-inspector

Version:

[![npm version](https://badge.fury.io/js/react-native-avo-inspector.svg)](https://badge.fury.io/js/react-native-avo-inspector)

138 lines (137 loc) 5.06 kB
import { AvoSchemaParser } from "./AvoSchemaParser"; import { deepEquals } from "./utils"; export class AvoDeduplicator { constructor() { this.avoFunctionsEvents = {}; this.manualEvents = {}; this.avoFunctionsEventsParams = {}; this.manualEventsParams = {}; } shouldRegisterEvent(eventName, params, fromAvoFunction) { this.clearOldEvents(); if (fromAvoFunction) { this.avoFunctionsEvents[Date.now()] = eventName; this.avoFunctionsEventsParams[eventName] = params; } else { this.manualEvents[Date.now()] = eventName; this.manualEventsParams[eventName] = params; } const checkInAvoFunctions = !fromAvoFunction; return !this.hasSameEventAs(eventName, params, checkInAvoFunctions); } hasSameEventAs(eventName, params, checkInAvoFunctions) { let result = false; if (checkInAvoFunctions) { if (this.lookForEventIn(eventName, params, this.avoFunctionsEventsParams)) { result = true; } } else { if (this.lookForEventIn(eventName, params, this.manualEventsParams)) { result = true; } } if (result) { delete this.avoFunctionsEventsParams[eventName]; delete this.manualEventsParams[eventName]; } return result; } lookForEventIn(eventName, params, eventsStorage) { for (const otherEventName in eventsStorage) { if (eventsStorage.hasOwnProperty(otherEventName)) { if (otherEventName === eventName) { const otherParams = eventsStorage[eventName]; if (otherParams && deepEquals(params, otherParams)) { return true; } } } } return false; } hasSeenEventParams(params, checkInAvoFunctions) { let result = false; if (checkInAvoFunctions) { if (this.lookForEventParamsIn(params, this.avoFunctionsEventsParams)) { result = true; } } else { if (this.lookForEventParamsIn(params, this.manualEventsParams)) { result = true; } } return result; } lookForEventParamsIn(params, eventsStorage) { for (const otherEventName in eventsStorage) { if (eventsStorage.hasOwnProperty(otherEventName)) { const otherParams = eventsStorage[otherEventName]; if (otherParams && deepEquals(params, otherParams)) { return true; } } } return false; } async shouldRegisterSchemaFromManually(eventName, eventSchema) { this.clearOldEvents(); return !(await this.hasSameShapeInAvoFunctionsAs(eventName, eventSchema)); } async hasSameShapeInAvoFunctionsAs(eventName, eventSchema) { let result = false; if (await this.lookForEventSchemaIn(eventName, eventSchema, this.avoFunctionsEventsParams)) { result = true; } if (result) { delete this.avoFunctionsEventsParams[eventName]; } return result; } async lookForEventSchemaIn(eventName, eventSchema, eventsStorage) { for (const otherEventName in eventsStorage) { if (eventsStorage.hasOwnProperty(otherEventName)) { if (otherEventName === eventName) { const otherSchema = await AvoSchemaParser.extractSchema(eventsStorage[eventName]); if (otherSchema && deepEquals(eventSchema, otherSchema)) { return true; } } } } return false; } clearOldEvents() { const now = Date.now(); const msToConsiderOld = 300; for (const time in this.avoFunctionsEvents) { if (this.avoFunctionsEvents.hasOwnProperty(time)) { const timestamp = Number(time) || 0; if (now - timestamp > msToConsiderOld) { const eventName = this.avoFunctionsEvents[time]; delete this.avoFunctionsEvents[time]; delete this.avoFunctionsEventsParams[eventName]; } } } for (const time in this.manualEvents) { if (this.manualEvents.hasOwnProperty(time)) { const timestamp = Number(time) || 0; if (now - timestamp > msToConsiderOld) { const eventName = this.manualEvents[time]; delete this.manualEvents[time]; delete this.manualEventsParams[eventName]; } } } } // used in tests _clearEvents() { this.avoFunctionsEvents = {}; this.manualEvents = {}; this.avoFunctionsEventsParams = {}; this.manualEventsParams = {}; } }