Skip to content

Commit

Permalink
Implement lazily-read VFSDirectoryResource
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonpayton committed Nov 25, 2024
1 parent 2c05cb2 commit 3ffc471
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions packages/playground/blueprints/src/lib/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,12 @@ export class VFSDirectoryResource extends Resource<Directory> {
}

async resolve() {
return Promise.reject('Not implemented');
const name = this.name;
const files = await createLazyVFSFileTree(
this.reference.path,
this.playground!
);
return { name, files };
}

/** @inheritDoc */
Expand Down Expand Up @@ -334,19 +339,37 @@ export class BlueprintAssetDirectoryResource extends VFSDirectoryResource {
}
}

// export class LazyVFSFileTree extends Proxy implements FileTree {
// #root: string;

// constructor(root: string) {
// this.#root = root;
// super({}, {
// ownKeys() {
export async function createLazyVFSFileTree(
path: string,
playground: UniversalPHP
): Promise<FileTree> {
const keys = await playground.listFiles(path);
const keySet = new Set(keys);

// }
// })
// }

// }
return new Proxy<FileTree>(
{},
{
ownKeys() {
return keys;
},
async get(target, prop: string) {
if (!keySet.has(prop)) {
return undefined;
}
const fullPath = joinPaths(path, prop);
if (!(await playground.fileExists(fullPath))) {
return undefined;
}

if (await playground.isDir(fullPath)) {
return createLazyVFSFileTree(fullPath, playground);
} else {
return playground.readFileAsBuffer(joinPaths(path, prop));
}
},
}
);
}

/**
* A `Resource` that represents a literal file.
Expand Down

0 comments on commit 3ffc471

Please sign in to comment.