IjzerenHein / firestorter

Use Google Firestore in React with zero effort, using MobX 🤘
http://firestorter.com
MIT License
378 stars 50 forks source link

Suggestion: new mode type "Once" #65

Closed damonmaria closed 5 years ago

damonmaria commented 5 years ago

I'm creating a Document inside an @computed property. I don't want the doc to update with changes (it's the initialValues passed to a redux-form and I don't want it changing when the form submits and updates Firestore). So I need to put the mode to off and then fetch() it. But I can't do the fetch when creating the doc (it's in @computed so bad idea) so I instead need to have an autorun() to load it:

    autorun(() => {
      if (
        this.initialDoc &&
        !this.initialDoc.isLoading &&
        !this.initialDoc.hasData
      ) {
        this.initialDoc.fetch().catch(log.error)
      }
    })

Am I missing some better way to do this? If not...

Could we have a mode called something like once that is like off but also eagerly fetches the snapshot? That way I could create it and use the Document's isLoading and data just like in other modes.

damonmaria commented 5 years ago

@IjzerenHein Are you OK for me to attempt a PR for this? Do you have any suggestions / comments / ideas regarding this suggestion?

IjzerenHein commented 5 years ago

@damonmaria Yes, please go ahead, I forgot to get back on this. I do have one concern though. Maybe it's just my gut feeling and I'm totally wrong, but I think that the actual cause of the problem at hand is somewhere else, and solving it here may not be the right place. I think that solving the problem at the source would yield better results with less possible side effects. E.g., by making a copy of the data when you show the form.

damonmaria commented 5 years ago

After some refactoring the need for this has gone away... so you were exactly right.