From 33956ba58058af6e5ed983fa9b7acb7069319b4b Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Thu, 27 Aug 2020 16:48:26 -0400 Subject: [PATCH 1/2] fix(integrations): Add localforage typing --- packages/integrations/src/offline.ts | 59 ++++++++++++++++------------ 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/packages/integrations/src/offline.ts b/packages/integrations/src/offline.ts index 51bcb6b80d73..0a38af8448d8 100644 --- a/packages/integrations/src/offline.ts +++ b/packages/integrations/src/offline.ts @@ -1,7 +1,6 @@ import { Event, EventProcessor, Hub, Integration } from '@sentry/types'; import { getGlobalObject, logger, uuid4 } from '@sentry/utils'; -// @ts-ignore: Module '"localforage"' has no default export. -import localforage from 'localforage'; +import * as localForage from 'localforage'; /** * cache offline errors and send when connected @@ -43,7 +42,7 @@ export class Offline implements Integration { public constructor(options: { maxStoredEvents?: number } = {}) { this.global = getGlobalObject(); this.maxStoredEvents = options.maxStoredEvents || 30; // set a reasonable default - this.offlineEventStore = localforage.createInstance({ + this.offlineEventStore = localForage.createInstance({ name: 'sentry/offlineEventStore', }); @@ -68,9 +67,11 @@ export class Offline implements Integration { if ('navigator' in this.global && 'onLine' in this.global.navigator && !this.global.navigator.onLine) { this._cacheEvent(event) .then((_event: Event): Promise => this._enforceMaxEvents()) - .catch((_error): void => { - logger.warn('could not cache event while offline'); - }); + .catch( + (_error): void => { + logger.warn('could not cache event while offline'); + }, + ); // return null on success or failure, because being offline will still result in an error return null; @@ -103,10 +104,12 @@ export class Offline implements Integration { const events: Array<{ event: Event; cacheKey: string }> = []; return this.offlineEventStore - .iterate((event: Event, cacheKey: string, _index: number): void => { - // aggregate events - events.push({ cacheKey, event }); - }) + .iterate( + (event: Event, cacheKey: string, _index: number): void => { + // aggregate events + events.push({ cacheKey, event }); + }, + ) .then( (): Promise => // this promise resolves when the iteration is finished @@ -118,9 +121,11 @@ export class Offline implements Integration { .map(event => event.cacheKey), ), ) - .catch((_error): void => { - logger.warn('could not enforce max events'); - }); + .catch( + (_error): void => { + logger.warn('could not enforce max events'); + }, + ); } /** @@ -142,18 +147,22 @@ export class Offline implements Integration { * send all events */ private async _sendEvents(): Promise { - return this.offlineEventStore.iterate((event: Event, cacheKey: string, _index: number): void => { - if (this.hub) { - const newEventId = this.hub.captureEvent(event); - - if (newEventId) { - this._purgeEvent(cacheKey).catch((_error): void => { - logger.warn('could not purge event from cache'); - }); + return this.offlineEventStore.iterate( + (event: Event, cacheKey: string, _index: number): void => { + if (this.hub) { + const newEventId = this.hub.captureEvent(event); + + if (newEventId) { + this._purgeEvent(cacheKey).catch( + (_error): void => { + logger.warn('could not purge event from cache'); + }, + ); + } + } else { + logger.warn('no hub found - could not send cached event'); } - } else { - logger.warn('no hub found - could not send cached event'); - } - }); + }, + ); } } From 9031f6706603eef9de413c80d16237af241563e5 Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Fri, 28 Aug 2020 08:50:47 +0200 Subject: [PATCH 2/2] fix: yarn fix:prettier --- packages/integrations/src/offline.ts | 54 ++++++++++++---------------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/packages/integrations/src/offline.ts b/packages/integrations/src/offline.ts index 0a38af8448d8..dac92c0e5619 100644 --- a/packages/integrations/src/offline.ts +++ b/packages/integrations/src/offline.ts @@ -67,11 +67,9 @@ export class Offline implements Integration { if ('navigator' in this.global && 'onLine' in this.global.navigator && !this.global.navigator.onLine) { this._cacheEvent(event) .then((_event: Event): Promise => this._enforceMaxEvents()) - .catch( - (_error): void => { - logger.warn('could not cache event while offline'); - }, - ); + .catch((_error): void => { + logger.warn('could not cache event while offline'); + }); // return null on success or failure, because being offline will still result in an error return null; @@ -104,12 +102,10 @@ export class Offline implements Integration { const events: Array<{ event: Event; cacheKey: string }> = []; return this.offlineEventStore - .iterate( - (event: Event, cacheKey: string, _index: number): void => { - // aggregate events - events.push({ cacheKey, event }); - }, - ) + .iterate((event: Event, cacheKey: string, _index: number): void => { + // aggregate events + events.push({ cacheKey, event }); + }) .then( (): Promise => // this promise resolves when the iteration is finished @@ -121,11 +117,9 @@ export class Offline implements Integration { .map(event => event.cacheKey), ), ) - .catch( - (_error): void => { - logger.warn('could not enforce max events'); - }, - ); + .catch((_error): void => { + logger.warn('could not enforce max events'); + }); } /** @@ -147,22 +141,18 @@ export class Offline implements Integration { * send all events */ private async _sendEvents(): Promise { - return this.offlineEventStore.iterate( - (event: Event, cacheKey: string, _index: number): void => { - if (this.hub) { - const newEventId = this.hub.captureEvent(event); - - if (newEventId) { - this._purgeEvent(cacheKey).catch( - (_error): void => { - logger.warn('could not purge event from cache'); - }, - ); - } - } else { - logger.warn('no hub found - could not send cached event'); + return this.offlineEventStore.iterate((event: Event, cacheKey: string, _index: number): void => { + if (this.hub) { + const newEventId = this.hub.captureEvent(event); + + if (newEventId) { + this._purgeEvent(cacheKey).catch((_error): void => { + logger.warn('could not purge event from cache'); + }); } - }, - ); + } else { + logger.warn('no hub found - could not send cached event'); + } + }); } }