Skip to content

Commit

Permalink
support System.has and System.set
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Feb 3, 2019
1 parent 1afd8f0 commit 6b85a8c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
39 changes: 34 additions & 5 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,51 @@ Resolves a module specifier relative to an optional parent URL, returning the re

### Registry API (system.js only)

#### System.delete(url) -> Boolean
#### System.delete(id) -> Boolean
Type: `Function`

Deletes a module from the registry by URL.
Deletes a module from the registry by ID.

Returns true if the module was found in the registry before deletion.

```js
System.delete('http://site.com/normalized/module/name.js');
```

#### System.get(url) -> Module
#### System.get(id) -> Module
Type: `Function`

Retrieve a loaded module from the registry by URL.
Retrieve a loaded module from the registry by ID.

```js
System.get('http://site.com/normalized/module/name.js').exportedFunction();
```
```

Module records with an error state will return `null`.

#### System.has(id) -> Boolean
Type: `Function`

Determine if a given ID is available in the loader registry.

Module records that have an error state in the registry still return `true`,
while module records with in-progress loads will return `false`.

```js
System.get('http://site.com/normalized/module/name.js').exportedFunction();
```

#### System.set(id, module) -> Module
Type: `Function`

Sets a module in the registry by ID.

```js
System.set('http://site.com/normalized/module/name.js', {
exportedFunction: value
});
```

`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.
34 changes: 34 additions & 0 deletions src/features/registry.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { systemJSPrototype, REGISTRY } from '../system-core.js';

const toStringTag = typeof Symbol !== 'undefined' && Symbol.toStringTag;

systemJSPrototype.get = function (id) {
const load = this[REGISTRY][id];
if (load && load.e === null && !load.E) {
Expand All @@ -9,6 +11,38 @@ systemJSPrototype.get = function (id) {
}
};

systemJSPrototype.set = function (id, module) {
let ns;
if (toStringTag && module[toStringTag] === 'Module') {
ns = module;
}
else {
ns = Object.assign(Object.create(null), module);
if (toStringTag)
Object.defineProperty(ns, toStringTag, { value: 'Module' });
}
const done = Promise.resolve(ns);
this.delete(id);
this[REGISTRY][id] = {
id: id,
i: [],
n: ns,
I: done,
L: done,
h: false,
d: [],
e: null,
eE: undefined,
E: undefined,
C: done
};
};

systemJSPrototype.has = function (id) {
const load = this[REGISTRY][id];
return load && load.e === null && !load.E;
};

// Delete function provided for hot-reloading use cases
systemJSPrototype.delete = function (id) {
const load = this.get(id);
Expand Down
10 changes: 10 additions & 0 deletions test/system-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,20 @@ describe('Core API', function () {
assert.equal(loader.get('x').y, 42);
});

it('Supports System.has', function () {
assert.equal(loader.has('x'), true);
});

it('Supports System.delete', function () {
loader.delete('x');
assert.equal(loader.get('x'), undefined);
});

it('Supports System.set', async function () {
loader.set('x', { y: 43 });
const x = await loader.import('x');
assert.equal(x.y, 43);
});
});
});

Expand Down

0 comments on commit 6b85a8c

Please sign in to comment.