diff --git a/src/app/dapp.ts b/src/app/dapp.ts index 00770df..0e56bcf 100644 --- a/src/app/dapp.ts +++ b/src/app/dapp.ts @@ -245,7 +245,7 @@ export async function getDAppDependencies( // load all dependencies, check for its location and trigger the sub loading // eslint-disable-next-line no-restricted-syntax - for (const dependency of depKeys) { + await Promise.all(depKeys.map(async (dependency) => { // eslint-disable-next-line no-await-in-loop let subDefinition = await System .import(`${dependency}.${utils.getDomainName()}!ens`); @@ -285,7 +285,7 @@ export async function getDAppDependencies( // load recursive dependencies // eslint-disable-next-line no-await-in-loop await getDAppDependencies(dependency, subDefinition, depTree, deep); - } + })); } } } diff --git a/src/app/ens.ts b/src/app/ens.ts index 02bb73c..da2fdb0 100644 --- a/src/app/ens.ts +++ b/src/app/ens.ts @@ -38,7 +38,8 @@ const contractFuncSigs = { }, }; -const ensCache: any = ((): any => { +const loadedEns: any = { }; +export const ensCache: any = ((): any => { // reset ens cache const { dappBrowserBuild } = (window as any); if (dappBrowserBuild !== window.localStorage['evan-dapp-browser-build']) { @@ -55,11 +56,6 @@ const ensCache: any = ((): any => { return {}; })(); -/** - * is inserted when the application was bundled, used to prevent window usage - */ -declare let evanGlobals: any; - async function postToEthClient(requestString: string): Promise { const [, , protocol, host, defaultPort] = config.web3Provider .match(/^((http[s]?|ftp|ws[s]):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?/); @@ -188,6 +184,23 @@ export async function getContentHashForAddress(address: string): Promise return bytes32ToIpfsHash(result); } +/** + * Sets the ens cache for a ens address with a loaded dbcp description. + * + * @param {string} address ens address + * @param {any} dbcp dbcp.public + */ +export function setEnsCache(address: string, dbcp: any): void { + // set ens cache to speed up initial loading + if (dbcp.dapp.type === 'cached-dapp') { + ensCache[address] = JSON.stringify(dbcp); + } else { + delete ensCache[address]; + } + + window.localStorage['evan-ens-cache'] = JSON.stringify(ensCache); +} + /** * Resolves the content behind a ens address. * @@ -217,16 +230,8 @@ export async function resolveContent(address: string) { const ipfsResult = await ipfsCatPromise(ipfsHash); // parse the result const dbcp = JSON.parse(ipfsResult).public; - - // set ens cache to speed up initial loading - if (dbcp.dapp.type === 'cached-dapp') { - ensCache[address] = JSON.stringify(dbcp); - } else { - delete ensCache[address]; - } - + setEnsCache(address, dbcp); // save ens cache - window.localStorage['evan-ens-cache'] = JSON.stringify(ensCache); return dbcp; } catch (ex) { const errMsg = `Could not parse content of address ${address}: ${ipfsHash} (${ex.message})`; diff --git a/src/app/routing.ts b/src/app/routing.ts index 17447dd..231c1b4 100644 --- a/src/app/routing.ts +++ b/src/app/routing.ts @@ -17,9 +17,10 @@ the following URL: https://evan.network/license/ */ -import { startDApp } from './dapp'; import * as utils from './utils'; import Navigo from '../libs/navigo'; +import { ensCache } from './ens'; +import { startDApp, loadDApp } from './dapp'; /** * is inserted when the application was bundled, used to prevent window usage @@ -33,7 +34,6 @@ let lastRootENS: string; export let router: any; export let defaultDAppENS: string; -export let history: string[]; /** * Go to onboarding. (#/onboarding.evan) @@ -134,7 +134,19 @@ export async function onRouteChange(): Promise { activeRootENS = getActiveRootENS(); lastRootENS = activeRootENS; - await startDApp(activeRootENS); + // start main application + const mainDAppLoad = startDApp(activeRootENS); + + // preload nested dapps, if cache known ens cache is registered + const split = window.location.hash.replace(/#\/|#/g, '').split('?')[0].split('/'); + split.forEach((subEns: string) => { + if (subEns !== activeRootENS && ensCache[subEns]) { + utils.log(`preloading ${subEns} dapp`); + loadDApp(subEns); + } + }); + + await mainDAppLoad; } catch (ex) { utils.log(`Error while onRouteChange and startDApp (${activeRootENS})`); console.error(ex); @@ -150,19 +162,6 @@ export async function onRouteChange(): Promise { * @return {void} resolved when routing was created */ export async function initialize(initialRoute?: string): Promise { - // load history from cache - if (window.performance.navigation.type === 1 && !window.sessionStorage['evan-route-reloaded']) { - history = []; - } else { - try { - history = JSON.parse(window.sessionStorage['evan-route-history']); - } catch (ex) { } - } - - // setup history functions - history = history || []; - updateHistory(); - // watch for window reload to save, that the current session was reloaded delete window.sessionStorage['evan-route-reloaded']; window.addEventListener('beforeunload', (event) => { @@ -201,14 +200,6 @@ export function getRouteFromUrl(): string { .split('?')[0]; } -/** - * Takes the current navigation history and writes it to the sessionStorage if the user navigates to - * another page and navigates back - */ -export function updateHistory() { - window.sessionStorage['evan-route-history'] = JSON.stringify(history); -} - /** * Parse the url queryParams and return a specific parameter from it * diff --git a/src/static/dev.html b/src/static/dev.html deleted file mode 100644 index 39f7472..0000000 --- a/src/static/dev.html +++ /dev/null @@ -1,67 +0,0 @@ - - - - - - - evan.network - - - - - - - - - - - - - - - - - - - -
TESTCORE
-
- -
- - - diff --git a/src/systemjs/plugins/ens.ts b/src/systemjs/plugins/ens.ts index a1e520c..3adebb1 100644 --- a/src/systemjs/plugins/ens.ts +++ b/src/systemjs/plugins/ens.ts @@ -18,10 +18,9 @@ */ import * as utils from '../../app/utils'; -import System from '../index'; import { getQueryParameters } from '../../app/routing'; import { ipfsCatPromise } from '../../app/ipfs'; -import { resolveContent, parseToValidEnsAddress } from '../../app/ens'; +import { resolveContent, parseToValidEnsAddress, setEnsCache } from '../../app/ens'; export default function(System: any) { // libraries that should be cached @@ -36,19 +35,22 @@ export default function(System: any) { */ const getDefinitionFromEns = async function(ensAddress: string, domain: string) { // remove domain from the end of the ensAddress to get the dapp name - let split = parseToValidEnsAddress(ensAddress).split('.'); + const split = parseToValidEnsAddress(ensAddress).split('.'); const dappName = split.slice(0, split.length - 1).join('.'); // get correct ens address and check if a cached ens is availabled const validEnsAddress = parseToValidEnsAddress(ensAddress); let dbcp; + let updateEnsCache = false; try { if (utils.isDevAvailable(dappName) && ensAddress.indexOf('0x') !== 0) { // load json and resolve it as stringified dbcp = await System.import(`dapps/${dappName}/dbcp.json!json`); + updateEnsCache = true; } else if (validEnsAddress.indexOf('Qm') === 0) { dbcp = await ipfsCatPromise(validEnsAddress); + updateEnsCache = true; } else { dbcp = await resolveContent(validEnsAddress); } @@ -56,11 +58,18 @@ export default function(System: any) { console.dir(ex); } + // parse dbcp to the correct format if (typeof dbcp === 'string') { dbcp = JSON.parse(dbcp); } + dbcp = dbcp && dbcp.public ? dbcp.public : dbcp; - return JSON.stringify(dbcp && dbcp.public ? dbcp.public : dbcp) || ''; + // update ens cache for local / ipfs loaded dbcp + if (updateEnsCache && dbcp) { + setEnsCache(ensAddress, dbcp); + } + + return JSON.stringify(dbcp) || ''; }; /**