From 8795eb0b479fa9eaa7ed3dabe9dd8e03e45552c4 Mon Sep 17 00:00:00 2001 From: Sigve Kvalsvik Date: Wed, 11 Oct 2017 15:29:47 +0200 Subject: [PATCH] Simplify and improve app init chain #531 --- app/App.jsx | 55 +++++++--------- .../Dashboard/DashboardContainer.jsx | 5 +- app/components/LoadingIndicator.jsx | 1 + app/components/Wallet/BalanceClaimActive.jsx | 2 +- app/routerTransition.js | 64 ++++++++++--------- app/stores/AccountRefsStore.js | 41 +++++++----- app/stores/AccountStore.js | 7 +- app/stores/AddressIndex.js | 4 +- package.json | 2 +- 9 files changed, 92 insertions(+), 89 deletions(-) diff --git a/app/App.jsx b/app/App.jsx index 59b72b3add..508bac3bd1 100644 --- a/app/App.jsx +++ b/app/App.jsx @@ -21,8 +21,8 @@ import TransactionConfirm from "./components/Blockchain/TransactionConfirm"; import WalletUnlockModal from "./components/Wallet/WalletUnlockModal"; import BrowserSupportModal from "./components/Modal/BrowserSupportModal"; import Footer from "./components/Layout/Footer"; -import Incognito from "./components/Layout/Incognito"; -import { isIncognito } from "feature_detect"; +// import Incognito from "./components/Layout/Incognito"; +// import { isIncognito } from "feature_detect"; class App extends React.Component { @@ -35,7 +35,7 @@ class App extends React.Component { let syncFail = ChainStore.subError && (ChainStore.subError.message === "ChainStore sync error, please check your system clock") ? true : false; this.state = { - loading: true, + loading: false, synced: ChainStore.subscribed, syncFail, theme: SettingsStore.getState().settings.get("themes"), @@ -58,30 +58,19 @@ class App extends React.Component { componentDidMount() { try { - isIncognito(function(incognito){ - this.setState({incognito}); - }.bind(this)); - NotificationStore.listen(this._onNotificationChange.bind(this)); SettingsStore.listen(this._onSettingsChange); - - ChainStore.init().then(() => { - this.setState({synced: true}); - Promise.all([ - AccountStore.loadDbData(Apis.instance().chainId) - ]).then(() => { - AccountStore.tryToSetCurrentAccount(); - this.setState({loading: false}); - }).catch(error => { - console.log("[App.jsx] ----- ERROR ----->", error); - this.setState({loading: false}); - }); - }).catch(error => { - console.log("[App.jsx] ----- ChainStore.init error ----->", error); - let syncFail = ChainStore.subError && (ChainStore.subError.message === "ChainStore sync error, please check your system clock") ? true : false; - - this.setState({loading: false, synced: false, syncFail}); + ChainStore.subscribe(() => { + if (ChainStore.subscribed !== this.state.synced || ChainStore.subError) { + let syncFail = ChainStore.subError && (ChainStore.subError.message === "ChainStore sync error, please check your system clock") ? true : false; + this.setState({ + synced: ChainStore.subscribed, + syncFail + }); + } }); + + AccountStore.tryToSetCurrentAccount(); } catch(e) { console.error("e:", e); } @@ -152,21 +141,23 @@ class App extends React.Component { // } render() { - let {isMobile, showChat, dockedChat, theme, incognito, incognitoWarningDismissed } = this.state; + let {isMobile, showChat, dockedChat, theme } = this.state; let content = null; let showFooter = this.props.location.pathname.indexOf("market") === -1; - - if(incognito && !incognitoWarningDismissed){ - content = ( - - ); - } else if (this.state.syncFail) { + // if(incognito && !incognitoWarningDismissed){ + // content = ( + // + // ); + // } else + if (this.state.syncFail) { content = ( ); } else if (this.state.loading) { - content =
; + content =
+ +
; } else if (this.props.location.pathname === "/init-error") { content =
{this.props.children}
; } else { diff --git a/app/components/Dashboard/DashboardContainer.jsx b/app/components/Dashboard/DashboardContainer.jsx index a672042913..7258c782a2 100644 --- a/app/components/Dashboard/DashboardContainer.jsx +++ b/app/components/Dashboard/DashboardContainer.jsx @@ -26,10 +26,7 @@ class DashboardContainer extends React.Component { }, lowVolumeMarkets: () => { return MarketsStore.getState().lowVolumeMarkets; - }, - // marketStats: () => { - // return MarketsStore.getState().allMarketStats; - // } + } }}> diff --git a/app/components/LoadingIndicator.jsx b/app/components/LoadingIndicator.jsx index 1678b75e26..f6db6036b3 100644 --- a/app/components/LoadingIndicator.jsx +++ b/app/components/LoadingIndicator.jsx @@ -42,6 +42,7 @@ class LoadingIndicator extends React.Component { return (
+
{this.props.loadingText}
diff --git a/app/components/Wallet/BalanceClaimActive.jsx b/app/components/Wallet/BalanceClaimActive.jsx index 4e62949bb9..4caad9987f 100644 --- a/app/components/Wallet/BalanceClaimActive.jsx +++ b/app/components/Wallet/BalanceClaimActive.jsx @@ -120,7 +120,7 @@ BalanceClaimActive = connect(BalanceClaimActive, { }, getProps() { let props = BalanceClaimActiveStore.getState(); - props.account_refs = AccountRefsStore.getState().account_refs; + props.account_refs = AccountRefsStore.getAccountRefs(); return props; } }); diff --git a/app/routerTransition.js b/app/routerTransition.js index 9cc2a9fe18..32204bcc8e 100644 --- a/app/routerTransition.js +++ b/app/routerTransition.js @@ -83,7 +83,7 @@ const willTransitionTo = (nextState, replaceState, callback, appInit=true) => { } } const currentChain = Apis.instance().chain_id; - const chainChanged = oldChain && oldChain !== currentChain; + const chainChanged = oldChain !== currentChain; oldChain = currentChain; var db; try { @@ -95,38 +95,42 @@ const willTransitionTo = (nextState, replaceState, callback, appInit=true) => { return callback(); } return Promise.all([db, SettingsStore.init()]).then(() => { - return Promise.all([ - PrivateKeyActions.loadDbData().then(()=> { - AccountRefsStore.loadDbData(); - }), - WalletDb.loadDbData().then(() => { - // if (!WalletDb.getWallet() && nextState.location.pathname === "/") { - // replaceState("/dashboard"); - // } - if (nextState.location.pathname.indexOf("/auth/") === 0) { - replaceState("/dashboard"); - } - }).then(() => { - if (chainChanged) { - ChainStore.clearCache(); - ChainStore.subscribed = false; - ChainStore.init().then(() => { + let chainStoreResetPromise = chainChanged ? ChainStore.resetCache() : Promise.resolve(); + return chainStoreResetPromise.then(() => { + return Promise.all([ + PrivateKeyActions.loadDbData().then(()=> { + return AccountRefsStore.loadDbData(); + }), + WalletDb.loadDbData().then(() => { + // if (!WalletDb.getWallet() && nextState.location.pathname === "/") { + // replaceState("/dashboard"); + // } + if (nextState.location.pathname.indexOf("/auth/") === 0) { + replaceState("/dashboard"); + } + }).then(() => { + if (chainChanged) { + // ChainStore.clearCache(); + // ChainStore.subscribed = false; + // return ChainStore.resetCache().then(() => { AccountStore.reset(); - AccountStore.loadDbData(currentChain).catch(err => { + return AccountStore.loadDbData(currentChain).catch(err => { console.error(err); }); - }); - } - }) - .catch((error) => { - console.error("----- WalletDb.willTransitionTo error ----->", error); - replaceState("/init-error"); - }), - WalletManagerStore.init() - ]).then(()=> { - SettingsActions.changeSetting({setting: "activeNode", value: connectionManager.url}); - callback(); - }); + // }); + } + }) + .catch((error) => { + console.error("----- WalletDb.willTransitionTo error ----->", error); + replaceState("/init-error"); + }), + WalletManagerStore.init() + ]).then(()=> { + SettingsActions.changeSetting({setting: "activeNode", value: connectionManager.url}); + callback(); + }); + }) + }).catch(err => { console.error(err); replaceState("/init-error"); diff --git a/app/stores/AccountRefsStore.js b/app/stores/AccountRefsStore.js index 38d089d9c7..8706e66f0c 100644 --- a/app/stores/AccountRefsStore.js +++ b/app/stores/AccountRefsStore.js @@ -11,7 +11,7 @@ class AccountRefsStore extends BaseStore { constructor() { super() - this._export("loadDbData") + this._export("loadDbData", "getAccountRefs"); this.state = this._getInitialState() this.bindListeners({ onAddPrivateKey: PrivateKeyActions.addKey }) this.no_account_refs = Immutable.Set() // Set of account ids @@ -21,23 +21,34 @@ class AccountRefsStore extends BaseStore { _getInitialState() { this.chainstore_account_ids_by_key = null; this.chainstore_account_ids_by_account = null; + let account_refs = new Immutable.Map(); + account_refs = account_refs.set(this._getChainId(), Immutable.Set()); return { - account_refs: Immutable.Set() - // loading_account_refs: false - } + account_refs + }; + } + + getAccountRefs(chainId = this._getChainId()) { + return this.state.account_refs.get(chainId, Immutable.Set()); + } + + _getChainId() { + return Apis.instance().chain_id || "4018d7844c78f6a6c41c6a552b898022310fc5dec06da467ee7905a8dad512c8"; } onAddPrivateKey({private_key_object}) { if(ChainStore.getAccountRefsOfKey(private_key_object.pubkey) !== undefined) - this.chainStoreUpdate() + this.chainStoreUpdate(); } loadDbData() { - // this.setState(this._getInitialState()) this.chainstore_account_ids_by_key = null; this.chainstore_account_ids_by_account = null; this.no_account_refs = Immutable.Set(); - this.state = {account_refs: Immutable.Set()}; + + let account_refs = new Immutable.Map(); + account_refs = account_refs.set(this._getChainId(), Immutable.Set()); + this.state = {account_refs}; return loadNoAccountRefs() .then( no_account_refs => this.no_account_refs = no_account_refs ) .then( ()=> this.chainStoreUpdate() ); @@ -55,7 +66,7 @@ class AccountRefsStore extends BaseStore { checkPrivateKeyStore() { let no_account_refs = this.no_account_refs - let account_refs = Immutable.Set() + let temp_account_refs = Immutable.Set() PrivateKeyStore.getState().keys.keySeq().forEach( pubkey => { if(no_account_refs.has(pubkey)) return let refs = ChainStore.getAccountRefsOfKey(pubkey) @@ -76,21 +87,21 @@ class AccountRefsStore extends BaseStore { no_account_refs = no_account_refs.add(pubkey) return } - account_refs = account_refs.add(refs.valueSeq()) + temp_account_refs = temp_account_refs.add(refs.valueSeq()) }) - account_refs = account_refs.flatten(); + temp_account_refs = temp_account_refs.flatten(); /* Discover accounts referenced by account name in permissions */ - account_refs.forEach(account => { + temp_account_refs.forEach(account => { let refs = ChainStore.getAccountRefsOfAccount(account); if(refs === undefined) return; if( ! refs.size) return; - account_refs = account_refs.add(refs.valueSeq()); + temp_account_refs = temp_account_refs.add(refs.valueSeq()); }); - account_refs = account_refs.flatten(); - if( ! this.state.account_refs.equals(account_refs)) { + temp_account_refs = temp_account_refs.flatten(); + if( ! this.getAccountRefs().equals(temp_account_refs)) { + this.state.account_refs = this.state.account_refs.set(this._getChainId(), temp_account_refs); // console.log("AccountRefsStore account_refs",account_refs.size); - this.setState({account_refs}) } if(!this.no_account_refs.equals(no_account_refs)) { this.no_account_refs = no_account_refs diff --git a/app/stores/AccountStore.js b/app/stores/AccountStore.js index ac8f8f823c..6d584cf911 100644 --- a/app/stores/AccountStore.js +++ b/app/stores/AccountStore.js @@ -90,7 +90,7 @@ class AccountStore extends BaseStore { } else { accountStorage.remove("referralAccount"); } - console.log("referralAccount", referralAccount); + if (referralAccount) console.log("referralAccount", referralAccount); return referralAccount; } @@ -181,7 +181,6 @@ class AccountStore extends BaseStore { loadDbData() { let linkedAccounts = Immutable.Set().asMutable(); let chainId = Apis.instance().chain_id; - return new Promise((resolve, reject) => { iDB.load_data("linked_accounts") .then(data => { @@ -227,7 +226,7 @@ class AccountStore extends BaseStore { addAccountRefs() { // Simply add them to the linkedAccounts list (no need to persist them) - let account_refs = AccountRefsStore.getState().account_refs; + let account_refs = AccountRefsStore.getAccountRefs(); if( ! this.initial_account_refs_load && this.account_refs === account_refs) { return this.setState({refsLoaded: true}); } @@ -489,7 +488,7 @@ class AccountStore extends BaseStore { checkAccountRefs() { // Simply add them to the linkedAccounts list (no need to persist them) - let account_refs = AccountRefsStore.getState().account_refs; + let account_refs = AccountRefsStore.getAccountRefs(); account_refs.forEach(id => { let account = ChainStore.getAccount(id); diff --git a/app/stores/AddressIndex.js b/app/stores/AddressIndex.js index 0553fa5528..e2a3761b86 100644 --- a/app/stores/AddressIndex.js +++ b/app/stores/AddressIndex.js @@ -103,7 +103,7 @@ class AddressIndex extends BaseStore { if(this.loadAddyMapPromise) return this.loadAddyMapPromise; this.loadAddyMapPromise = iDB.root.getProperty("AddressIndex").then( map => { this.state.addresses = map ? Immutable.Map(map) : Immutable.Map(); - console.log("AddressIndex load", this.state.addresses.size); + // console.log("AddressIndex load", this.state.addresses.size); this.state.addresses.valueSeq().forEach( pubkey => this.pubkeys.add(pubkey) ); this.setState({ addresses: this.state.addresses }); }); @@ -113,7 +113,7 @@ class AddressIndex extends BaseStore { saveAddyMap() { clearTimeout(this.saveAddyMapTimeout); this.saveAddyMapTimeout = setTimeout(()=> { - console.log("AddressIndex save", this.state.addresses.size); + // console.log("AddressIndex save", this.state.addresses.size); this.setState({saving: false}); // If indexedDB fails to save, it will re-try via PrivateKeyStore calling this.add return iDB.root.setProperty("AddressIndex", this.state.addresses.toObject()); diff --git a/package.json b/package.json index cdd6335ce6..0a32d7812f 100644 --- a/package.json +++ b/package.json @@ -125,7 +125,7 @@ "alt-container": "^1.0.0", "alt-react": "0.0.1", "bignumber.js": "^4.0.0", - "bitsharesjs": "^1.3.2", + "bitsharesjs": "^1.3.3", "classnames": "^2.2.1", "cookies-js": "^1.2.1", "counterpart": "^0.17.1",