Skip to content

Commit

Permalink
If async-storage or expo-file-system is not installed, the SDK wi…
Browse files Browse the repository at this point in the history
…ll fallback to `persistence: memory` and log a warning (#146)

* If  or  is not installed, the SDK will fallback to  and log a warning

* fix

* add test

* fix

* fix

* fix

* fix

* fix
  • Loading branch information
marandaneto authored Dec 21, 2023
1 parent 1e7bd5d commit 7026e98
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions posthog-react-native/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 12 additions & 2 deletions posthog-react-native/src/posthog-rn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,18 @@ 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', customAsyncStorage: undefined })
)
}
} else {
posthog = Promise.resolve(new PostHog(apiKey, options))
}
Expand Down

0 comments on commit 7026e98

Please sign in to comment.