Skip to content

Commit

Permalink
project cache now contains a map of all of the projects assets, loade…
Browse files Browse the repository at this point in the history
…d into memory as binary
  • Loading branch information
SillyFreak committed Sep 17, 2020
1 parent a265037 commit 15f5a01
Showing 1 changed file with 55 additions and 4 deletions.
59 changes: 55 additions & 4 deletions src/components/ide/Ide/useProjectCache.js
Expand Up @@ -4,14 +4,20 @@ import * as React from 'react';

import filer, { fs } from 'filer';

import { type FilerRecursiveStatInfo, Project } from '../../../core/store/projects';
import {
type FilerRecursiveStatInfo,
type FilerRecursiveDirectoryInfo,
Project,
getDescendant,
} from '../../../core/store/projects';

import { useAsyncState } from '../../misc/hooks';

type ProjectCache = {|
files: FilerRecursiveStatInfo,
simulatorXml: string | null,
layoutJson: string | null,
assets: Map<string, Uint8Array>,
|};

export default function useProjectCache(
Expand All @@ -25,6 +31,51 @@ export default function useProjectCache(
return;
}

async function loadAssets(root: FilerRecursiveStatInfo): Promise<Map<string, Uint8Array>> {
const assetsMap: Map<string, Uint8Array> = new Map();

let assetsFile: FilerRecursiveStatInfo;
try {
assetsFile = getDescendant(root, '.metadata', 'assets');
} catch (err) {
console.error(err);
return assetsMap;
}

if (!assetsFile.isDirectory()) return assetsMap;
// $FlowExpectError
const assetsDir: FilerRecursiveDirectoryInfo = assetsFile;

async function walk(prefix: string, files: FilerRecursiveStatInfo[]) {
await Promise.all(
files.map(async f => {
if (f.isDirectory()) {
// $FlowExpectError
const dir: FilerRecursiveDirectoryInfo = f;
await walk(`${prefix}${f.name}/`, dir.contents);
} else {
const absolutePath = project.resolve('./.metadata/assets', prefix, f.name);
const asset = await fs.promises.readFile(absolutePath);
assetsMap.set(`asset:${prefix}${f.name}`, asset);
}
}),
);
}

await walk('', assetsDir.contents);

return assetsMap;
}

async function loadFilesAndAssets(): Promise<
[FilerRecursiveStatInfo, Map<string, Uint8Array>],
> {
const files = await project.getFiles();
const assets = await loadAssets(files);

return [files, assets];
}

async function loadSimulatorXml(): Promise<string | null> {
const absolutePath = project.resolve('./.metadata/simulator');
try {
Expand Down Expand Up @@ -54,13 +105,13 @@ export default function useProjectCache(

async function loadProjectCache() {
// load project from the file system
const [files, simulatorXml, layoutJson] = await Promise.all([
project.getFiles(),
const [[files, assets], simulatorXml, layoutJson] = await Promise.all([
loadFilesAndAssets(),
loadSimulatorXml(),
loadLayoutJson(),
]);

return { files, simulatorXml, layoutJson };
return { files, simulatorXml, layoutJson, assets };
}

setState(loadProjectCache());
Expand Down

0 comments on commit 15f5a01

Please sign in to comment.