From e6ff59341b95373ca10d6c045b1b81a49f84aff2 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 21 Dec 2023 13:21:54 +0100 Subject: [PATCH 1/8] If or is not installed, the SDK will fallback to and log a warning --- posthog-react-native/CHANGELOG.md | 4 ++++ posthog-react-native/src/posthog-rn.ts | 10 ++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/posthog-react-native/CHANGELOG.md b/posthog-react-native/CHANGELOG.md index 1216c642..164f2e78 100644 --- a/posthog-react-native/CHANGELOG.md +++ b/posthog-react-native/CHANGELOG.md @@ -1,3 +1,7 @@ +# 2.9.2 - 2023-12-21 + +1. If `async-storage` or `expo-file-system` is not installed, the SDK will fallback to `persistence: memory` and log a warning + # 2.9.1 - 2023-12-14 1. `getPersistedProperty` uses Nullish Coalescing operator to fallback to `undefined` only if the property is not found diff --git a/posthog-react-native/src/posthog-rn.ts b/posthog-react-native/src/posthog-rn.ts index 1129961b..6415039b 100644 --- a/posthog-react-native/src/posthog-rn.ts +++ b/posthog-react-native/src/posthog-rn.ts @@ -46,8 +46,14 @@ export class PostHog extends PostHogCore { if (!posthog) { const persistence = options?.persistence ?? 'file' if (persistence === 'file') { - const storage = new SemiAsyncStorage(options?.customAsyncStorage || buildOptimisiticAsyncStorage()) - posthog = storage.preloadAsync().then(() => new PostHog(apiKey, options, storage)) + try { + const storage = new SemiAsyncStorage(options?.customAsyncStorage || buildOptimisiticAsyncStorage()) + posthog = storage.preloadAsync().then(() => new PostHog(apiKey, options, storage)) + } catch (error) { + console.error( + 'PostHog was unable to initialise with persistence set to "file". Falling back to "memory" persistence.', error) + posthog = Promise.resolve(new PostHog(apiKey, { ...options, persistence: 'memory' })) + } } else { posthog = Promise.resolve(new PostHog(apiKey, options)) } From fc82c2e83c2c20fe15935a78e723bfa98a718bea Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 21 Dec 2023 13:47:42 +0100 Subject: [PATCH 2/8] fix --- posthog-react-native/src/posthog-rn.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/posthog-react-native/src/posthog-rn.ts b/posthog-react-native/src/posthog-rn.ts index 6415039b..7039b45e 100644 --- a/posthog-react-native/src/posthog-rn.ts +++ b/posthog-react-native/src/posthog-rn.ts @@ -51,7 +51,9 @@ export class PostHog extends PostHogCore { posthog = storage.preloadAsync().then(() => new PostHog(apiKey, options, storage)) } catch (error) { console.error( - 'PostHog was unable to initialise with persistence set to "file". Falling back to "memory" persistence.', error) + 'PostHog was unable to initialise with persistence set to "file". Falling back to "memory" persistence.', + error + ) posthog = Promise.resolve(new PostHog(apiKey, { ...options, persistence: 'memory' })) } } else { From 7a9f29b709495ff001f85ba3a9c329d533a5eabf Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 21 Dec 2023 14:03:11 +0100 Subject: [PATCH 3/8] add test --- posthog-react-native/test/posthog.spec.ts | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/posthog-react-native/test/posthog.spec.ts b/posthog-react-native/test/posthog.spec.ts index a05c3438..803a16c9 100644 --- a/posthog-react-native/test/posthog.spec.ts +++ b/posthog-react-native/test/posthog.spec.ts @@ -71,6 +71,30 @@ describe('PostHog React Native', () => { expect(posthog.getDistinctId()).toEqual('bar') }) + it('should initialize with in memory storage if no storage available or broken', async () => { + // preloadAsync calls getItem + const myBrokenStorageMock = (): PostHogCustomAsyncStorage => { + return { + async getItem(key: string) { + throw new Error('error') + }, + + async setItem(key: string, value: string) { + throw new Error('error') + }, + } + } + + posthog = await PostHog.initAsync('test-token', { + bootstrap: { distinctId: 'bar' }, + persistence: 'file', + customAsyncStorage: myBrokenStorageMock(), + flushInterval: 0, + }) + expect(posthog.getAnonymousId()).toEqual('bar') + expect(posthog.getDistinctId()).toEqual('bar') + }) + it('should allow customising of native app properties', async () => { posthog = await PostHog.initAsync('test-token', { customAppProperties: { $app_name: 'custom' }, From c99173b1cde096a2d3b1605a01c1810a4ba97156 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 21 Dec 2023 14:03:46 +0100 Subject: [PATCH 4/8] fix --- posthog-react-native/test/posthog.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posthog-react-native/test/posthog.spec.ts b/posthog-react-native/test/posthog.spec.ts index 803a16c9..2867476b 100644 --- a/posthog-react-native/test/posthog.spec.ts +++ b/posthog-react-native/test/posthog.spec.ts @@ -72,7 +72,7 @@ describe('PostHog React Native', () => { }) it('should initialize with in memory storage if no storage available or broken', async () => { - // preloadAsync calls getItem + // preloadAsync calls getItem, getItem throws an error const myBrokenStorageMock = (): PostHogCustomAsyncStorage => { return { async getItem(key: string) { From 26b810065d747cc87eee7bf4a3b5da8044f022d4 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 21 Dec 2023 14:06:47 +0100 Subject: [PATCH 5/8] fix --- posthog-core/src/index.ts | 2 +- posthog-react-native/test/posthog.spec.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/posthog-core/src/index.ts b/posthog-core/src/index.ts index b1a80683..8ed52531 100644 --- a/posthog-core/src/index.ts +++ b/posthog-core/src/index.ts @@ -37,7 +37,7 @@ class PostHogFetchNetworkError extends Error { constructor(public error: unknown) { // TRICKY: "cause" is a newer property but is just ignored otherwise. Cast to any to ignore the type issue. - // @ts-ignore + // @ts-expect-error super('Network error while fetching PostHog', error instanceof Error ? { cause: error } : {}) } } diff --git a/posthog-react-native/test/posthog.spec.ts b/posthog-react-native/test/posthog.spec.ts index 2867476b..8add68b0 100644 --- a/posthog-react-native/test/posthog.spec.ts +++ b/posthog-react-native/test/posthog.spec.ts @@ -76,11 +76,11 @@ describe('PostHog React Native', () => { const myBrokenStorageMock = (): PostHogCustomAsyncStorage => { return { async getItem(key: string) { - throw new Error('error') + throw new Error(`error getting ${key}`) }, async setItem(key: string, value: string) { - throw new Error('error') + throw new Error(`error setting ${key} with the value ${value}`) }, } } From aabf5bb5322472d282a0a92225b4532827638973 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 21 Dec 2023 14:08:45 +0100 Subject: [PATCH 6/8] fix --- posthog-core/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posthog-core/src/index.ts b/posthog-core/src/index.ts index 8ed52531..b1a80683 100644 --- a/posthog-core/src/index.ts +++ b/posthog-core/src/index.ts @@ -37,7 +37,7 @@ class PostHogFetchNetworkError extends Error { constructor(public error: unknown) { // TRICKY: "cause" is a newer property but is just ignored otherwise. Cast to any to ignore the type issue. - // @ts-expect-error + // @ts-ignore super('Network error while fetching PostHog', error instanceof Error ? { cause: error } : {}) } } From 34895006d20eb54cd5f6437572c7fb0a25124d4d Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 21 Dec 2023 14:18:51 +0100 Subject: [PATCH 7/8] fix --- posthog-react-native/src/posthog-rn.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/posthog-react-native/src/posthog-rn.ts b/posthog-react-native/src/posthog-rn.ts index 7039b45e..29a73ecd 100644 --- a/posthog-react-native/src/posthog-rn.ts +++ b/posthog-react-native/src/posthog-rn.ts @@ -54,7 +54,9 @@ export class PostHog extends PostHogCore { 'PostHog was unable to initialise with persistence set to "file". Falling back to "memory" persistence.', error ) - posthog = Promise.resolve(new PostHog(apiKey, { ...options, persistence: 'memory' })) + posthog = Promise.resolve( + new PostHog(apiKey, { ...options, persistence: 'memory', customAsyncStorage: undefined }) + ) } } else { posthog = Promise.resolve(new PostHog(apiKey, options)) From 5d77206304e38e6c5ac1383f055e7e2a2eff9178 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 21 Dec 2023 14:27:53 +0100 Subject: [PATCH 8/8] fix --- posthog-react-native/test/posthog.spec.ts | 24 ----------------------- 1 file changed, 24 deletions(-) diff --git a/posthog-react-native/test/posthog.spec.ts b/posthog-react-native/test/posthog.spec.ts index 8add68b0..a05c3438 100644 --- a/posthog-react-native/test/posthog.spec.ts +++ b/posthog-react-native/test/posthog.spec.ts @@ -71,30 +71,6 @@ describe('PostHog React Native', () => { expect(posthog.getDistinctId()).toEqual('bar') }) - it('should initialize with in memory storage if no storage available or broken', async () => { - // preloadAsync calls getItem, getItem throws an error - const myBrokenStorageMock = (): PostHogCustomAsyncStorage => { - return { - async getItem(key: string) { - throw new Error(`error getting ${key}`) - }, - - async setItem(key: string, value: string) { - throw new Error(`error setting ${key} with the value ${value}`) - }, - } - } - - posthog = await PostHog.initAsync('test-token', { - bootstrap: { distinctId: 'bar' }, - persistence: 'file', - customAsyncStorage: myBrokenStorageMock(), - flushInterval: 0, - }) - expect(posthog.getAnonymousId()).toEqual('bar') - expect(posthog.getDistinctId()).toEqual('bar') - }) - it('should allow customising of native app properties', async () => { posthog = await PostHog.initAsync('test-token', { customAppProperties: { $app_name: 'custom' },