Skip to content

Commit

Permalink
Merge branch 'develop' into feature/CORE-1121-ipfs-cache-aborted
Browse files Browse the repository at this point in the history
  • Loading branch information
Tschuck authored Mar 12, 2020
2 parents a95f432 + 6733e66 commit 8c2930f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 111 deletions.
4 changes: 2 additions & 2 deletions src/app/dapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down Expand Up @@ -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);
}
}));
}
}
}
Expand Down
35 changes: 21 additions & 14 deletions src/app/ens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const contractFuncSigs = {

const loadedEns: any = { };
const ensCache: any = ((): any => {
export const ensCache: any = ((): any => {

// reset ens cache
const { dappBrowserBuild } = (window as any);
if (dappBrowserBuild !== window.localStorage['evan-dapp-browser-build']) {
Expand All @@ -56,11 +58,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<any> {
const [, , protocol, host, defaultPort] = config.web3Provider
.match(/^((http[s]?|ftp|ws[s]):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?/);
Expand Down Expand Up @@ -189,6 +186,23 @@ export async function getContentHashForAddress(address: string): Promise<string>
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.
*
Expand Down Expand Up @@ -222,17 +236,10 @@ 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];
}

// save ens cache
window.localStorage['evan-ens-cache'] = JSON.stringify(ensCache);
setEnsCache(address, dbcp);
loadedEns[address] = dbcp;

return dbcp;
} catch (ex) {
const errMsg = `Could not parse content of address ${address}: ${ipfsHash} (${ex.message})`;
Expand Down
39 changes: 15 additions & 24 deletions src/app/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,7 +34,6 @@ let lastRootENS: string;

export let router: any;
export let defaultDAppENS: string;
export let history: string[];

/**
* Go to onboarding. (#/onboarding.evan)
Expand Down Expand Up @@ -134,7 +134,19 @@ export async function onRouteChange(): Promise<void> {
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);
Expand All @@ -150,19 +162,6 @@ export async function onRouteChange(): Promise<void> {
* @return {void} resolved when routing was created
*/
export async function initialize(initialRoute?: string): Promise<void> {
// 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) => {
Expand Down Expand Up @@ -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
*
Expand Down
67 changes: 0 additions & 67 deletions src/static/dev.html

This file was deleted.

17 changes: 13 additions & 4 deletions src/systemjs/plugins/ens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,31 +35,41 @@ 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);
}
} catch (ex) {
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) || '';
};

/**
Expand Down

0 comments on commit 8c2930f

Please sign in to comment.