How to use the mobx-keystone.fromSnapshot function in mobx-keystone

To help you get started, we’ve selected a few mobx-keystone examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github xaviergonz / mobx-keystone / packages / site / src / examples / clientServer / appInstance.tsx View on Github external
function initAppInstance() {
  // we get the snapshot from the server, which is a serializable object
  const rootStoreSnapshot = server.getInitialState()

  // and hydrate it into a proper object
  const rootStore = fromSnapshot(rootStoreSnapshot)

  let serverAction = false
  const runServerActionLocally = (actionCall: SerializedActionCallWithModelIdOverrides) => {
    let wasServerAction = serverAction
    serverAction = true
    try {
      // in clients we use the sync new model ids version to make sure that
      // any model ids that were generated in the server side end up being
      // the same in the client side
      applySerializedActionAndSyncNewModelIds(rootStore, actionCall)
    } finally {
      serverAction = wasServerAction
    }
  }

  // listen to action messages to be replicated into the local root store