Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,33 @@ describe('cacheManager', () => {
);
});

test('serialises the snapshot before anything can change it', async () => {
const cacheManager = new DiskCacheManager({buildParameters}, defaultConfig);
// The file map's own state, which may change at the next `await`.
const fileSystemData = new Map<string, unknown>();
const snapshot: CacheData = {
clocks: new Map(),
fileSystemData,
plugins: new Map(),
};
const written = serialize(snapshot);
const getSnapshot = () => {
queueMicrotask(() => {
fileSystemData.set('later.js', [0, 0, 0, null, 0]);
});
return snapshot;
};
await cacheManager.write(getSnapshot, {
changedSinceCacheRead: true,
eventSource: {onChange: () => () => {}},
onWriteError: () => {},
});
expect(mockWriteFile).toHaveBeenCalledWith(
cacheManager.getCacheFilePath(),
written,
);
});

test('creates the cache directory if it does not exist', async () => {
const cacheManager = new DiskCacheManager({buildParameters}, defaultConfig);
const getSnapshot = jest.fn(
Expand Down
11 changes: 10 additions & 1 deletion packages/metro-file-map/src/flow-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export interface CacheManager {
* crawler - i.e. internal state reflects the current file system state.
*
* getSnapshot may be retained and called at any time before end(), such as
* in response to eventSource 'change' events.
* in response to eventSource 'change' events. What it returns is the file
* map's own state, not a copy, and may change at the next `await`.
* Serialize it synchronously, or copy it to keep it.
*/
write(
getSnapshot: () => CacheData,
Expand Down Expand Up @@ -392,6 +394,7 @@ export interface FileSystem {
removedFiles: Set<string>,
filesToStat: Map<string, number>,
};
// A copy of the file system's data, which later changes don't affect.
getSerializableSnapshot(): CacheData['fileSystemData'];
getSha1(file: Path): ?string;
getOrComputeSha1(file: Path): Promise<?{sha1: string, content?: Buffer}>;
Expand Down Expand Up @@ -594,6 +597,12 @@ export interface ReadonlyFileSystemChanges<out T = FileMetadata> {
}

export interface MutableFileSystem extends FileSystem {
/**
* The file system's own data, not a copy, so it changes with the file
* system. Serialize it synchronously (see `CacheManager.write`), or use
* `getSerializableSnapshot()` for a copy.
*/
getSerializableState(): CacheData['fileSystemData'];
remove(filePath: Path, listener?: FileSystemListener): void;
addOrModify(
filePath: Path,
Expand Down
4 changes: 2 additions & 2 deletions packages/metro-file-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ export default class FileMap extends EventEmitter {
* 4. Serialize a snapshot of our raw data via the configured cache manager
*/
async #takeSnapshotAndPersist(
fileSystem: FileSystem,
fileSystem: MutableFileSystem,
clocks: WatchmanClocks,
plugins: ReadonlyArray<IndexedPlugin>,
changedSinceCacheRead: boolean,
Expand All @@ -920,7 +920,7 @@ export default class FileMap extends EventEmitter {
await this.#cacheManager.write(
() => ({
clocks: new Map(clocks),
fileSystemData: fileSystem.getSerializableSnapshot(),
fileSystemData: fileSystem.getSerializableState(),
plugins: new Map(
plugins.map(({plugin}) => [
plugin.name,
Expand Down
4 changes: 4 additions & 0 deletions packages/metro-file-map/src/lib/TreeFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ export default class TreeFS implements MutableFileSystem {
return this.#cloneTree(this.#rootNode);
}

getSerializableState(): CacheData['fileSystemData'] {
return this.#rootNode;
}

static fromDeserializedSnapshot(args: DeserializedSnapshotInput): TreeFS {
const {rootDir, fileSystemData, processFile} = args;
const tfs = new TreeFS({processFile, rootDir});
Expand Down
9 changes: 9 additions & 0 deletions packages/metro-file-map/src/lib/__tests__/TreeFS-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ describe.each([['win32'], ['posix']])('TreeFS on %s', platform => {
expect(deserialized.exists(p('/project/foo/another.js'))).toBe(true);
});

test('getSerializableSnapshot returns a copy, getSerializableState does not', () => {
const snapshot = tfs.getSerializableSnapshot();
const state = tfs.getSerializableState();
expect(snapshot).toEqual(state);
tfs.addOrModify(p('/project/foo/new.js'), [0, 1, 0, null, 0, null]);
expect(snapshot).not.toEqual(tfs.getSerializableSnapshot());
expect(state).toEqual(tfs.getSerializableSnapshot());
});

test('implements linkStats()', () => {
expect(tfs.linkStats(p('/project/link-to-foo/another.js'))).toEqual({
fileType: 'f',
Expand Down
Loading