Last active
August 5, 2022 15:14
-
-
Save gzimbron/b345a23f0eb3a5698917ea9ee3f60b96 to your computer and use it in GitHub Desktop.
Wait for DataStore Load
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { browser } from '$app/env'; | |
import { PubSub } from '@aws-amplify/pubsub'; | |
import { Amplify, DataStore, Hub } from 'aws-amplify'; | |
import awsmobile from 'src/aws-exports'; | |
export default async function startAmplifyConfig() { | |
if (!browser) return; | |
Amplify.configure({ ...awsmobile, ssr: true }); | |
PubSub.configure({ ...awsmobile }); | |
DataStore.configure(); | |
await DataStore.clear(); | |
await DataStore.start(); | |
await waitForDataStoreLoad(); | |
} | |
const waitForDataStoreLoad = async () => { | |
//promesa que se ejecuta cuando el datastore esta listo | |
await new Promise<void>((resolve) => { | |
Hub.listen('datastore', async (hubData) => { | |
const { event } = hubData.payload; | |
if (event === 'ready') { | |
resolve(); | |
} | |
}); | |
}); | |
}; |
Thanks for posting this - solved a massive issue I was having which I thought was a race condition!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, I'm using your code with the await method on react native. I figured out that the problem occurs when i use the datastore.create, any ideas for why this happens?