How to use the mobx-state-tree.applySnapshot function in mobx-state-tree

To help you get started, we’ve selected a few mobx-state-tree 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 fadiquader / next.js-mst / stores / index.js View on Github external
// fetch: fetcher,
        alert: m => console.log(m) // Noop for demo: window.alert(m)
      }
    )
  }
  if(store === null) {
    store = Store.create(
      {},
      {
        // fetch: fetcher,
        alert: m => console.log(m) // Noop for demo: window.alert(m)
      }
    )
  }
  if (snapshot) {
    applySnapshot(store, snapshot)
  }
  return store
}
github fluid-notion / fluid-outliner / core / utils / mobx-helpers.ts View on Github external
export const safeApplySnapshot = (target: IStateTreeNode, snapshot: any) => {
    try {
        applySnapshot(target, snapshot)
        return true
    } catch (e) {
        // tslint:disable-next-line:no-console
        console.error(e)
        alert(
            "Saved outline was found to be corrupt." +
                "Please report this as an issue." +
                "It will help us debug if you could also attach the downloaded snapshot in the bug report"
        )
        try {
            downloadJSON(snapshot)
        } catch (e) {
            // tslint:disable-next-line:no-console
            console.error(e)
            alert("Failed to download snapshot")
        }
github skellock / mst-async-storage / src / with-async-storage.ts View on Github external
load: flow(function*() {
        const data = yield load(key)
        if (data) {
          applySnapshot(self, data)
        }

        // now monitor for changes
        if (autoSave) {
          enableSaving()
        }

        // send back the data
        return data
      }) as () => Promise,
github birkir / hekla / src / stores / Account.ts View on Github external
(async () => {
  Account.setIsChecking(true);

  try {
    const data = JSON.parse(await AsyncStorage.getItem('Account'));
    applySnapshot(Account.read, data.read);
    applySnapshot(Account.voted, data.voted);
    applySnapshot(Account.favorited, data.favorited);
    applySnapshot(Account.flagged, data.flagged);
    applySnapshot(Account.hidden, data.hidden);
  } catch (err) {
    console.log('Could not decode Account');
  }

  let username;
  let password;

  try {
    const credentials = await Keychain.getInternetCredentials('news.ycombinator.com');
    if (credentials) {
      username = credentials.username;
      password = credentials.password;
    } else {
      Account.setIsChecking(false);
      return false;
    }
github birkir / hekla / src / stores / Account.ts View on Github external
(async () => {
  Account.setIsChecking(true);

  try {
    const data = JSON.parse(await AsyncStorage.getItem('Account'));
    applySnapshot(Account.read, data.read);
    applySnapshot(Account.voted, data.voted);
    applySnapshot(Account.favorited, data.favorited);
    applySnapshot(Account.flagged, data.flagged);
    applySnapshot(Account.hidden, data.hidden);
  } catch (err) {
    console.log('Could not decode Account');
  }

  let username;
  let password;

  try {
    const credentials = await Keychain.getInternetCredentials('news.ycombinator.com');
    if (credentials) {
      username = credentials.username;
      password = credentials.password;
    } else {
github mobxjs / mst-gql / examples / 5-nextjs / utils / initModels.ts View on Github external
isServer: boolean,
  snapshot = null
): ModelCreationType {
  if (isServer) {
    store = RootStore.create(undefined, {
      gqlHttpClient: createHttpClient("http://localhost:3000/api/graphql"),
      ssr: true
    })
  }
  if (store === null) {
    store = RootStore.create(undefined, {
      gqlHttpClient: createHttpClient("http://localhost:3000/api/graphql")
    })
  }
  if (snapshot) {
    applySnapshot(store, snapshot)
  }
  return store
}
github mobxjs / mst-gql / examples / 5-nextjs / pages / _app.tsx View on Github external
export function getStore(snapshot = null): ModelCreationType {
  if (isServer || !store) {
    store = RootStore.create(undefined, {
      gqlHttpClient: createHttpClient("http://localhost:3000/api/graphql"),
      ssr: true
    })
  }
  if (snapshot) {
    applySnapshot(store, snapshot)
  }
  return store
}
github vonovak / react-navigation-mst-demo / App.js View on Github external
_loadPersistedState = async () => {
    const retrievedState = await AsyncStorage.getItem(appStatePersistenceKey);

    if (retrievedState) {
      const rootStoreJson = JSON.parse(retrievedState);
      if (RootStore.is(rootStoreJson)) {
        applySnapshot(this.rootStore, rootStoreJson);
      }
    }
  };
github mobxjs / mobx-state-tree / packages / mst-example-boxes / src / stores / domain-state.js View on Github external
const allBoxes = values(store.boxes)
        for (let i = 0; i < amount; i++) {
            store.addArrow(
                allBoxes[Math.floor(Math.random() * allBoxes.length)],
                allBoxes[Math.floor(Math.random() * allBoxes.length)]
            )
        }
    })
}

/**
    Save / Restore the state of the store while self module is hot reloaded
*/
if (module.hot) {
    if (module.hot.data && module.hot.data.store) {
        applySnapshot(store, module.hot.data.store)
    }
    module.hot.dispose(data => {
        data.store = getSnapshot(store)
    })
}
github zooniverse / front-end-monorepo / packages / app-content-pages / src / shared / stores / initStore.js View on Github external
function initStore (
  isServer,
  snapshot = null,
  panoptesClient = defaultClient,
) {
  if (isServer) {
    store = Store.create({}, { panoptesClient })
  }

  if (store === null) {
    store = Store.create({}, { panoptesClient })
  }

  if (snapshot) {
    applySnapshot(store, snapshot)
  }

  return store
}