Re: [w3ctag/design-reviews] Realms API ECMAScript Proposal (#542)

Thank you so much for the review, @kenchris, @LeaVerou, and @plinss!

>  1.a) If so, how would one get the default export of a module?

```js
await importValue('./file.js', 'default');
```

> 1.b) Presumably if one wants multiple exports from a module, they'd need to call importValue once for each export. Hopefully the module is only actually imported once?

We had this question in other channels. At first glance, we think about setting much of the control directly from Incubator Realm to Child Realm, but the modules injection can also be controlled with a module in between.

The example below uses `./inside-code.js` as this control module to load many bindings from the `test-runner` module.

```js
// ./inside-code.js
import { start, getTapReport } from 'test-runner';
import './test-file.js';
export default function(cb) {
  start()
    .then(getTapReport)
    .then(report => cb(report.toString()));
}
```

```js
// ./main.js
const r = new Realm();
const log = console.log.bind(console);

const runTests = await r.importValue('./inside-code', 'default');

runTests(log);
```

It's good to note that, anyway, consecutive `await importValue` calls with the same specifier would reuse the values cached in the module graph.

> 1.c) Have you considered a mechanism where multiple exports can be retrieved in one call? e.g. const [foo, bar] = realm.importValues('module.js', ['foo', 'bar']);

Yes! If we don't have a full usage picture, that's our initial intuition. Although, this demands more implementation details I was looking for in `importValue` as a low-level code and it's also possible in user land:

```js
// ./main.js
const r = new Realm();

async function importValues(realm, specifier, bindingList) {
  return Promise.all([].map.call(bindingList, bindingName => realm.importValue(specifier, bindingName)));
}

const [ padLeft, padRight ] = await importValues(r, './str-tools.js', ['padLeft', 'padRight']);
```

I'll answer the next questions in follow up comments here.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3ctag/design-reviews/issues/542#issuecomment-840861621

Received on Thursday, 13 May 2021 22:05:18 UTC