Skip to content

Instantly share code, notes, and snippets.

@jfparadis
Forked from dckc/main.js
Last active January 10, 2020 08:02
Show Gist options
  • Save jfparadis/34245613447ef2371cd6588adc7e83a4 to your computer and use it in GitHub Desktop.
Save jfparadis/34245613447ef2371cd6588adc7e83a4 to your computer and use it in GitHub Desktop.
Compartment global acessor before imports, & multiple imports() calls per compartment

Compartment dynamic import & multiple imports per compartment

Output

mod1
getting g1: g1 original value
g1 original value
setting g1: g1 original value -> new value 1
mod1 done.
mod2
getting g1: new value 1
new value 1
setting g1: new value 1 -> new value 2
mod2 done.
trace("mod2\n");
trace(globalThis.g1 + '\n');
globalThis.g1 = 'new value';
trace('mod2 done.\n');
// Allows new Compartment("import").global.import("foo")
globalThis.import = async function(specifier) {
return import(specifier);
}
let g1Value = 'g1 original value';
const c1 = new Compartment("import");
Object.defineProperty(c1.global, 'g1', {
get() {
trace(`getting g1: ${g1Value}\n`);
return g1Value;
},
set(v) {
trace(`setting g1: ${g1Value} -> ${v}\n`);
g1Value = v;
},
});
c1.global.import("mod1");
c1.global.import("mod2");
{
"include": "$(MODDABLE)/examples/manifest_base.json",
"modules": {
"*": [
"./main",
"./mod1",
"./mod2",
"./import",
],
},
"preload": [
],
}
trace("mod1\n");
trace(globalThis.g1 + '\n');
globalThis.g1 = 'new value 1';
trace('mod1 done.\n');
trace("mod2\n");
trace(globalThis.g1 + '\n');
globalThis.g1 = 'new value 2';
trace('mod2 done.\n');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment