Skip to content

Commit

Permalink
Making the registry iterable. See #1918. (#1919)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldenning authored and guybedford committed Apr 7, 2019
1 parent cc44bad commit 984dcd1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,19 @@ System.set('http://site.com/normalized/module/name.js', {
`module` is an object of names to set as the named exports.

If `module` is an existing Module Namespace, it will be used by reference.

#### System.entries() -> Array
Type: `Function`

Allows you to retrieve all modules in the System registry. Each value will be an array with two values: a key and the module. Also available
at `System[Symbol.iterator]`.

```js
System.entries().forEach((key, value) => {
console.log(entry); // ['http://localhost/path-to-file.js', {exportName: 'exportedValue'}]
});

for (let entry of System) {
console.log(entry); // ['http://localhost/path-to-file.js', {exportName: 'exportedValue'}]
}
```
23 changes: 22 additions & 1 deletion src/features/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,25 @@ systemJSPrototype.delete = function (id) {
depLoad.i.splice(importerIndex, 1);
});
return delete this[REGISTRY][id];
};
};

systemJSPrototype.entries = function () {
const registry = this[REGISTRY];
return Object.keys(registry).map(function (key) {
return [key, registry[key].n];
});
}

if (typeof Symbol !== 'undefined') {
systemJSPrototype[Symbol.iterator] = function () {
const registry = this[REGISTRY];
const keys = Object.keys(registry);
let index = 0;
return {
next() {
const key = keys[index++];
return { done: index > keys.length, value: key && [key, registry[key].n] };
}
};
}
}
21 changes: 21 additions & 0 deletions test/system-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,27 @@ describe('Core API', function () {
assert(resolveReturnValue instanceof Promise);
assert.equal(resolvedX, 'http://x');
});

it('Supports iteration', async function () {
loader.set('h', {a: 'b'});
await loader.import('h');

let foundH = false;
for (let entry of loader) {
if (entry[0] === 'h' && entry[1].a === 'b') {
foundH = true;
}
}

assert(foundH);
});

it('Supports System.entries', async function () {
loader.set('i', {a: 'b'});
await loader.import('i');

assert(loader.entries().some(entry => entry[0] === 'i' && entry[1].a === 'b'));
})
});
});

Expand Down

0 comments on commit 984dcd1

Please sign in to comment.