Skip to content

Commit

Permalink
Add ability to do recursive loading
Browse files Browse the repository at this point in the history
  • Loading branch information
ferris committed Oct 2, 2024
1 parent 97060a1 commit 1ca1545
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/kicanvas/elements/kicanvas-embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class KiCanvasEmbedElement extends KCUIElement {
@attribute({ type: String })
zoom: "objects" | "page" | string | null;

custom_resolver: ((name: string) => URL) | null = null;

#schematic_app: KCSchematicAppElement;
#board_app: KCBoardAppElement;

Expand Down Expand Up @@ -115,7 +117,7 @@ class KiCanvasEmbedElement extends KCUIElement {
return;
}

const vfs = new FetchFileSystem(sources);
const vfs = new FetchFileSystem(sources, this.custom_resolver);
await this.#setup_project(vfs);
}

Expand Down
21 changes: 20 additions & 1 deletion src/kicanvas/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,33 @@ export class Project extends EventTarget implements IDisposable {

this.#fs = fs;

const promises = [];
let promises = [];

for (const filename of this.#fs.list()) {
promises.push(this.#load_file(filename));
}

await Promise.all(promises);

while (promises.length) {
// 'Recursively' resolve all schematics until none are remaining
promises = [];
for (const schematic of this.schematics()) {
for (const sheet of schematic.sheets) {
const sheet_sch = this.#files_by_name.get(
sheet.sheetfile ?? "",
) as KicadSch;

if (!sheet_sch && sheet.sheetfile) {
// Missing schematic, attempt to fetch
promises.push(this.#load_file(sheet.sheetfile));
}

}
}
await Promise.all(promises);
}

this.#determine_schematic_hierarchy();

this.loaded.open();
Expand Down
31 changes: 26 additions & 5 deletions src/kicanvas/services/vfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,35 @@ export abstract class VirtualFileSystem {
*/
export class FetchFileSystem extends VirtualFileSystem {
private urls: Map<string, URL> = new Map();
private resolver!: (name: string) => URL;

constructor(urls: (string | URL)[]) {
#default_resolver(name: string): URL {
const url = new URL(name, window.location.toString());
return url;
}

#resolve(filepath: string | URL): URL {
if (typeof filepath === 'string') {
let cached_url = this.urls.get(filepath);

Check failure on line 58 in src/kicanvas/services/vfs.ts

View workflow job for this annotation

GitHub Actions / lint

'cached_url' is never reassigned. Use 'const' instead
if (cached_url) {
return cached_url;
} else {
const url = this.resolver(filepath)
const name = basename(url);
this.urls.set(name, url);
return url;
}
}
return filepath;
}

constructor(urls: (string | URL)[], resolve_file: ((name: string) => URL) | null = null) {
super();

this.resolver = resolve_file ?? this.#default_resolver

for (const item of urls) {
const url = new URL(item, window.location.toString());
const name = basename(url);
this.urls.set(name, url);
this.#resolve(item)
}
}

Expand All @@ -66,7 +87,7 @@ export class FetchFileSystem extends VirtualFileSystem {
}

public override async get(name: string): Promise<File> {
const url = this.urls.get(name);
const url = this.#resolve(name);

if (!url) {
throw new Error(`File ${name} not found!`);
Expand Down

0 comments on commit 1ca1545

Please sign in to comment.