Skip to content

Commit

Permalink
Simplify and improve app init chain bitshares#531
Browse files Browse the repository at this point in the history
  • Loading branch information
svk31 committed Oct 11, 2017
1 parent c7b6e43 commit 8795eb0
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 89 deletions.
55 changes: 23 additions & 32 deletions app/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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"),
Expand All @@ -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);
}
Expand Down Expand Up @@ -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 = (
<Incognito onClickIgnore={this._onIgnoreIncognitoWarning.bind(this)}/>
);
} else if (this.state.syncFail) {
// if(incognito && !incognitoWarningDismissed){
// content = (
// <Incognito onClickIgnore={this._onIgnoreIncognitoWarning.bind(this)}/>
// );
// } else
if (this.state.syncFail) {
content = (
<SyncError />
);
} else if (this.state.loading) {
content = <div className="grid-frame vertical"><LoadingIndicator /></div>;
content = <div className="grid-frame vertical">
<LoadingIndicator loadingText={"Connecting to APIS and tarting app"}/>
</div>;
} else if (this.props.location.pathname === "/init-error") {
content = <div className="grid-frame vertical">{this.props.children}</div>;
} else {
Expand Down
5 changes: 1 addition & 4 deletions app/components/Dashboard/DashboardContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ class DashboardContainer extends React.Component {
},
lowVolumeMarkets: () => {
return MarketsStore.getState().lowVolumeMarkets;
},
// marketStats: () => {
// return MarketsStore.getState().allMarketStats;
// }
}
}}>
<Dashboard {...this.props} />
</AltContainer>
Expand Down
1 change: 1 addition & 0 deletions app/components/LoadingIndicator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class LoadingIndicator extends React.Component {
return (
<div className={classes}>
<div className="loading-panel">
<div className="text-center">{this.props.loadingText}</div>
<div className="spinner">
<div className="bounce1"></div>
<div className="bounce2"></div>
Expand Down
2 changes: 1 addition & 1 deletion app/components/Wallet/BalanceClaimActive.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
Expand Down
64 changes: 34 additions & 30 deletions app/routerTransition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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");
Expand Down
41 changes: 26 additions & 15 deletions app/stores/AccountRefsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() );
Expand All @@ -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)
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions app/stores/AccountStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class AccountStore extends BaseStore {
} else {
accountStorage.remove("referralAccount");
}
console.log("referralAccount", referralAccount);
if (referralAccount) console.log("referralAccount", referralAccount);
return referralAccount;
}

Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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});
}
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions app/stores/AddressIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
Expand All @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 8795eb0

Please sign in to comment.