-
-
Save dherman/c2559e9c176db9770c595574788ede99 to your computer and use it in GitHub Desktop.
a tiny userland registry, using a "parse" and "link" primitive
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
import {b} from "./b.js"; | |
import $ from "jquery"; | |
export function a() { | |
// ... | |
} |
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
import {a} from "./a.js"; | |
import $ from "jquery"; | |
export function b() { | |
// ... | |
} |
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
class TinyLittleRegistry { | |
constructor(realm, jquery) { | |
this.#realm = realm; | |
this.#jquery = jquery; | |
} | |
@lazy records() { | |
return Promise.all([fetch("a.js"), fetch("b.js")]) | |
.then(sources => sources.map(s => this.#realm.parseModule(s))); | |
} | |
@lazy async graph() { | |
let { a, b } = await this.records(); | |
a.link({ | |
"./b.js": b, | |
"jquery": this.#jquery | |
}); | |
b.link({ | |
"./a.js": a, | |
"jquery": this.#jquery | |
}); | |
return { a, b }; | |
} | |
} | |
import * as jquery from "jquery"; | |
class TinyLittleRealm extends Realm { | |
constructor() { | |
super(); | |
this.#registry = new TinyLittleRegistry(this, jquery); | |
} | |
[Realm.import](name, referrer) { | |
if (["a", "b"].indexOf(name) < 0) { | |
throw new ReferenceError("unrecognized module: " + name); | |
} | |
return this.#registry.graph().then(graph => graph[name]); | |
} | |
} | |
let realm = new TinyLittleRealm(); | |
realm.eval("import('a')").then(m => m.a()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment