Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split projectCache off from projectInfo; those are the problematic th…
…ings that don't quite work for opening a new file after it was created
- Loading branch information
1 parent
ee82ed5
commit 39e3af5
Showing
3 changed files
with
84 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// @flow | ||
|
||
import * as React from 'react'; | ||
|
||
import filer, { fs } from 'filer'; | ||
|
||
import { | ||
type FilerRecursiveStatInfo, | ||
Project, | ||
} from '../../../core/store/projects'; | ||
|
||
type ProjectCache = {| | ||
files: FilerRecursiveStatInfo, | ||
simulatorXml: string | null, | ||
|}; | ||
|
||
export default function useProjectCache( | ||
project: Project | null, | ||
): [ProjectCache | null, () => void] { | ||
const [state, setState] = React.useState<ProjectCache | null>(null); | ||
|
||
function refreshProject() { | ||
if (project === null) { | ||
setState(null); | ||
return; | ||
} | ||
|
||
async function loadSimulatorXml(): Promise<string | null> { | ||
const absolutePath = project.resolve('./.metadata/simulator'); | ||
try { | ||
const workspaceXml = await fs.promises.readFile(absolutePath, 'utf8'); | ||
if (workspaceXml === '') return null; | ||
return workspaceXml; | ||
} catch (ex) { | ||
if (!(ex instanceof filer.Errors.ENOENT)) { | ||
throw ex; | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
(async () => { | ||
// load project from the file system | ||
const files = await project.getFiles(); | ||
const simulatorXml = await loadSimulatorXml(); | ||
|
||
setState({ files, simulatorXml }); | ||
})(); | ||
} | ||
|
||
// refresh project when projectName changes | ||
React.useEffect(() => { | ||
refreshProject(); | ||
}, [project]); | ||
|
||
return [state, refreshProject]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters